知识储备:
布局、测量、绘制;事件传递机制;动画;
绘制: canvas、paint、matrix
动画: property animation
孙群: 点击打开链接
量算、布局、绘图、触摸事件是自定义view的核心。
测量: onMeasure
MeasureSpec : UNSPECIFIED \ AT_MOST \ EXACTLY
继承自ImageView\TextView等已有的控件,其 onMeasure、onDraw方法已有实现,不用管onLayout,继承自viewGroup才需要实现onLayout。
直接继承自 View的控件,其onMeasure已实现,但是其onDraw是空方法实现,其onLayout也是空实现,但是不用管onLayout。
继承自view
public class MyView extends View {
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println("widthMeasureSpec = " + MeasureSpec.getSize(widthMeasureSpec));
System.out.println("heightMeasureSpec = " + MeasureSpec.getSize(heightMeasureSpec));
System.out.println("dendity = " + getContext().getResources().getDisplayMetrics().density);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawARGB(198, 255, 198, 100);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.adnonstop.bitmapshader.views.MyView
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
01-18 15:04:03.875 2648-2648/com.adnonstop.bitmapshader I/System.out: widthMeasure = 263
01-18 15:04:03.875 2648-2648/com.adnonstop.bitmapshader I/System.out: heightMeasure = 263
01-18 15:04:03.875 2648-2648/com.adnonstop.bitmapshader I/System.out: dendity = 2.625
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 l
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 t
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 263 r
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 263 b
先走测量,-》布局,-》绘制。
父布局是ScrollView
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.adnonstop.bitmapshader.views.MyView
android:layout_width="100dp"
android:layout_height="100dp" />
</ScrollView>
打印结果:
1-18 15:05:54.385 2648-2648/com.adnonstop.bitmapshader I/System.out: widthMeasure = 263
01-18 15:05:54.385 2648-2648/com.adnonstop.bitmapshader I/System.out: heightMeasure = 0
01-18 15:05:54.385 2648-2648/com.adnonstop.bitmapshader I/System.out: dendity = 2.625
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 l
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 t
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 263 r
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 b
高度未知;需要自己开发者帮助测量高度。
如果是HorizontalScrollView,则宽度未知
01-18 15:10:23.325 2648-2648/com.adnonstop.bitmapshader I/System.out: widthMeasure = 0
01-18 15:10:23.325 2648-2648/com.adnonstop.bitmapshader I/System.out: heightMeasure = 263
01-18 15:10:23.325 2648-2648/com.adnonstop.bitmapshader I/System.out: dendity = 2.625
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: l = 0
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: t = 0
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: r = 0
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: b = 263
未指定就没办法了,你得手动指定了:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
density = getContext().getResources().getDisplayMetrics().density;
widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (density * 100), MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (density * 100), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
打印结果:
01-18 15:44:35.665 15142-15142/? I/System.out: widthMeasure = 262
01-18 15:44:35.665 15142-15142/? I/System.out: heightMeasure = 262
01-18 15:44:35.665 15142-15142/? I/System.out: dendity = 2.625
01-18 15:44:35.795 15142-15142/? I/System.out: l = 0
01-18 15:44:35.795 15142-15142/? I/System.out: t = 0
01-18 15:44:35.795 15142-15142/? I/System.out: r = 262
01-18 15:44:35.795 15142-15142/? I/System.out: b = 262
继承自已有的TextView,父布局是scrollView的话,只会根据内容的高度分配高度,而不会根据xml里 layout_heighet = 100dp 设置高度。