Java 如何在Android中旋转位图?

Java 如何在Android中旋转位图?,java,android,bitmap,Java,Android,Bitmap,我知道在这个问题上已经有线程了,但是解决方案似乎使用了Matrix类中的方法,这些方法似乎不再有效。即使在导入之后,也无法解析这些方法。我基本上是尝试将位图旋转90度,因为当我垂直拍照时,它会从侧面出来。以下是我的活动代码: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layo

我知道在这个问题上已经有线程了,但是解决方案似乎使用了Matrix类中的方法,这些方法似乎不再有效。即使在导入之后,也无法解析这些方法。我基本上是尝试将位图旋转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);



        }
    }
试试这个:

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()方法更改位图屏幕,然后再次创建位图并将其还原。

您可以向布局中添加文本视图并将位图设置为它

ImageView yourView = (ImageView)findViewById(imageviewid);
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
yourView.setImageBitmap(bitmap);
可能您可以在要旋转的视图(图像视图设置为位图)上使用
RotateAnimation
,并且不要忘记将动画设置为
fillAfter=true
duration=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"
/>

或者您也可以使用
API>=11
简单地在视图中执行此操作。设置旋转(角度)这是旋转位图的正确方法:D

公共位图旋转位图(位图原始、浮动度数){
矩阵=新矩阵();
矩阵。预旋转(度);
位图旋转位图=Bitmap.createBitmap(原始,0,0,原始.getWidth(),原始.getHeight(),矩阵,真);
原始的。回收();
返回旋转位图;
}

check@ThanhLeTran如果我的答案对你不起作用,你能批评一下吗?请你的答案解释一下为什么这个代码回答了这个问题?代码唯一的答案是,因为他们不教解决方案。
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
yourView.startAnimation(rotation);