《Android进阶之光》--View体系与自定义View
No1:
View的滑動(dòng)
1)layout()方法的
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){//獲取手指摸點(diǎn)的橫坐標(biāo)和縱坐標(biāo)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://計(jì)算移動(dòng)的距離int offsetX = x - lastX;int offsetY = y - lastY;//調(diào)用layout方法來(lái)重新放置它的位置layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY);break;}return true;} }2)offsetLeftAndRight()與offsetTopAndBottom()
對(duì)上面代碼進(jìn)行修改
case MotionEvent.ACTION_MOVE://計(jì)算移動(dòng)的距離int offsetX = x - lastX;int offsetY = y - lastY;//對(duì)left和right進(jìn)行偏移 offsetLeftAndRight(offsetX);//對(duì)top和bottom進(jìn)行偏移 offsetTopAndBottom(offsetY);break;3)LayoutParams(改變布局參數(shù))
同樣對(duì)上面代碼進(jìn)行修改
case MotionEvent.ACTION_MOVE:ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();layoutParams.leftMargin = getLeft() + offsetX;layoutParams.topMargin = getTop() + offsetY;setLayoutParams(layoutParams);break;4)動(dòng)畫
5)scrollTo與scrollBy
scrollTo(x,y)表示移動(dòng)到一個(gè)具體的坐標(biāo)點(diǎn),而scrollBy(x,y)表示移動(dòng)的增量為dx、dy。其中scrollBy最終也是要調(diào)用scrollTo的。
View.java的scrollBy和scrollTo源碼
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(); }//最后調(diào)用 mCustomView.smoothScrollTo(-400,0);No2:
View的measure流程,ViewGroup中定義了measureChildren方法
View和ViewGroup中均沒(méi)有實(shí)現(xiàn)onLayout方法
No3:
View的draw流程
1)繪制背景:View.drawBackground()
2)繪制View的內(nèi)容:重寫View.onDraw()
3)繪制子View:ViewGroup.dispatchDraw()對(duì)子View進(jìn)行遍歷->ViewGroup.drawChild()->View.draw()
4)繪制裝飾:View.onDrawForeground()
No4:
自定義View
1)繼承系統(tǒng)控件的自定義View:重寫onDraw()
2)繼承View的自定義View:重寫onDraw()、考慮warp_content屬性以及padding屬性設(shè)置、或者自定義屬性、考慮是否重寫onTouchEvent()
3)自定義組合控件
4)自定義ViewGroup:重寫onLayout()、處理warp_content屬性、處理滑動(dòng)沖突、彈性滑動(dòng)到其他頁(yè)面、快速滑動(dòng)到其他頁(yè)面、再次觸摸屏幕阻止頁(yè)面繼續(xù)滑動(dòng)
轉(zhuǎn)載于:https://www.cnblogs.com/anni-qianqian/p/8505978.html
總結(jié)
以上是生活随笔為你收集整理的《Android进阶之光》--View体系与自定义View的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 土地备案登记怎么备案?
- 下一篇: 土地备案证怎么办理流程?