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);
}
}
RoundCornerTextview2
最新推荐文章于 2024-01-04 15:49:15 发布