android进阶-自定义控件之量算

知识储备:

  布局、测量、绘制;事件传递机制;动画;

           绘制: 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 设置高度。


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值