Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate image in android

Tags:

android

I want rotate image in both the ways Clockwise as well as Anti clock wise. I had try but not rotate image both the way, so plz give me solution for my problem if you know..

Thanks in Advance.................///

like image 949
SAM Bhadani Avatar asked Mar 22 '11 10:03

SAM Bhadani


2 Answers

Use the code below

public class Rotateimage extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
          // or just load a resource from the res/drawable directory:
          Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flo);

          // find the width and height of the screen:
          Display d = getWindowManager().getDefaultDisplay();
          int x = d.getWidth();
          int y = d.getHeight();

          // get a reference to the ImageView component that will display the image:
          ImageView img1 = (ImageView)findViewById(R.id.imageView1);

          // scale it to fit the screen, x and y swapped because my image is wider than it is tall
          Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);

          // create a matrix object
          Matrix matrix = new Matrix();

          matrix.postRotate(45, 90, 180);

          // create a new bitmap from the original using the matrix to transform the result
          Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
                // display the rotated bitmap
          img1.setImageBitmap(rotatedBitmap);
}}
like image 182
Anu Avatar answered Sep 24 '22 07:09

Anu


Here is the base code to load a bitmap and rotate it left or right:

// Load a bitmap from a drawable, make sure this drawable exists in your project
Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),
        R.drawable.ic_launcher);

// Create two matrices that will be used to rotate the bitmap
Matrix rotateRight = new Matrix();
Matrix rotateLeft = new Matrix();

// Set the matrices with the desired rotation 90 or -90 degrees
rotateRight.preRotate(90);
rotateLeft.preRotate(-90);

// Create bitmaps based on the loaded bitmap 'sprite' and apply one of
// the rotation matrices
Bitmap rSprite = Bitmap.createBitmap(sprite, 0, 0,
        sprite.getWidth(), sprite.getHeight(), rotateRight, true);
Bitmap lSprite = Bitmap.createBitmap(sprite, 0, 0,
        sprite.getWidth(), sprite.getHeight(), rotateLeft, true);

Now go and use rSprite and lSprite.

Here is a full sample that actually draws the bitmaps to screen:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new drawView(this));
    }

    private class drawView extends View{
        public drawView(Context context){
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            // Load a bitmap from a drawable, make sure this drawable exists in your project
            Bitmap sprite = BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.ic_launcher);

            // Create two matrices that will be used to rotate the bitmap
            Matrix rotateRight = new Matrix();
            Matrix rotateLeft = new Matrix();

            // Set the matrices with the desired rotation 90 or -90 degrees
            rotateRight.preRotate(90);
            rotateLeft.preRotate(-90);

            // Create bitmaps based on the loaded bitmap 'sprite' and apply one of
            // the rotation matrices
            Bitmap rSprite = Bitmap.createBitmap(sprite, 0, 0,
                    sprite.getWidth(), sprite.getHeight(), rotateRight, true);
            Bitmap lSprite = Bitmap.createBitmap(sprite, 0, 0,
                    sprite.getWidth(), sprite.getHeight(), rotateLeft, true);

            //Draw the first unrotated sprite at the top left of the screen
            canvas.drawBitmap(sprite, 0, 0, null);

            //Draw the rotated right sprite on the 2nd row
            canvas.drawBitmap(rSprite, 0, sprite.getHeight() + 5, null);

            //Draw the rotated left sprite on the 3rd row
            canvas.drawBitmap(lSprite, 0, sprite.getHeight() * 2 + 5, null);
        }
    }
}
like image 29
Error 454 Avatar answered Sep 24 '22 07:09

Error 454