1

I have a Canvas Text is drawn using drawText Method with Rotation of -45 degree, what i need is a touch action for that Text part alone, the ref image is below, enter image description here

My Code is below

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    Paint newpaint=new Paint();

    newpaint.setColor(Color.RED);

    canvas.drawLine(10,10,getMeasuredWidth(),10,newpaint);

    if(lines.size()>0)
    {
        Paint BluePaint=new Paint();

        BluePaint.setColor(Color.BLUE);

        BluePaint.setStrokeWidth(1.5f);

        for (Line l : lines)
        {
            canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY+50, BluePaint);
            canvas.save();
            canvas.rotate((float) (-45), l.startX, l.stopY+160);
            canvas.drawText("Add Location",l.startX-10,l.stopY+90,BluePaint);
            canvas.restore();

        }
    }

}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    float x = event.getX();
    float y = event.getY();
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            //Check if the x and y position of the touch is inside the bitmap
            if( x > bitmapXPosition && x < bitmapXPosition + getMeasuredWidth() && y > bitmapYPosition && y < bitmapYPosition + 5 )
            {
                lines.add(new Line(event.getX(), event.getY()));

                invalidate();

                return true;
            }
    }
    return false;

}

class Line {
    float startX, startY, stopX, stopY;
    public Line(float startX, float startY, float stopX, float stopY) {
        this.startX = startX;
        this.startY = startY;
        this.stopX = stopX;
        this.stopY = stopY;
    }
    public Line(float startX, float startY) { // for convenience
        this(startX, startY, startX, startY);
    }
}

I have detected the Line Touch event but, cannot be able to detect the touch event of the Text. Help to solve this.

Brendon
  • 1,368
  • 1
  • 12
  • 28

1 Answers1

0

First you need to apply the Affine transformation to the event.getX(),even.getY() point. Then check if the rectangle around the text contains this new transformed point as if it was not rotated.

Translating a point around some center: Java: Rotate Point around another by specified degree value

Community
  • 1
  • 1
Timur
  • 127
  • 1
  • 9