自定义View简介 - onMeasure,onDraw,自定义属性

1.自定义View简介

自定义View可以认为是继承自View或者ViewGroup。经常是处理一些系统没有的效果(如ImageView,TextView,Button实际也是自定义view)

2.自定义View介绍常用属性

三个构造函数

public class MyTextView extends View {

    //构造函数在代码new的时候调用
    public MyTextView(Context context) {
        super(context);
    }

    //在布局中使用
    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    //在不居中调用,布局中有style的情况
    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //布局的宽高都是有这个方法指定
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取宽高的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);


    }
}

onMeasure()方法

/**
     * 自定义view的测量方法
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //布局的宽高都是有这个方法指定
        //指定控件的宽高,需要测量
        //获取宽高的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        /**
         * MeasureSpec.AT_MOST : 在布局中指定了wrap_content
         * MeasureSpec.EXACTLY : 在不居中指定了确切的值  100dp   
match_parent  fill_parent
         * MeasureSpec.UNSPECIFIED : 尽可能的大,很少能用到,
ListView , ScrollView 在测量子布局的时候会用UNSPECIFIED
         */
        //获取宽高的大小
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    }

onDraw()

 /**
     * 用于绘制
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 画文本        
        canvas.drawText();        
        // 画弧       
        canvas.drawArc();        
        // 画圆       
        canvas.drawCircle();
    }

onTouchEvent:处理用户客户交互的

 /**
     * 处理用户客户交互的
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下
                Log.e("TAG", "手指按下");
                break;
            case MotionEvent.ACTION_MOVE:
                // 手指移动
                Log.e("TAG", "手指移动");
                break;
            case MotionEvent.ACTION_UP:
                // 手指抬起
                Log.e("TAG", "手指抬起");
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

3.自定义属性的一些细节

1.在res下的values下面新建attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--name 自定义View的名字 TextView-->
    <declare-styleable name="TextView">
        <!-- name 属性名称    format 格式:
         string 文字  color 颜色
         dimension 宽高 字体大小  integer 数字
         reference 资源(drawable)         -->
        <attr name="text" format="string" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="maxLength" format="integer" />
        <attr name="background" format="reference|color" />
        <!-- 枚举 -->
        <attr name="inputType">
            <enum name="number" value="1" />
            <enum name="text" value="2" />
            <enum name="password" value="3" />
        </attr>
    </declare-styleable>
</resources>

2.在布局中使用
声明命名空间,然后在自己的自定义View中使用

xmlns:app="http://schemas.android.com/apk/res-auto"
<com.lzy.view.TextView
        style="@style/defualt"
        android:text="Hello World!"
        app:text="@string/app_name"
        app:textColor="@color/colorAccent"/>

在自定义View中获取属性

// 获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextView);
        mText = array.getString(R.styleable.TextView_text);
        // 15 15px 15sp
        mTextColor = array.getColor(R.styleable.TextView_textColor, mTextColor);
        mTextSize = array.getDimensionPixelSize(R.styleable.TextView_textSize, mTextSize);
        // 回收
        array.recycle();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD钙奶-lalala

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值