public class MyView extends View{ private Paint mpaint1; private Paint mpaint2; private Paint mpaint3; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs,R.style.AppTheme); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); mpaint1 = new Paint(); mpaint2 = new Paint(); mpaint3 = new Paint(); //第一个圆 mpaint1.setColor(Color.RED); mpaint1.setAntiAlias(true); mpaint1.setStyle(Paint.Style.FILL); mpaint1.setStrokeWidth(1); canvas.drawCircle(getWidth()/2,getHeight()/2,70,mpaint1); //第二个圆 mpaint2.setColor(Color.GREEN); mpaint2.setAntiAlias(true); mpaint2.setStyle(Paint.Style.STROKE); mpaint2.setStrokeWidth(100); canvas.drawCircle(getWidth()/2,getHeight()/2,100,mpaint2); //第三个画笔 mpaint3.setColor(Color.BLUE); mpaint3.setAntiAlias(true); mpaint3.setStyle(Paint.Style.STROKE); mpaint3.setStrokeWidth(1); float yh=mpaint3.measureText("圆环"); canvas.drawText("圆环",(getWidth()-yh)/2,getHeight()/2,mpaint3); } //重写onTouchEvent方法点击在不同位置,提示不同位置 public boolean onTouchEvent(MotionEvent event) { int x; int y; switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //得到点击坐标 x= (int) event.getX(); y=(int) event.getY(); int x1=(x-getWidth()/2)*(x-getWidth()/2); int y1=(y-getHeight()/2)*(y-getHeight()/2); //判断点击位置是否在圆内 if (x1+y1<70*70){ Toast.makeText(getContext(),"小圆内",Toast.LENGTH_SHORT).show(); }else if (x1+y1<100*100&&x1+y1>70*70){ Toast.makeText(getContext(),"圆环内",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getContext(),"圆环外",Toast.LENGTH_SHORT).show(); } break; } return true; } }
点击圆点判断位置】
最新推荐文章于 2021-05-31 05:56:42 发布