I'm currently working on an Android app with a Drawing Surface where I want to draw a bitmap onto. I use a Canvas to show the bitmap to the user before it is drawn to the drawing surface.
My problem is following: I am trying to implement a rotation with a handleMove function which is called from onTouch(). following code calculates the rotation angle:
private void rotate(float deltaX, float deltaY) {
if (mDrawingBitmap == null) {
return;
}
PointF currentPoint = new PointF(deltaX + mPreviousEventCoordinate.x,
deltaY + mPreviousEventCoordinate.y);
double previousXLength = mPreviousEventCoordinate.x - mToolPosition.x;
double currentXLength = currentPoint.x - mToolPosition.x;
double previousYLength = mPreviousEventCoordinate.y - mToolPosition.y;
double currentYLength = currentPoint.y - mToolPosition.y;
double deltaAngle = Math.atan2(currentXLength / previousYLength, currentYLength / currentYLength);
mBoxRotation = (float) (deltaAngle * 180 / Math.PI);
}
Here deltaX and deltaY are the movement distance how far the user moves the finger on the screen. mToolPosition is the center of the canvas.
private void drawBitmap(Canvas canvas) {
Paint bitmapPaint = new Paint(Paint.DITHER_FLAG);
canvas.save();
canvas.clipRect(new RectF(-mBoxWidth / 2, -mBoxHeight / 2,
mBoxWidth / 2, mBoxHeight / 2), Op.UNION);
canvas.drawBitmap(mDrawingBitmap, null, new RectF(-mBoxWidth / 2, -mBoxHeight / 2,
mBoxWidth / 2, mBoxHeight / 2), bitmapPaint);
canvas.restore();
}
This code is used to draw the bitmap onto the rotated canvas. just before drawBitmap(canvas) is called i use canvas.rotate(mBoxRotation)
to rotate the canvas.
My problem is that if the canvas is rotated so that the top left point of the canvas has a bigger y-coordinate the center of the bitmap, the bitmap flips for 180°. This means, that it cannot be rotated continuously for 360° Is there a chance to ignore the orientation of the bitmap so that it does not flip?
Edit:
This image shows the bitmap rotated to the right for about 100°
And this happens if I rotate it more to the right. so at about 130° the image flips because top is bottom and bottom is top now.