Rotate image onclick issue

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

Answers


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.


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


Need Your Help

Create a SQL query

mysql sql

I have this tables: Tables link