ShapeTextView

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

ShapeTextView

属性的定义

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

 
        
            
            
        
        
        
        
        
        
        
        
        
        
    
属性 类型 作用
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)