美文网首页
ShapeTextView

ShapeTextView

作者: violinlin | 来源:发表于2017-05-05 10:46 被阅读96次

自定义ShapeTextView 其实就是代码代替xml实现shape的过程

ShapeTextView

属性的定义

每个View都有一些它的特殊属性,在创建新的View的时候,应该考虑到它所具有的属性并在在res-values-styles文件中定义View需要的属性。关于属性的介绍可参考绘制钟表

 <declare-styleable name="ShapeTextView">
        <attr name="shape" format="enum">
            <enum name="rectangle" value="0" />
            <enum name="oval" value="1" />
        </attr>
        <attr name="solidNormal" format="color" />
        <attr name="solidPressed" format="color" />
        <attr name="cornersRadius" format="dimension" />
        <attr name="cornerTopLeft" format="dimension" />
        <attr name="cornerTopRight" format="dimension" />
        <attr name="cornerBottomLeft" format="dimension" />
        <attr name="cornerBottomRight" format="dimension" />
        <attr name="strokeWidth" format="dimension" />
        <attr name="strokeColor" format="color" />
    </declare-styleable>
属性 类型 作用
shape enum 枚举类型,定义了ovalrectangle常用的两种
solidNormal color 填充色(正常显示)
solidPressed color 填充色(点击显示)
cornersRadius dimension 圆角半径(shape:rectangle)可用
cornerTopLeft、cornerTopRight、cornerBottomLeft、cornerBottomRight dimension 自定义每个角的半径,不能同时设置cornersRadius属性,或设置cornersRadius为0
strokeWidth dimension 描边的宽度
strokeColor color 描边的颜色

创建ShapeTextView 继承TextView

在构造方法中获取自定义的属性

  public ShapeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView);
        shape = array.getInteger(R.styleable.ShapeTextView_shape, SHAPE_RECTANGEL);


        solidNormalColor = array.getColor(R.styleable.ShapeTextView_solidNormal, Color.parseColor("#00000000"));
        solidPressedColor = array.getColor(R.styleable.ShapeTextView_solidPressed, Color.parseColor("#00000000"));


        cornersRadius = array.getDimension(R.styleable.ShapeTextView_cornersRadius, 0);

        cornersTopLeft = array.getDimension(R.styleable.ShapeTextView_cornerTopLeft, 0);
        cornersTopRight = array.getDimension(R.styleable.ShapeTextView_cornerTopRight, 0);
        cornersBottomLeft = array.getDimension(R.styleable.ShapeTextView_cornerBottomLeft, 0);
        cornersBottomRight = array.getDimension(R.styleable.ShapeTextView_cornerBottomRight, 0);

        strokeWidth = array.getDimension(R.styleable.ShapeTextView_strokeWidth, 0);

        strokeColor = array.getColor(R.styleable.ShapeTextView_strokeColor, Color.parseColor("#00000000"));
        array.recycle();
    }

实现shape标签

使用GradientDrawable类在代码中实现shape标签中的属性

   // normal state
        GradientDrawable drawableNormal = new GradientDrawable();
        // 设置Shape
        drawableNormal.setShape(shape);
        // 设置圆角半径
        drawableNormal.setCornerRadius(cornersRadius);
        // 圆角半径(每个圆角半径的值)
        if (cornersRadius == 0) {
            drawableNormal.setCornerRadii(new float[]{
                    cornersTopLeft, cornersTopLeft,
                    cornersTopRight, cornersTopRight,
                    cornersBottomRight, cornersBottomRight,
                    cornersBottomLeft, cornersBottomLeft});
        }
        //描边的宽度和颜色
        drawableNormal.setStroke((int) strokeWidth, strokeColor);
        //设置填充色
        drawableNormal.setColor(solidNormalColor);

实现selector 标签

使用StateListDrawable在代码中实现selector标签中的属性

    // 设置背景选择器
        StateListDrawable stateListDrawable = new StateListDrawable();

        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);

        stateListDrawable.addState(new int[]{}, drawableNormal);

        // 设置视图的背景
        setBackground(stateListDrawable);

重写onDraw()方法

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setShape();//方法内主要内容为上面代码段
    }

效果预览

enter image description here

相关文章

网友评论

      本文标题:ShapeTextView

      本文链接:https://www.haomeiwen.com/subject/igmjtxtx.html