一、安卓繪圖流程
在Android平台上,圖形的渲染和顯示過程是非常複雜的。主要包括以下幾個處理步驟:
1、將顯示內容從應用程序發送到系統框架 ,
2、把圖形數據傳遞到OpenGL ES圖形引擎去處理,
3、OpenGL ES返回一組最終的像素值,此時保存在圖像緩衝區里
4、Android系統把這些像素複製到顯示緩存區,完成最終的顯示操作。
整個過程除了第1個步驟,都是通過系統內部多個進程進行的,因此這個過程實際上並不需要開發人員額外關心。
二、Android View繪製流程
View是Android應用程序的界面元素,任何繪製單元在屏幕上的顯示都必須通過它才能實現。它在Android的繪製流程中是非常重要的一個環節。以下是Android View繪製流程的詳細介紹:
1、構建視圖層次
視圖層次是指View的樹形結構,由一個或多個View和 ViewGroup組成。當應用程序啟動時,Android運行時系統會創建一個視圖層次,即Activity / Fragment 中的setContentView()中指定的布局。在Android中視圖的層次結構可以通過findViewById方法獲取。
View view = findViewById(R.id.view_id);
2、測量過程
View被繪製之前,需要先測量出它的尺寸和大小。在測量過程中,系統會要求每一個View通過onMeasure()來計算自己的寬度和高度,該方法在View對象中實現,是View類的抽象方法之一。在每次事件分發的開始時,會自動觸發View的測量過程。
舉個例子,我們在Activity中創建一個TextView,然後通過日誌來輸出它的寬度和高度:
TextView textView = findViewById(R.id.textview_id); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); textView.measure(widthMeasureSpec, heightMeasureSpec); Log.d("TAG", "TextView width: " + textView.getMeasuredWidth() + " height: " + textView.getMeasuredHeight());
3、布局過程
布局過程是指確定View在視圖層次中的位置和大小。在這個過程中,會通過onLayout()方法確定View的四個頂點坐標,即left、top、right、bottom。如果是ViewGroup,會在這個方法中對自己的子View進行布局。
下面是一個簡單的自定義ViewGroup的例子,自定義ViewGroup通過LayoutParams添加子View,重寫onLayout()方法對子View進行布局。
public class CustomLayout extends ViewGroup { public CustomLayout(Context context) { this(context, null); } public CustomLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width, height); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int count = getChildCount(); int curWidth, curHeight, curLeft, curTop, maxHeight; final int childLeft = this.getPaddingLeft(); final int childTop = this.getPaddingTop(); maxHeight = 0; curLeft = childLeft; curTop = childTop; for (int i = 0; i = r) { curLeft = childLeft; curTop += maxHeight; maxHeight = 0; } child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight); if (maxHeight < curHeight) maxHeight = curHeight; curLeft += curWidth; } } }
4、繪製過程
繪製過程是指在View的結構確定後,將View及其子View繪製到屏幕上的機制。在這個過程中,會通過onDraw()方法,將View顯示到屏幕上。最後會把所有View的繪製結果合併,並複製到Frame Buffer中,通過Surface給顯示屏幕顯示。
下面是一個簡單的自定義View的例子,通過onDraw()方法繪製一個矩形和圓形:
public class CustomView extends View { private Paint mPaint = new Paint(); private int mWidth; private int mHeight; public CustomView(Context context) { this(context, null); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(4); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(mWidth / 4, mHeight / 4, mWidth * 3 / 4, mHeight * 3 / 4, mPaint); canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 4, mPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); } }
三、結論
因為Android平台的繪製和顯示過程是非常複雜而且固定的。所以很多開發人員不需要了解細節,只需要知道Android提供的一系列API即可實現各種視圖的繪製和顯示。當然,對於一些對視圖的自定義更多的應用,需要掌握更多的內容和技巧。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286921.html