Sunday, 1 January 2012

Android Image Rotation

Description:


Below method will rotate image to 45 degrees.

Note: Don't forget to create ImageView in your layout xml file with android:id="@+id/imageView1

     /**
     * Rotate image to 45 degrees.
     */

    public void RotateImage(){
        ImageView image;
        Bitmap bMap;
        Matrix matrix;
        
        //Get ImageView from layout xml file
        image = (ImageView) findViewById(R.id.imageView1);
        
        //Decode Image using Bitmap factory.
        bMap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        
        //Create object of new Matrix.
        matrix = new Matrix();
        
        //set image rotation value to 45 degrees in matrix.
        matrix.postRotate(45);

        //Create bitmap with new values.
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 00,
                bMap.getWidth(), bMap.getHeight(), matrix, true);
        
        //put rotated image in ImageView.
        image.setImageBitmap(bMapRotate);
    }
  

5 comments:

  1. Thanks! it is a great tutorial yet very simple!

    ReplyDelete
  2. pls help me to rotate an image continuously....but not an image view...for eg..a fan image.....pls help me....thanks in advance

    ReplyDelete
  3. Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), matrix, true); will cause OutOfMemoryError if the image is large enough(up to the java heap for each app on your phone and the camera's resolution).

    ReplyDelete
  4. Thank you so much, Your code is very useful for me.

    ReplyDelete