android 自定义View drawable

先看效果图:
这里写图片描述

自定义的步骤:
1.自定义view的属性
2.获取view的属性
3.设置view的高和宽
4.重绘view

1、在values文件平下新attr.xml文件,内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="customTv">
    <attr name="img" format="reference"/>
    <attr name="textBg" format="reference"/>
</declare-styleable>
</resources>

2、在自定义view获取v自定义属性

    /**左边的icon*/
    private Bitmap mImg;
    /**引用的drawable selector*/
    private Drawable mDrawable;
    /**View 宽*/
    private int mWidth;
    /**View 高*/
    private int mHeight;
    /**画笔*/
    private Paint mPaint;

    public CustomTextView(Context context) {
        this(context, null);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context,attrs,defStyle);
    }

    private void initView(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.customTv,defStyle,0);
        int n = a.getIndexCount();
        for(int i = 0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.customTv_img:
                    mImg = BitmapFactory.decodeResource(getResources(),a.getResourceId(attr,0));
                    break;
                case R.styleable.customTv_textBg:
                    mDrawable = a.getDrawable(attr);
                    break;
            }
        }
        a.recycle();
        mPaint = new Paint();
        updateDrawable(mDrawable);
    }

    /**
     * 设置要改变drawable
     * @param d
     */
    private void updateDrawable(Drawable d) {
        d.setCallback(this);
        drawableStateChanged();
        invalidate();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        Drawable d = mDrawable;
        if (d != null && d.isStateful()) {
            d.setState(getDrawableState());
        }
    }

2、View 的高和宽

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = mImg.getWidth() + mDrawable.getIntrinsicWidth();
        mHeight = mImg.getHeight();
        setMeasuredDimension(mWidth,mHeight);
    }

4、绘制View

@Override
protected void onDraw(Canvas canvas) {
    /**绘制左边的Icon*/
    canvas.drawBitmap(mImg, 0f, 0f, mPaint);
    /**设置drawable范围*/
    mDrawable.setBounds(0,0,mDrawable.getIntrinsicWidth(),mDrawable.getIntrinsicHeight());
    /**画布右移50*/
    canvas.translate(50,0);
    /**绘制drawable*/
    mDrawable.draw(canvas);
}

只有右边的才有点击效果,左边的icon不需要点击交果,重写onTouchEvent()

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
              /**点击的坐标左边icon范围内,消费此次事件*/
                if((int)event.getX() < mImg.getWidth())
                    return false;
                break;
        }
        return super.onTouchEvent(event);
    }

源码地址:http://download.csdn.net/detail/ooppcool/8927453

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值