2

I draw a shapedrawables on a canvas and I want to detect on touch event what shapes has been touched. without calculating by (x,y), just automatically as exists in html.. Thanks! :)

I succeeded to draw the shapes, and read a lot about how I can detect ontouch by calculating with (x,y), but it's not clever to do that if I have the possibility to detect the touched shapes automatically. If I can't do that with a canvas I will be glad to hear how can I draw shapes and detect touched shapes with another widgets in android.

public class CanvasView extends View implements View.OnTouchListener
{
    private List<ShapeDrawable> shapes;
    private ShapeDrawable currentCircle;
    public CanvasViewenter code here(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        this.shapes = new ArrayList<>();
    }
    @Override
    protected void onDraw(final Canvas canvas)
    {
        super.onDraw(canvas);
    }
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent)
    {
        //here I want to get all the shapes that have been touched
        List<ShapeDrawable> touchedShapes = null;
        return true;
    }
}
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53

1 Answers1

0

Based on what a canvas actually is, I do not believe that there is any way for it to intrinsically know the locations of shapes drawn onto it.

Keeping track of where shapes are drawn (with coords unfortunately), and checking that against the touch event might be the only option.

I say 'might', because I am not an expert in this area, but I have worked with the canvas a lot in the past.

That said, there are libs out there to simplify this. In one mapping application I worked on, I used this:

https://github.com/onlylemi/MapView

This lib has built in support for knowing where things (shapes) are on the map. That said, this lib uses coords and touch events underneath (as seen in their MarkLayer.java class), as do most you will find.

AzraelPwnz
  • 506
  • 2
  • 12