Question

I've written a custom view that displays text in the center of the screen as seen below. enter image description here

I've also made it so that if you touch the green box you can rotate and scale the text as seen below. enter image description here

Heres the problem. Whenever I rotate the text and let go, then try to rotate again it cant detect that the rect is being touched using myRect.contains(X,Y). After some time I found that after its rotated, and touch where the original green box was, it allows me to rotate again.

OnTouchEvent is obviously calculating the Rects position correctly since it drawing in the correct location. I just can figure out why the touch coordinates seem to be referencing old positions.

Here's my onDraw() method.

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

    canvas.save();
    canvas.rotate((float)mAngle, mBorderRect.centerX(), mBorderRect.centerY());
    canvas.drawRect(mBorderRect, mTextBorderPaint);
    canvas.drawText(mText, mBorderRect.left, mBorderRect.bottom, mTextPaint);
    canvas.drawRect(mResizeRect, mBGPaint);
    canvas.restore();

}
Was it helpful?

Solution

Remember that you're rotating the canvas, not the Rect. To get this to work you'll have to apply the inverse rotation on the touch position first, then compute myRect.contains(X,Y).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top