0

i'm still working on a piece of code, i want to rotate the image clockwise and counter clockwise using buttons, i tried the following code:

case (R.id.clock):
            matrix.postRotate(90);
            matrix.postRotate(getDegreesFromRadians(angle), mid.x, mid.y);

        break;

        case (R.id.anticlock):
            float degrees = 0;
            degrees = degrees-10;
            matrix.postRotate(degrees); 
            break;

it is working but not the proper way, first i've to press the button then click on imageview to rotate the image. Any help?

i asked the question here too Android Rotate image ontouch but now i want it using buttons

Community
  • 1
  • 1
Numair
  • 1,062
  • 1
  • 20
  • 41

2 Answers2

2

This is what I usually do. I create static Util class with this method:

public static Bitmap rotate(Bitmap bitmap, int degree) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix mtx = new Matrix();
    mtx.postRotate(degree);

  return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

and then, I use it like this:

case 21: // ROTATE LEFT
    ImageProcessActivity.processedBitmap = ImageUtil.rotate(ImageProcessActivity.processedBitmap, -90);
    d = new BitmapDrawable(ImageProcessActivity.processedBitmap);
    ImageProcessActivity.selectedImage.setImageDrawable(d);

break;
case 22: // ROTATE RIGHT
    ImageProcessActivity.processedBitmap = ImageUtil.rotate(ImageProcessActivity.processedBitmap, 90);
    d = new BitmapDrawable(ImageProcessActivity.processedBitmap);
    ImageProcessActivity.selectedImage.setImageDrawable(d);
break;

You see, in my code, I separate the image that is being displayed with the ImageView. This way, I can easily manipulate the image without the hasle of ImageView. ImageView, as it name explained, is only for viewing the image. Not the source for manipulation.

ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

You must notify that your view has changed, so that android renders it egain. Views are not frameRate rendered, so do like this after changing the matrix:

yourView.invalidate();

Should work

Ivan Seidel
  • 2,394
  • 5
  • 32
  • 49