【问题标题】:How do I rotate a bitmap in Android?如何在 Android 中旋转位图?
【发布时间】:2015-07-11 00:41:41
【问题描述】:

我知道这个问题已经有了一些线索,但解决方案似乎使用了 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);



        }
    }

【问题讨论】:

标签: java android bitmap


【解决方案1】:

试试这个:

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() 方法更改位图屏幕并再次创建位图并还原您

【讨论】:

  • 能否请您edit 解释一下为什么这段代码回答了这个问题?纯代码答案是 discouraged,因为它们不教授解决方案。
【解决方案2】:

您可以在布局中添加一个 TextView 并为其设置位图

ImageView yourView = (ImageView)findViewById(imageviewid);
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
yourView.setImageBitmap(bitmap);

大概你可以在你想Rotate的View(ImageView设置为Bitmap)上使用RotateAnimation,别忘了把Animation设置为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);

或者您可以简单地使用yourView.setRotation(angle)API &gt;= 11 执行此操作。

【讨论】:

  • @ThanhLeTran 如果我的回答不适合你,你能批评一下吗?
【解决方案3】:

这是旋转位图的正确方法: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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多