Touch listener on path android canvas

726 views Asked by At

My custom view contain several paths on canvas and need to listen touch event on same view but different paths. So how can I achieve this here is my piece of code:

All I need to listen path area touched 

Here is view class

public class Hexa extends View implements OnTouchListener {

int displayX;
int displayY;
Path path1;
Path path2;

public Hexa(Context context) {
    super(context);
    initializeDisplay();
    this.setOnTouchListener(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.parseColor("#1b1c20"));
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(Color.parseColor("#2a2f35"));
    // Path path ;
    Point a, b, c, d, e;

    // draw upper
    a = new Point(displayX * 5 / 100, 0);
    b = new Point(displayX * 95 / 100, 0);
    c = new Point(displayX * 5 / 100, displayY
            - ((int) displayY * (100 - 30) / 100));
    d = new Point(displayX * 95 / 100, displayY
            - ((int) displayY * (100 - 30) / 100));
    e = new Point(displayX / 2, displayY
            - ((int) displayY * (100 - 40) / 100));

    path1 = new Path();
    path1.setFillType(FillType.EVEN_ODD);
    path1.lineTo(a.x, a.y);
    path1.lineTo(b.x, b.y);
    path1.lineTo(d.x, d.y);
    path1.lineTo(e.x, e.y);
    path1.lineTo(c.x, c.y);
    path1.lineTo(a.x, a.y);
    path1.close();
    canvas.drawPath(path1, paint);

    // draw lower
    a = new Point(displayX * 5 / 100, displayY);
    b = new Point(displayX * 95 / 100, displayY);

    c = new Point(displayX * 5 / 100, displayY
            - ((int) displayY * (100 - 70) / 100));
    d = new Point(displayX * 95 / 100, displayY
            - ((int) displayY * (100 - 70) / 100));
    e = new Point(displayX / 2, displayY
            - ((int) displayY * (100 - 60) / 100));

    path2 = new Path();
    path2.setFillType(FillType.EVEN_ODD);
    path2.lineTo(a.x, a.y);
    path2.lineTo(b.x, b.y);
    path2.lineTo(d.x, d.y);
    path2.lineTo(e.x, e.y);
    path2.lineTo(c.x, c.y);
    path2.lineTo(a.x, a.y);
    path2.close();
    canvas.drawPath(path2, paint);

}

@Override
public boolean onTouch(View view, MotionEvent event) {

    int e=event.getAction();
    switch(e){
    case MotionEvent.ACTION_DOWN:
        //here i need touch listener
        if(area of path1){
            Log.d("PATH1", "AREA TOUCHED");
        }
        else if (area of path2){
            Log.d("PATH2", "AREA TOUCHED");
        }

        break;
    }



    return true;
 }
}

Is there is way to listen multiple listener on same view.

0

There are 0 answers