0

I want to rotate the bitmap by 90 degrees in Android. And I don't want to get a new instance. Is there any way to resolve this? I have an idea: just rotate the pixels of the bitmap. But I can't do it.

temp = Bitmap.createBitmap(temp, 0, 0, w, h, matrix, false);

dda
  • 6,030
  • 2
  • 25
  • 34
jhondge
  • 2,352
  • 1
  • 17
  • 13

1 Answers1

1

This works:

http://warting.github.com/AndroidBitmapRotate/

public class RotateBitmapActivity extends Activity {

    ImageView iv;
    private Bitmap bitmap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        iv = (ImageView) findViewById(R.id.ImageView01);

        findViewById(R.id.left).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rotate(-90F);
            }
        });

        findViewById(R.id.right).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rotate(90F);
            }
        });
    }

    private void rotate(float degrees) {

        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postRotate(degrees);

        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, true);
        iv.setImageBitmap(bitmap);
    }
}
Wärting
  • 1,086
  • 1
  • 12
  • 19
  • Thanks for you answer.But I want to get the bitmap just the same instance .Your resolution will have two diffrent instance.I can't hold the imageview refrence. – jhondge Jun 05 '12 at 08:57
  • I dont know why that is important, would it work if you create a custom class that holds the bitmap and keep the reference to the class instead? – Wärting Jun 05 '12 at 12:39