java 如何在 Android 中旋转位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29982528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I rotate a bitmap in Android?
提问by Thanh Le Tran
I know there are threads on this question already but the solutions seem to use methods from the Matrix class that don't seem to work anymore. Even after imports the methods cannot be resolved. I'm basically trying to rotate a bitmap 90 degrees because it comes out sideways when I take a picture vertically. Here's my code for the activity:
我知道已经有关于这个问题的线索,但解决方案似乎使用了 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);
}
}
回答by Hitesh Singh
try this:
试试这个:
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);
}
Here please pass your bitmap or in angle what you want to show your bitmap like 90 180 etc.it will change bitmap screen using postRotate() method of class Matrix and again create bitmap and revert you
在这里,请传递您的位图或您想要显示位图的角度,例如 90 180 等。它将使用 Matrix 类的 postRotate() 方法更改位图屏幕,然后再次创建位图并恢复您
回答by MChaker
You can add a TextView to your layout and set the Bitmap to it
您可以将 TextView 添加到您的布局并将位图设置为它
ImageView yourView = (ImageView)findViewById(imageviewid);
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
yourView.setImageBitmap(bitmap);
Probably you can use RotateAnimation
on the View (ImageView set to Bitmap) you want to Rotate, and do not forget to set the Animation to fillAfter=true
and duration=0
.
可能您可以RotateAnimation
在要旋转的视图(ImageView 设置为位图)上使用,并且不要忘记将动画设置为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"
/>
Now all you need is to inflate the animation to your View
现在你所需要的就是将动画膨胀到你的视图
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
yourView.startAnimation(rotation);
Or you can simply do this yourView.setRotation(angle)
with API >= 11
.
或者您可以简单地yourView.setRotation(angle)
使用API >= 11
.
回答by Yau
This is the correct way to rotate bitmap :D
这是旋转位图的正确方法: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;
}