RoundCornerTextview2

来源:互联网 发布:s7200梯形图编程实例 编辑:程序博客网 时间:2024/06/05 20:45
public class RoundCornerTextView2 extends TextView {    public RoundCornerTextView2(Context context) {        super(context);    }    //    public RoundCornerTextView2(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerTextView);        float dp = typedArray.getDimension(R.styleable.RoundCornerTextView_rc_corner_radius, 0.0f);        // 最终绘图的单位是像素        int cornerRadius = dp2px(context, dp);        typedArray.recycle();        // 替换 background        GradientDrawable background = new GradientDrawable();        background.setCornerRadius(cornerRadius);        background.setColor(Color.TRANSPARENT);        background.setStroke(2, getCurrentTextColor());        setBackground(background);    }    //    protected int dp2px(Context context, float dp) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (dp * scale + 0.5f);    }}

0 0
原创粉丝点击