public class RoundCornerTextView extends TextView {
public RoundCornerTextView(Context context) {
super(context);
}
//
public RoundCornerTextView(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
ColorDrawable originalBackground = (ColorDrawable) getBackground();
int bgColor = originalBackground != null ? originalBackground.getColor() : Color.TRANSPARENT;
PaintDrawable newBackground = new PaintDrawable();
newBackground.setCornerRadius(cornerRadius);
newBackground.getPaint().setColor(bgColor);
setBackground(newBackground);
}
//
protected int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
RoundCornnerTextView
最新推荐文章于 2024-01-04 15:49:15 发布