main布局(自定义view控件)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.a_zidingiyiview_demo.RingView android:layout_width="match_parent" android:layout_height="match_parent"></com.example.a_zidingiyiview_demo.RingView> </LinearLayout>
自定义view代码
public class RingView extends View { private final Paint paint; private final Context context; private int i=1234; public RingView(Context context) { this(context,null); } public RingView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; this.paint = new Paint(); this.paint.setAntiAlias(true); //消除锯齿 this.paint.setStyle(Paint.Style.STROKE); //绘制空心圆 } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub int center = getWidth()/2; int innerCircle = dip2px(context, 40); //设置内圆半径 int ringWidth = dip2px(context, 5); //设置圆环宽度 //绘制内圆 this.paint.setARGB(155, 167, 190, 206); this.paint.setStrokeWidth(30); this.paint.setColor(Color.YELLOW); canvas.drawCircle(center,center, innerCircle, this.paint); //绘制文字 Paint textPaint = new Paint(); textPaint.setColor(Color.BLACK); textPaint.setStyle(Paint.Style.FILL); textPaint.setTextSize(25); canvas.drawText(String.valueOf(i),330,370,textPaint); //绘制圆环 /* this.paint.setARGB(255, 212 ,225, 233); this.paint.setStrokeWidth(2); this.paint.setColor(Color.BLUE); canvas.drawCircle(center,center, innerCircle+1+ringWidth/2, this.paint); //绘制外圆 /* this.paint.setARGB(155, 167, 190, 206); this.paint.setStrokeWidth(10); this.paint.setColor(Color.RED); canvas.drawCircle(center,center, innerCircle+ringWidth, this.paint);*/ super.onDraw(canvas); } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //随机数的工具 Random random = new Random(); //做计算 i=1001+(int)(Math.random()*1000); // count++; invalidate(); break; } return super.onTouchEvent(event); } }如有雷同 不胜荣幸!!!