android自定义view重写及调用的一些方法

一般重写、调用的方法

  • onMeasure
    测量本质就是测量本身有多大,也就是给mMeasuredWidth和mMeasuredHeight这两个属性赋值,也就是调用setMeasuredDimension这个方法。另外父view测量子view的时候调用的measure方法,还有一些衍生方法如measureChildWithMargins
  • onLayout
    作用是子view应该怎样放置,也就是设置子view的mLeft、mTop、mRight、mBottom属性。该方法在View中是空实现,很显然主要用于ViewGroup。父view放置子view的时候调用layout方法。
  • onDraw
    具体长什么样。

事件相关

这是的事件主要指的是MotionEvent

  • dispatchTouchEvent
  • onInterceptTouchEvent
    拦截事件,返回true说明要拦截该事件,既然拦截就应该处理即在onTouchEvent方法中消耗该事件。注:该方法只存在于ViewGroup中,很好理解对于View来说事件发给它了它就应该处理,因为它不想ViewGroup有子View,拦截的意思也就了然了,拦截的ViewGroup向子View的事件传递。
  • onTouchEvent
    消耗事件。

其他一些需要重写及调用的方法

  • onOverScrolled
    调用overScrollBy时调用该方法,overScrollBy源码注释的大致意思是滑动至边缘时返回true。该方法的应用不止于此,ScrollView和HorizontalScrollView是在onTouchEvent方法中通过调用overScrollBy实现滑动的,具体实现滑动的方法的是onOverScrolled。
protected void onOverScrolled(int scrollX, int scrollY,
            boolean clampedX, boolean clampedY) {
        // Treat animating scrolls differently; see #computeScroll() for why.
        if (!mScroller.isFinished()) {
            final int oldX = mScrollX;
            final int oldY = mScrollY;
            mScrollX = scrollX;
            mScrollY = scrollY;
            invalidateParentIfNeeded();
            onScrollChanged(mScrollX, mScrollY, oldX, oldY);
            if (clampedX) {
                mScroller.springBack(mScrollX, mScrollY, 0, getScrollRange(), 0, 0);
            }
        } else {
            super.scrollTo(scrollX, scrollY);
        }

        awakenScrollBars();
    }
  • generateDefaultLayoutParams()
    addView时默认的LayoutParams。
  • generateLayoutParams(AttributeSet attrs)
    LayoutInflater inflate时调用该方法,xml文件定义的View会调用此方法。
  • generateLayoutParams(ViewGroup.LayoutParams lp)
    addView时checkLayoutParams()返回为false时,调用该方法。
  • checkLayoutParams(ViewGroup.LayoutParams p)
    addView时调用,检测LayoutParams是否合理。
public void addView(View child, int index) {
        if (child == null) {
            throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
        }
        LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = generateDefaultLayoutParams();
            if (params == null) {
                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
            }
        }
        addView(child, index, params);
    }
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
                   .
                   .
                   .
        if (root != null) {
            if (DEBUG) {
                  System.out.println("Creating params from root: " + root);
             }
             // Create layout params that match root, if supplied
             params = root.generateLayoutParams(attrs);
             if (!attachToRoot) {
                 // Set the layout params for temp if we are not
                 // attaching. (If we are, we use addView, below)
                 temp.setLayoutParams(params);
             }
         }
                   .
                   .
                   .
}
private void addViewInner(View child, int index, LayoutParams params,
            boolean preventRequestLayout) {
                   .
                   .
                   .
        if (!checkLayoutParams(params)) {
            params = generateLayoutParams(params);
        }
                   .
                   .
                   .
}

这三个方法用于定义自定义控件的LayoutParams。一般自定义控件都有自己的LayoutParams,这个三个方法就是指明自定义控件的LayoutParams。

View的方法有很多,学到了再记录。

猜你喜欢

转载自blog.csdn.net/forever_love007/article/details/80735777
今日推荐