0

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°

enter image description here

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.

enter image description here

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Roland
  • 49
  • 4
  • why not to use drawBitmap with Matrix param? – pskink Apr 23 '14 at 07:45
  • I tried it with matrix param, but the result was not what I expected. I want to rotate the canvas and clip the bitmap to the canvas, so I don't have to actually rotate the bitmap. But as I mentioned the bitmap then flips for 180°. For me it looks like the bitmap has an orientation which specifies top and bottom. – Roland Apr 23 '14 at 10:06
  • post an image or two describing what you wanna do – pskink Apr 23 '14 at 10:22
  • I edited my question and added 2 screenshots. – Roland Apr 23 '14 at 11:40
  • see my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue to get an idea how simple you can do it using the Matrix – pskink Apr 23 '14 at 13:08

0 Answers0