@SuppressLint("HandlerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("tag","onCreate");
setContentView(R.layout.activity_main);
}
public class DemoView extends View {
public DemoView(Context context) {
super(context);
Log.i("tag", "---- public DemoView(Context context) ----");
}
public DemoView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Log.i("tag", "---- public DemoView(Context context,AttributeSet attrs) ----");
}
public DemoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.i("tag", "---- public DemoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) ----");
}
/**
* 使用布局文件XML创建CustomView时,在xml文件加载完成后调用这个方法
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Log.i("tag", "---- onFinishInflate() ----");
}
/**
* CustomView的大小发生改变时调用这个方法
*
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.i("tag", "---- onSizeChanged = " + " w = " + w + " h = " + h + " oldW = " + oldw + " oldH = " + oldw);
}
/**
* 在画布上面绘制
*
* @param canvas
*/
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Log.i("tag", "---- dispatchDraw = ");
}
/**
* 绘制CustomView时调用
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("tag", "---- onDraw ----");
}
/**
* 测量CustomView的大小
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("tag", "---- onMeasure ----");
}
/**
* 将CustomView放置到父容器中去
*
* @param changed
* @param left
* @param top
* @param right
* @param bottom
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.i("tag", "---- onLayout ----");
}
/**
* 将CustomView依附到Window中
*/
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.i("tag", "---- onAttachedToWindow ----");
}
/**
* 当手机屏幕从横屏和竖屏 相互转化时调用
*
* @param newConfig
*/
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("tag", "---- onConfigurationChanged ----");
}
@Override
public void requestLayout() {
super.requestLayout();
Log.i("tag", "---- requestLayout ----");
}
@Override
public void invalidate() {
super.invalidate();
Log.i("tag", "---- invalidate ----");
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.example.handlerdemo.DemoView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/layout">
<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="Hello World!"-->
<!-- app:layout_constraintBottom_toBottomOf="parent"-->
<!-- app:layout_constraintLeft_toLeftOf="parent"-->
<!-- app:layout_constraintRight_toRightOf="parent"-->
<!-- app:layout_constraintTop_toTopOf="parent" />-->
</com.example.handlerdemo.DemoView>
首次打开activity:
activity处于onPause的时候:
重新在onResume: