How to set ontouch listener for something drawn using canvas: Android

前端 未结 2 2017
轻奢々
轻奢々 2020-12-30 13:16

I have a custom view in which i am drawing one big circle and a small circle on the edge of this big circle.

I would like to move the small circle and so would like

相关标签:
2条回答
  • 2020-12-30 13:36

    try this (this is a little modified version of MyView i already posted as an answer for your previous question):

    public class MyView extends View {
        private final static String TAG = "Main.MyView";
    
        private static final float CX = 0;
        private static final float CY = 0;
        private static final float RADIUS = 20;
        private static final float BIGRADIUS = 50;
        private static final int NORMAL_COLOR = 0xffffffff;
        private static final int PRESSED_COLOR = 0xffff0000;
    
        private Paint mPaint;
        private Path mSmallCircle;
        private Path mCircle;
        private Matrix mMatrix;
        private float mAngle;
    
        private int mSmallCircleColor;
    
        public MyView(Context context) {
            super(context);
            mPaint = new Paint();
    
            mSmallCircle = new Path();
            mSmallCircle.addCircle(BIGRADIUS + RADIUS + CX, CY, RADIUS, Direction.CW);
            mSmallCircleColor = NORMAL_COLOR;
    
            mCircle = new Path();
            mCircle.addCircle(0, 0, BIGRADIUS, Direction.CW);
    
            mMatrix = new Matrix();
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_UP) {
                mSmallCircleColor = NORMAL_COLOR;
                invalidate();
                return false;
            }
            float w2 = getWidth() / 2f;
            float h2 = getHeight() / 2f;
            float r = 0;
            if (action == MotionEvent.ACTION_DOWN) {
                float[] pts = {
                        BIGRADIUS + RADIUS + CX, CY
                };
                mMatrix.mapPoints(pts);
                r = (float) Math.hypot(event.getX() - pts[0], event.getY() - pts[1]);
            }
            if (r < RADIUS) {
                mSmallCircleColor = PRESSED_COLOR;
                mAngle = (float) (180 * Math.atan2(event.getY() - h2, event.getX() - w2) / Math.PI);
                invalidate();
                return true;
            }
            return false;
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            float w2 = getWidth() / 2f;
            float h2 = getHeight() / 2f;
            mMatrix.reset();
            mMatrix.postRotate(mAngle);
            mMatrix.postTranslate(w2, h2);
    
            canvas.concat(mMatrix);
            mPaint.setColor(0x88ffffff);
            canvas.drawPath(mCircle, mPaint);
            mPaint.setColor(mSmallCircleColor);
            canvas.drawPath(mSmallCircle, mPaint);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 13:39

    You can get the rectangle of your touch point and the rectangle(position) of your inner circle and check if they cross over with the Intersects method.

    http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html#intersects(java.awt.Rectangle)

    Your canvas onTouchListener can do whatever it needs to do if the touchpoint intersect your circle.

    e.g:

        // Create a rectangle from the point of touch
        Rect touchpoint = new Rect(x,y,10,10);
    
        // Create a rectangle from the postion of the circle.
        Rect myCircle=new Rect(10,10,20,20);
    
        if (Rect.intersects(myCircle,touchpoint)){
            Log.d("The circle was touched");
        }
    
    0 讨论(0)
提交回复
热议问题