chai
chai

Reputation: 13

matrix.postRotate(90) in image rotation

I can only rotate the image once and when I click on the button again, the image freeze and doesn't rotate. Please help me.

try{
    //Bitmap bMap;

    //Get ImageView from layout xml file
    img = (ImageView) findViewById(R.id.imageView01);

    //Decode Image using Bitmap factory.
    Bitmap bMap = BitmapFactory.decodeFile(selectedImagePath);

    //Create object of new Matrix.
    Matrix matrix = new Matrix();

    //set image rotation value to 90 degrees in matrix.
    matrix.postRotate(90);

    //Create bitmap with new values.
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, true);

    //put rotated image in ImageView.
    img.setImageBitmap(bMapRotate);

    Context context = getApplicationContext();
    CharSequence text = "Image Rotated" ;
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);

    toast.show();

}catch (Exception e) {       
    e.printStackTrace();
    displayExceptionMessage(e.getMessage());
}

Upvotes: 0

Views: 12752

Answers (1)

bakriOnFire
bakriOnFire

Reputation: 2681

Try this

try {
    //Bitmap bMap;

    //Get ImageView from layout xml file
    img = (ImageView) findViewById(R.id.imageView01);

    //Decode Image using Bitmap factory.
    //Bitmap bMap = BitmapFactory.decodeFile(selectedImagePath);
     Bitmap bMap = Bitmap.createBitmap(img.getDrawingCache());

    //Create object of new Matrix.
    Matrix matrix = new Matrix();

    //set image rotation value to 90 degrees in matrix.
    matrix.postRotate(90);
    matrix.postScale(0.5f, 0.5f);

    int newWidth = bMap.getWidth()/2;
    int newHeight = bMap.getHeight()/2;

    //Create bitmap with new values.
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);

    //put rotated image in ImageView.
    img.setImageBitmap(bMapRotate);

    Context context = getApplicationContext();
    CharSequence text = "Image Rotated" ;
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);

    toast.show();

} catch (Exception e) {       
    e.printStackTrace();
    displayExceptionMessage(e.getMessage());
}

Wht i've done in ur code is i've created a bitmap from the ImageView for rotation.

Hope this helps..

Upvotes: 2

Related Questions