上一篇簡單說了下View的工作繪製原理,其中說到了幾個重要的方法measure、layout、draw…這篇主要記錄下自定義View這些方法的意思和使用。
一、onMeasure
測量View的大小,代碼實現如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/**
* 設置寬度
*/
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
{
mWidth = specSize;
} else
{
// 由圖片決定的寬
int desireByImg = getPaddingLeft() + getPaddingRight() + mImage.getWidth();
// 由字體決定的寬
int desireByTitle = getPaddingLeft() + getPaddingRight() + mTextBound.width();
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
int desire = Math.max(desireByImg, desireByTitle);
mWidth = Math.min(desire, specSize);
}
}
/***
* 設置高度
*/
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
if (specMode == MeasureSpec.EXACTLY)// match_parent
{
mHeight = specSize;
} else
{
int desire = getPaddingTop() + getPaddingBottom() + mImage.getHeight() + mTextBound.height();
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mHeight = Math.min(desire, specSize);
}
}
setMeasuredDimension(mWidth, mHeight);
}
由代碼見 用到了 specSize(測量的規格大小) 和 specMode(測量模式)。specSize 則就用於我們賦值寬高的大小,而specMode 則有三種模式:
類型 | 調用 | 描述 |
---|---|---|
UNSPECIFIED | MeasureSpec.UNSPECIFIED | 父容器不對View有任何限制,要多大給多大,一般用於系統內部 |
EXACTLY | MeasureSpec.EXACTLY | 父容器已經測出並指定了View的大小,此時View的大小就是specSize所指定的值。對應的是LayoutParams中的math_parent |
AT_MOST | MeasureSpec.AT_MOST | 父容器沒有指定View的具體大小,View的大小看具體實現,但大小又不能大於父容器指定的specSize得限制 |
最後通過 setMeasuredDimension 設置寬高。設置後可以通過getMeasureWidth 和 getMeasureHeight 方法獲取測量後的寬高。
二、onSizeChanged
確定View大小,並且在View的size改變後回調次方法。
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
三、onLayout
確定View在視圖中的位置。這個方法有點特別,包含兩個部分:一個是View,另一個是ViewGroup。分別看下源碼:
View:
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {}
ViewGroup:
@Override
protected abstract void onLayout(boolean changed, int l, int t, int r, int b);
根據源碼,可以看出,View的onlayout是一個空方法,ViewGroup是一個抽象方法,因爲onLayout()過程是爲了確定視圖在佈局中所在的位置,及父視圖確定子View的位置,所以ViewGroup 的 onLayout 是一個抽象方法,每個繼承ViewGroup的都要重寫這個方法:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (getChildCount() > 0) {
View childView = getChildAt(0);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (getChildCount() > 0) {
View childView = getChildAt(0);
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
}
}
在onLayout()過程結束後,我們就可以調用getWidth()方法和getHeight()方法來獲取視圖的寬高了。
上面說道的getMeasureWidth、getMeasureHeight 和 getWidth、getHeight 有什麼區別呢?
1、getMeasureWidth()方法在measure()過程結束後就可以獲取到了,而getWidth()方法要在layout()過程結束後才能獲取到
2、getMeasureWidth()方法中的值是通過setMeasuredDimension()方法來進行設置的,而getWidth()方法中的值則是通過視圖右邊的座標減去左邊的座標計算出來的
上面的代碼,這裏給子視圖的layout()方法傳入的四個參數分別是0、0、childView.getMeasuredWidth()和childView.getMeasuredHeight(),因此getWidth()方法得到的值就是childView.getMeasuredWidth() - 0 = childView.getMeasuredWidth() ,所以此時getWidth()方法和getMeasuredWidth() 得到的值就是相同的,但如果你將onLayout()方法中的代碼進行如下修改:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (getChildCount() > 0) {
View childView = getChildAt(0);
childView.layout(0, 0, 400, 400);
}
}
這樣getWidth()方法得到的值就是400 - 0 =
400,不會再和getMeasuredWidth()的值相同了。當然這種做法充分不尊重measure()過程計算出的結果,通常情況下是不推薦這麼寫的。
四、onDraw
繪製內容:
public class MyView extends View {
private PointF mPoint = new PointF(200, 200);
private Paint mPaint;
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(mPoint.x, mPoint.y, 100, mPaint);
}
}
上面是一個簡單繪製了一個圓。繪圖主要用到了Canvas(畫布) Paint(塗料)
Canvas 的一些主要方法:
- drawPath()
- drawLine()
- drawRect()
- drawCircle()
- drawOval()
- drawArc()
- drawPoint()
- drawBirmap()
- drawText()
Paint 的一些主要方法:
setAntiAlias(); //設置畫筆的鋸齒效果
setColor(); //設置畫筆的顏色
setARGB(); //設置畫筆的A、R、G、B值
setAlpha(); //設置畫筆的Alpha值
setTextSize(); //設置字體的尺寸
setStyle(); //設置畫筆的風格(空心或實心)
setStrokeWidth(); //設置空心邊框的寬度
getColor(); //獲取畫筆的顏色
———————————繪製方法和刷新方法的分割線—————————————
一、requestLayout
該方法的調用,會讓View樹重新進行一次onMeasure、onLayout、(onDraw)的過程。至於onDraw 會不會被調用有個小說明:
requestLayout如果沒有改變l,t,r,b,那就不會觸發onDraw;但是如果這次刷新是在動畫裏,mDirty非空,就會導致onDraw
二、invalidate
該方法的調用,會讓View樹重新執行onDraw方法,需要在主線程(UI線程)中使用。
二、postInvalidate
該方法和 invalidate 的作用是一樣的,都會是View執行onDraw,不同的是,postInvalidate 是在非UI線程中使用。
總結
自定義View篇: