美文网首页
支持圆角的TextView

支持圆角的TextView

作者: 慕尼黑凌晨四点 | 来源:发表于2021-05-11 21:25 被阅读0次

代码出处:自定义支持圆角的TextView

不知道为什么,Google一直不给TextView官方支持radius属性,写布局文件又太麻烦,所以只能自己写自定义的TextView。

  • attrs.xml
    <!--支持圆角的TextView-->
    <declare-styleable name="RoundTextView">
        <attr name="rtvBgColor" format="color"/>
        <attr name="rtvBorderWidth" format="dimension"/>
        <attr name="rtvBorderColor" format="dimension"/>
        <attr name="rtvRadius" format="dimension"/>
    </declare-styleable>
  • Java版本
/**
 * 支持圆角的TextView
 * Created by stephen on 2017/12/18.
 */
public class RoundTextView extends android.support.v7.widget.AppCompatTextView {

    public RoundTextView(Context context) {
        this(context, null);
    }

    public RoundTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundTextView, defStyleAttr, 0);

        if (attributes != null) {

            int rtvBorderWidth = attributes.getDimensionPixelSize(R.styleable.RoundTextView_rtvBorderWidth, 0);
            int rtvBorderColor = attributes.getColor(R.styleable.RoundTextView_rtvBorderColor, Color.BLACK);
            float rtvRadius = attributes.getDimension(R.styleable.RoundTextView_rtvRadius, 0);
            int rtvBgColor = attributes.getColor(R.styleable.RoundTextView_rtvBgColor, Color.WHITE);
            attributes.recycle();

            GradientDrawable gd = new GradientDrawable();//创建drawable
            gd.setColor(rtvBgColor);
            gd.setCornerRadius(rtvRadius);
            if (rtvBorderWidth > 0) {
                gd.setStroke(rtvBorderWidth, rtvBorderColor);
            }

            this.setBackground(gd);
        }
    }

    public void setBackgroungColor(@ColorInt int color) {
        GradientDrawable myGrad = (GradientDrawable) getBackground();
        myGrad.setColor(color);
    }
}
  • Kotlin版本
class RoundTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

    private val rtvBorderWidth: Int
    private val rtvBorderColor: Int
    private val rtvRadius: Float
    private val rtvBgColor: Int

    init {
        context.theme.obtainStyledAttributes(attrs, R.styleable.RoundTextView, defStyleAttr, 0)
            .apply {
                try {
                    rtvBorderWidth =
                        getDimensionPixelSize(R.styleable.RoundTextView_rtvBorderWidth, 0)
                    rtvBorderColor = getColor(R.styleable.RoundTextView_rtvBorderColor, Color.BLACK)
                    rtvRadius = getDimension(R.styleable.RoundTextView_rtvRadius, 0f)
                    rtvBgColor = getColor(R.styleable.RoundTextView_rtvBgColor, Color.WHITE)
                } finally {
                    recycle()
                }
            }

        GradientDrawable().let {
            it.setColor(rtvBgColor)
            it.cornerRadius = rtvRadius
            it.setStroke(rtvBorderWidth, rtvBorderColor)
            this.background = it
        }
    }

    fun setBackgroungColor(@ColorInt color: Int) {
        val myGrad = background as GradientDrawable
        myGrad.setColor(color)
    }
}

在项目中使用可以减少嵌套,或减少布局文件了。


又想到了一种切圆角的方法,OutlineProvider,Android5.0以上可用,必须开启硬件加速。
即如果我们的应用设置了android:hardwareAccelerated="false",以下方式都将无效:

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int radius= Math.min(getWidth(), getHeight())/2;
        this.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, getWidth(), getHeight(), radius);
            }
        });
        this.setClipToOutline(true);
    }

相关文章

网友评论

      本文标题:支持圆角的TextView

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