0

我想在 Android 中将位图旋转 90 度。而且我不想获得新实例。有没有办法解决这个问题?我有一个想法:只需旋转位图的像素。但我做不到。

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

4

1 回答 1

1

这有效:

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);
    }
}
于 2012-06-05T08:46:27.363 回答