RoundCornnerTextView
来源:互联网 发布:网络剧无心法师 编辑:程序博客网 时间:2024/06/03 20:27
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); }}
0 0