1 import android.content.Context; 2 import android.graphics.Canvas; 3 import android.graphics.Color; 4 import android.graphics.Paint; 5 import android.graphics.PaintFlagsDrawFilter; 6 import android.util.AttributeSet; 7 import android.widget.TextView; 8 9 public class CircleView extends TextView { 11 private Paint mBgPaint = new Paint(); 13 PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, 14 Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 15 16 public CircleView(Context context, AttributeSet attrs, int defStyle) { 17 super(context, attrs, defStyle); 19 } 20 21 public CircleView(Context context, AttributeSet attrs) { 22 super(context, attrs); 23 // TODO Auto-generated constructor stub 24 mBgPaint.setColor(Color.WHITE); 25 mBgPaint.setAntiAlias(true); 26 } 27 28 public CircleView(Context context) { 29 super(context); 30 // TODO Auto-generated constructor stub 31 mBgPaint.setColor(Color.WHITE); 32 mBgPaint.setAntiAlias(true); 33 } 34 35 @Override 36 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 37 // TODO Auto-generated method stub 38 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 39 int measuredWidth = getMeasuredWidth(); 40 int measuredHeight = getMeasuredHeight(); 41 int max = Math.max(measuredWidth, measuredHeight); 42 setMeasuredDimension(max, max); 43 } 44 45 @Override 46 public void setBackgroundColor(int color) { 47 // TODO Auto-generated method stub 48 mBgPaint.setColor(color); 49 } 50 51 /** 52 * 设置通知个数显示 53 * @param text 54 */ 55 public void setNotifiText(int text) { 56 setText(text + ""); 57 } 58 59 @Override 60 public void draw(Canvas canvas) { 61 // TODO Auto-generated method stub 62 canvas.setDrawFilter(pfd); 63 canvas.drawCircle(getWidth() / 2, getHeight() / 2, 64 Math.max(getWidth(), getHeight()) / 2, mBgPaint); 65 super.draw(canvas); 66 } 67 } 70 71 ============================= 74 75 private CircleView circleTextview; 76 circleTextview = (CircleView) findViewById(R.id.circleTextview); 77 circleTextview.setBackgroundColor(Color.RED);
时间: 2024-10-11 05:31:19