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

前端 未结 2 2018
轻奢々
轻奢々 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: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");
        }
    

提交回复
热议问题