如何自定义控件

如何自定义控件

###1、自定义属性的声明和提取

  • 1、分析需要的自定义属性
  • 2、在res/values/attrs.xml文件中定义声明
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="icon" format="reference"/>
    <attr name="color" format="color"/>
    <attr name="text" format="string"/>
    <attr name="text_size" format="dimension"/>
    <declare-styleable name="ChangeTextIconSizeWithColor">
        <attr name="icon"/>
        <attr name="color"/>
        <attr name="text"/>
        <attr name="text_size"/>
    </declare-styleable>
</resources>
复制代码
  • 3、在layout xml文件中进行使用
  • 4、在Viwe的构造方法中进行获取
 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeTextIconSizeWithColor);
        int n = a.getIndexCount();
        //也可以直接获取属性
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.ChangeTextIconSizeWithColor_color:
                    mColor = a.getColor(attr, 0xFF3343);
                    break;
                case R.styleable.ChangeTextIconSizeWithColor_icon:
                    BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
                    mBitmap = drawable.getBitmap();
                    break;
                case R.styleable.ChangeTextIconSizeWithColor_text:
                    mText = a.getString(attr);
                    break;
                case R.styleable.ChangeTextIconSizeWithColor_text_size:
                    mTextSize = a.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11, getResources().getDisplayMetrics()));

                    break;
            }
        }
        //资源回收
        a.recycle();
复制代码

###2、测量onMeasure()

  • 1、测量的模式:
    • EXACTRY
      • 及精确值模式,当我们将控件的layout_width属性或者layout_height属性指定为具体数值的时候,比如android:layout_width="100dp"。
    • AT_MOST
      • 最大值模式,当控件的layout_width属性或者layout_height属性指定wrap_content时候,控件大小一般随着控件的子空间内容的变化而变化,此时控件的尺寸只要不操作父控件允许的最大尺寸就行
    • UNSPECIFIED
      • 不指定测量的模式,想多大就多大 (一般在ScrollView或者ListView中)
  • 2、MeasureSpec(辅助类)
    • 通过这个类,就获取View的测量模式和View想要绘制的大小
    • 下面的代码就是一个模板,基本上没有什么变化的

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //super.onMeasure(widthMeasureSpec,heightMeasureSpec);//这个方法跟进去就是下面的这个方法
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

复制代码
   /**
     * 获取控件的高度
     *
     * @param heightMeasureSpec
     * @return
     */
    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        //模式
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        //大小
        int size = MeasureSpec.getSize(heightMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            result = 100;  //这个是指定好的控件的高度
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }

        return result;
    }
复制代码

获取宽度的代码和获取高度的差不多,这里就不写了,通过这里吗,就可以完成对高度和宽度值的自定义了 ###3、布局onLayout(ViewGroup)

  • 1、决定子View的位置
  • 2、尽可能的将onMeasure中的一些方法操作移动到此方法中(onLayout())
  • 3、requestLayout()
 @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            //拿到每一个view
            final View child = getChildAt(i);
            if (child.getVisibility() == GONE) {
                continue;
            }

            left = caculateChildLeft();//计算childView  layout的左上角的x坐标
            top = caculateChildTop();//计算childView  layout的左上交的y坐标

            child.layout(left, top, left + childWidth, top + childHeight);
        }
    }
复制代码

###4、绘制onDraw

  • 1、绘制内容区域
  • 2、invalidate()(UI线程),postInvalidate()(子线程)
  • 3、Canvas.drawXXX()
  • 4、translate、rotate、scale、skew
  • 5、sava()、restore() ###5、onTouchEvent
  • 1、触摸情况
    • ACTION_DOWN
    • ACTION_MOVE
    • ACTION_UP
  • 2、多指触摸(要决定哪个是控制的也就是ActivePointer)
    • ACTION_POINTER_DOWN
    • ACTION_POINTER_UP
  • 3、parent.requestDisallowInterceptTouchEvent(true)(请求父控件不要拦截事件,交给子控件拦截)
  • 4、VelocityTracker

###6、onInterceptTouchEvent(ViewGroup)

  • 1、触摸情况
    • ACTION_DOWN
    • ACTION_MOVE
    • ACTION_UP
  • 2、多指触摸(要决定哪个是控制的也就是ActivePointer)
    • ACTION_POINTER_DOWN
    • ACTION_POINTER_UP
  • 3、决定是否拦截该手势
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值