ShapeTextView

自定义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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,455评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • NumPy 是 Python 中科学计算的基础包,很多其他的科学计算库都是构建在这个库之上,在 Numpy 官网上...
    gxyz阅读 214评论 0 1
  • 今天下午,带孩子去秋林书城读书。见到一个中年妇女,带着一个三岁的小男孩。这个男孩非常调皮,在非常安静的读书环境中,...
    心如美玉阅读 380评论 0 1