查看自定义ViewGroup 和 View 流程
虽然android提供了很多组件,但有时还是不能满足开发中的古怪需求,
所以自定义就不可避免,而每次自定义组件时都很混乱。
所以备份自定义组件的相关log,以便于查看
1.自定义ViewGroup
package com.weidingqiang.testviewb; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; /** * Created by weidingqiang on 15/11/20. */ public class BaseViewGroup extends ViewGroup { private static final String TAG = BaseViewGroup.class.getSimpleName(); public BaseViewGroup(Context context) { super(context); Log.d(TAG,">>>>>>BaseViewGroup>>>>>>"); } public BaseViewGroup(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, ">>>>>>BaseViewGroup>>>>>> AttributeSet "); } public BaseViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Log.d(TAG, ">>>>>>BaseViewGroup>>>>>> defStyleAttr"); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { measureChildren(widthMeasureSpec,heightMeasureSpec); Log.d(TAG,">>>>>>onMeasure>>>>>>"); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { Log.d(TAG,">>>>>>onSizeChanged>>>>>>"); super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { View view = getChildAt(0); view.layout(l,t,r,b); Log.d(TAG,">>>>>>onLayout>>>>>>"); } @Override protected void onFinishInflate() { Log.d(TAG,">>>>>>onFinishInflate>>>>>>"); super.onFinishInflate(); } @Override protected void onDraw(Canvas canvas) { Log.d(TAG, ">>>>>>onDraw>>>>>>"); } }
2 自定义 View
package com.weidingqiang.testviewb; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.View; /** * Created by weidingqiang on 15/11/20. */ public class BaseView extends View { private static final String TAG = BaseView.class.getSimpleName(); private Paint paint; public BaseView(Context context) { super(context); Log.d(TAG, ">>>>>>BaseView>>>>>>"); } public BaseView(Context context, AttributeSet attrs) { super(context, attrs); Log.d(TAG, ">>>>>>BaseView>>>>>> AttributeSet"); paint = new Paint(); paint.setTextSize(40); // mPaint.setColor(mTitleTextColor); } public BaseView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); Log.d(TAG, ">>>>>>BaseView>>>>>> defStyleAttr"); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Log.d(TAG, ">>>>>>onMeasure>>>>>>"); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { Log.d(TAG,">>>>>>onSizeChanged>>>>>>"); super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { Log.d(TAG, ">>>>>>onLayout>>>>>>"); } @Override protected void onFinishInflate() { Log.d(TAG,">>>>>>onFinishInflate>>>>>>"); super.onFinishInflate(); } @Override protected void onDraw(Canvas canvas) { Log.d(TAG, ">>>>>>onDraw>>>>>>"); paint.setColor(Color.YELLOW); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint); paint.setColor(Color.BLACK); canvas.drawText("beidanga", 100 , 100, paint); } }
3.布局文件
<com.weidingqiang.testviewb.BaseViewGroup android:id="@+id/baseviewgroup" android:layout_width="300dp" android:layout_height="300dp" > <com.weidingqiang.testviewb.BaseView android:layout_width="100dp" android:layout_height="100dp" /> </com.weidingqiang.testviewb.BaseViewGroup>
最终要的莫过于log信息了
打印如下:
11-19 06:35:22.248 D/BaseViewGroup: >>>>>>BaseViewGroup>>>>>> AttributeSet
11-19 06:35:22.249 D/BaseView: >>>>>>BaseView>>>>>> AttributeSet
11-19 06:35:22.249 D/BaseView: >>>>>>onFinishInflate>>>>>>
11-19 06:35:22.249 D/BaseViewGroup: >>>>>>onFinishInflate>>>>>>
11-19 06:35:22.286 D/BaseView: >>>>>>onMeasure>>>>>>
11-19 06:35:22.286 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:35:22.286 D/BaseView: >>>>>>onMeasure>>>>>>
11-19 06:35:22.286 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:35:22.414 D/BaseView: >>>>>>onMeasure>>>>>>
11-19 06:35:22.414 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:35:22.414 D/BaseView: >>>>>>onMeasure>>>>>>
11-19 06:35:22.414 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:35:22.414 D/BaseViewGroup: >>>>>>onSizeChanged>>>>>>
11-19 06:35:22.414 D/BaseView: >>>>>>onSizeChanged>>>>>>
11-19 06:35:22.414 D/BaseView: >>>>>>onLayout>>>>>>
11-19 06:35:22.414 D/BaseViewGroup: >>>>>>onLayout>>>>>>
11-19 06:35:22.436 D/BaseView: >>>>>>onMeasure>>>>>>
11-19 06:35:22.436 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:35:22.436 D/BaseView: >>>>>>onMeasure>>>>>>
11-19 06:35:22.436 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:35:22.436 D/BaseView: >>>>>>onLayout>>>>>>
11-19 06:35:22.436 D/BaseViewGroup: >>>>>>onLayout>>>>>>
11-19 06:35:22.438 D/BaseView: >>>>>>onDraw>>>>>>
而修改ViewGroup布局时
ViewGroup.LayoutParams layoutParams = baseViewGroup.getLayoutParams(); layoutParams.height+=30; baseViewGroup.setLayoutParams(layoutParams); //baseViewGroup.onSizeChanged(0,0,0,0);
打印log为:
11-19 06:37:52.449 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:37:52.449 D/BaseViewGroup: >>>>>>onMeasure>>>>>>
11-19 06:37:52.449 D/BaseViewGroup: >>>>>>onSizeChanged>>>>>>
11-19 06:37:52.449 D/BaseView: >>>>>>onSizeChanged>>>>>>
11-19 06:37:52.449 D/BaseView: >>>>>>onLayout>>>>>>
11-19 06:37:52.449 D/BaseViewGroup: >>>>>>onLayout>>>>>>
11-19 06:37:52.449 D/BaseView: >>>>>>onDraw>>>>>>
如果想刷新 draw 方法,则直接调用invalidate (UI线程中) 或者 postInvalidate

浙公网安备 33010602011771号