Advanced light of Android -- View system and custom view

Keywords: Android Java

No1:

View sliding

1) Of the layout() method

public class CustomView extends View{
    private int lastX;
    private int lastY;
    public CustomView(Context context,AttributeSet attrs,int defStyleAttr){
        super(context,attrs,defStyleAttr);
    }
    public CustomView(Context context,AttributeSet attrs){
        super(context,attrs);
    }
    public CustomView(Context context){
        super(context);
    }
    
    public boolean onTouchEvent(MotionEvent event){
        //Obtain the abscissa and ordinate of finger touch point
        int x = (int)event.getX();
        int y = (int)event.getY();
        
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                //Calculate the distance to move
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                //call layout Method to reposition it
                layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY);
                break;
        }
        return true;
    }
}

2) offsetLeftAndRight() and offsetTopAndBottom()

Modify the above code

case MotionEvent.ACTION_MOVE:
    //Calculate the distance to move
    int offsetX = x - lastX;
    int offsetY = y - lastY;
    //Yes left and right Offset
    offsetLeftAndRight(offsetX);
    //Yes top and bottom Offset
    offsetTopAndBottom(offsetY);
    break;

3) Layoutparams (change layout parameters)

Also modify the above code

case MotionEvent.ACTION_MOVE:
    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
    layoutParams.leftMargin = getLeft() + offsetX;
    layoutParams.topMargin = getTop() + offsetY;
    setLayoutParams(layoutParams);
    break;

4) animation

5) scrollTo and scrollBy

scrollTo(x,y) means to move to a specific coordinate point, while scrollBy(x,y) means to move in dx, dy increments. In the end, scrollBy also calls scrollTo.

scrollBy and scrollTo source code of View.java

public void scrollTo(int x,int y){
    if(mScrollX!=x || mScrollY!=y){
        int oldX = mScrollX;
        int oldY = mScrollY;
        mScrollX = x;
        mScrollY = y;
        invalidateParentCaches();
        onScrollChanged(mScrollX,mScrollY,oldX,oldY);
        if(!awakenScrollBars()){
            postInvalidateOnAnimation();
        }
    }
}

public void scrollBy(int x,int y){
    scrollTo(mScrollX+x,mScrollY+y);
}

6)Scroller

public CustomView(Context context,AttributeSet attrs){
    super(context,attrs);
    mScroller = new Scroller(context);
}

@Override
public void computeScroll(){
    super.computeScroll();
    if(mScroller.computeScrollOffset()){
        ((View)getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
        invalidate();
    }
}

public void smoothScrollTo(int destX,int destY){
    int scrollX = getScrollX();
    int delta = destX-scrollX;
    mScroller.startScroll(scrollX,0,delta,0,2000);
    invalidate();
}

//Last call
mCustomView.smoothScrollTo(-400,0);

No2:

The measure process of View. The measureChildren method is defined in the ViewGroup

onLayout method is not implemented in View and ViewGroup

No3:

View's draw process

1) Draw background: View.drawBackground()

2) To draw the contents of a View: override View.onDraw()

3) Draw subview: ViewGroup.dispatchDraw() traverses the subview - > ViewGroup. Drawchild() - > View. Draw()

4) Draw decoration: view. Ondrawforegroup()

No4:

Custom View

1) Inherit custom View of system control: override onDraw()

2) Inherit the custom View of View: override onDraw(), consider the wap_content property and padding property settings, or custom properties, consider whether to override onTouchEvent()

3) Custom composite control

4) Custom ViewGroup: override onLayout(), process the warp ﹣ content property, handle sliding conflict, elastically slide to other pages, quickly slide to other pages, touch the screen again to prevent the page from sliding

Posted by mistjulia on Tue, 31 Mar 2020 08:12:22 -0700