android图像图像处理——自定义组件的属性

自定义组件的属性

步骤如下

【1】在Values资源文件下新建myview.xml

这里写图片描述

<resources>
    <declare-styleable name="myview">
        <attr name="myview_background" format="reference"/>
        <attr name="myview_paintwidth" format="reference|dimension"/>
    </declare-styleable>
</resources>

自定义两个属性背景图、画笔笔触大小;

【2】在布局文件中使用自定义属性

首先引入然后使用
这里写图片描述

【3】在Java文件中获取自定义组件信息

我们知道在在自定义组件Java文件中,的构造函数

public MyBitmapView2(Context context, AttributeSet attrs){}

attrs就是用来解析XML文件的
解析代码

public MyBitmapView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.myview);
        BitmapDrawable dra = (BitmapDrawable) a.getDrawable(R.styleable.myview_myview_background);
        Log.d("","得到背景图");
        if (dra != null) {
            Log.d("drawable", "" + dra.getIntrinsicWidth());
            bitmapBackground = dra.getBitmap();
        } else {
            bitmapBackground = BitmapFactory.decodeResource(getResources(), R.mipmap.cc);
        }


        int paintWidth=a.getDimensionPixelOffset(R.styleable.myview_myview_paintwidth,100);
        paintRect.setStrokeWidth(paintWidth);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值