点击圆点判断位置】


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;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值