如何在Android中旋转位图?

15

我知道这个问题已经有了讨论,但解决方案似乎使用了矩阵类的方法,而这些方法现在似乎不再起作用。即使导入了这些方法,也无法解决问题。我基本上是想将一幅位图旋转90度,因为当我垂直拍摄照片时,它会侧着出来。以下是我的活动代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        //Check that request code matches ours:
        if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE)
        {
            //Get our saved file into a bitmap object:
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);

            Intent intent = new Intent(this, EditActivity.class);

            ByteArrayOutputStream bs = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
            intent.putExtra("byteArray", bs.toByteArray());

            startActivity(intent);



        }
    }

1
请查看以下链接:https://blahti.wordpress.com/2014/02/03/android-rotate-scale-bitmap/ - Jaiprakash Soni
3个回答

23

试试这个:

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

请在此处传递您的位图或您希望展示位图的角度,如90、180等。它将使用类Matrix的postRotate()方法更改位图屏幕并再次创建位图并返回给您。


1
请您编辑一下您的答案,解释一下为什么这段代码可以回答问题。仅有代码的答案是不被鼓励的,因为它们无法教授解决方案。 - DavidPostill

2
这是旋转位图的正确方法 :D
public Bitmap rotateBitmap(Bitmap original, float degrees) {
        Matrix matrix = new Matrix();
        matrix.preRotate(degrees);
        Bitmap rotatedBitmap = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);
        original.recycle();
        return rotatedBitmap;
    }

0
你可以在布局中添加一个TextView,并将Bitmap设置给它。
ImageView yourView = (ImageView)findViewById(imageviewid);
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
yourView.setImageBitmap(bitmap);

你可以在想要旋转的视图上(将ImageView设置为Bitmap)使用RotateAnimation,并且不要忘记将动画设置为fillAfter=trueduration=0

<?xml version="1.0" encoding="utf-8"?>
<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="90"
  android:toDegrees="180"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="0"
  android:startOffset="0"
/>

现在你所需要做的就是将动画填充到你的视图中。
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
yourView.startAnimation(rotation);

或者你可以使用 API >= 11,并执行 yourView.setRotation(angle)


@ThanhLeTran,如果我的答案对你没有用,你能否批评一下? - MChaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接