自定义控件

自定义控件
自定义控件一般继承自View类。
对控件自身来说比较重要的两个方法是onDraw()方法和onMeasure()方法。
onMeasure()
该方法在View类中有默认实现。
重写onMeasure()方法,该方法有两个入参
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	....
    }
其中widthMeasureSpec和heightMeasureSpec分别是32位整数,高2位代表SpecMode(测量模式),低30位表示sizeWidth(宽)或sizeHeight(高)。
这两个参数由父ViewGroup和自身的LayoutParamas属性共同决定。
SpecMode有三种情况,分别是 EXACTALY(有确定的值) , AT_MOST(最大不能超过某个值) , UNSUSPECIFIED(未指定)。
以上讨论的只是父ViewGroup建议我们View的宽高,具体指定宽高还是在于我们自己。通过调用setMeasuredDimension()来告诉父容器本控件的实际宽高。该方法接收两个参数,分别是宽和高。
在View的onMeasure()方法默认实现中,并没有对AT_MOST模式做特殊处理,也就是说默认情况下,当我们在xml使用该自定义控件时,若宽高都设置wrap_content,则该View的宽高会充满父容器。因此我们对这种情况稍作处理:
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        switch (widthMode) {
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                if(width<150)
                    width = 150;
                break;
        }
        switch (heightMode){
            case MeasureSpec.AT_MOST:
            case MeasureSpec.EXACTLY:
                if(height<100){
                    height = 100;
                }
                break;
        }
        BASE_INT = width/10;
        setMeasuredDimension(width,height);
    }
onDraw()
该方法用于绘制,在某些情况下,可能会快速重复调用,因此避免在该方法中创建对象是一个明智的选择。
onDraw()方法有一个参数Canvas,代表画布。若要绘制图形,则需要Paint类对象与画布来共同完成。
可扩展性
若要提高一个自定义View的扩展性,有如下办法。以设置背景颜色为例。可以将颜色作为xml中的参数传入View中,再由onDraw()方法绘制成背景。
具体的操作如下:
1.在values目录下创建一个自定义的xml文件,并通过name关联到自定义View。在attr标签中设置想要扩展的属性,并给出其标准,例如是颜色,字体,大小等。
<resources>
    <declare-styleable name="ChartView">
        <attr name="backgroundColor" format="color"/>
    </declare-styleable>
</resources>
2.在View中重写构造方法,并用context获取TypedArray,然后从TypeArray中获取在xml中设置的属性
public ChartView(Context context) {
        super(context);
        init();
    }

    public ChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ChartView);
        bgColor = array.getColor(R.styleable.ChartView_backgroundColor, Color.RED);
        array.recycle();
        init();
    }

    public ChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ChartView);
        bgColor = array.getColor(R.styleable.ChartView_backgroundColor, Color.RED);
        array.recycle();
        init();
    }

    public ChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context,attrs,defStyleAttr,defStyleRes);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ChartView);
        bgColor = array.getColor(R.styleable.ChartView_backgroundColor, Color.RED);
        array.recycle();
        init();
    }
3.在xml中声明对应的属性,在声明之前记得在该文件中加入
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.example.administrator.viewdemo01.ChartView
        android:layout_width="250dp"
        android:layout_height="250dp"
        app:backgroundColor="#fff333"
        android:layout_gravity="center"
        android:id="@+id/chartview"/>







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值