码迷,mamicode.com
首页 > 移动开发 > 详细

Android - 自定义控件和属性(attr和TypedArray)

时间:2017-07-26 01:42:08      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:cycle   contex   values   format   数组   return   取值   csdn   android   

http://blog.csdn.net/zjh_1110120/article/details/50976027

 

1.attr format 取值类型

以ShapeView 为例

<declare-styleable name="ShapeViewStyle">
        <attr name="viewWidth" format="dimension|reference"/>
        <attr name="viewHeight" format="dimension|reference"/>
        <attr name="viewColor" format="color|reference"/>

        <attr name="viewShape" format="enum">
            <enum name="rect" value="0"/>
            <enum name="oval" value="1"/>
            <enum name="line" value="2"/>
        </attr>

        <attr name="lineWidth" format="dimension|reference"/>
        <attr name="lineDashWidth" format="dimension|reference"/>
        <attr name="lineDashGap" format="dimension|reference"/>
    </declare-styleable>

 

<com.example.administrator.llab.view.ShapeView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:viewColor="@color/red"
                    app:viewHeight="50dp"
                    app:viewShape="oval"
                    app:viewWidth="50dp"/>

 

TypedArray 详解

http://blog.csdn.net/zjh_1110120/article/details/50986589

大体意思是:TypedArray 是一个数组容器,在这个容器中装由 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtainAttributes(AttributeSet, int[]) 函数获取到的属性值。用完之后记得调用 recycle() 函数回收资源。索引值用来获取 Attributes 对应的属性值(这个 Attributes 将会被传入 obtainStyledAttributes() 函数)。

 

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ShapeViewStyle, defStyleAttr, 0);

        viewWidth = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_viewWidth, viewWidth);
        viewHeight = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_viewHeight, viewHeight);
        viewColor = typedArray.getColor(R.styleable.ShapeViewStyle_viewColor, viewColor);

        lineWidth = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_lineWidth, lineWidth);
        lineDashWidth = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_lineDashWidth, lineDashWidth);
        lineDashGap = (int) typedArray.getDimension(R.styleable.ShapeViewStyle_lineDashGap, lineDashGap);

        int shape = typedArray.getInt(R.styleable.ShapeViewStyle_viewShape, Shape.rect.ordinal());
        for (Shape shapeValue: Shape.values()) {
            if (shapeValue.ordinal() == shape) {
                viewShape = shapeValue;
                break;
            }
        }

        setImageDrawable(createShapeView());
    }

 

private LayerDrawable createShapeView() {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(viewColor);
        gradientDrawable.setSize(viewWidth, viewHeight);

        switch (viewShape) {
            case rect:
                gradientDrawable.setShape(GradientDrawable.RECTANGLE);
                break;
            case oval:
                gradientDrawable.setShape(GradientDrawable.OVAL);
                break;
            case line:
                gradientDrawable.setShape(GradientDrawable.LINE);
                gradientDrawable.setStroke(lineWidth, viewColor, lineDashWidth, lineDashGap);
                break;
        }
        return new LayerDrawable(new Drawable[]{gradientDrawable});
    }

 

Android - 自定义控件和属性(attr和TypedArray)

标签:cycle   contex   values   format   数组   return   取值   csdn   android   

原文地址:http://www.cnblogs.com/qlky/p/7237248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!