[英]How do I rotate a bitmap in Android?
我知道這個問題上已經存在線程,但解決方案似乎使用了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()方法更改位圖屏幕並再次創建位圖並還原你
您可以將TextView添加到布局中並將Bitmap設置為它
ImageView yourView = (ImageView)findViewById(imageviewid);
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
yourView.setImageBitmap(bitmap);
可能您可以在要旋轉的視圖(ImageView設置為位圖)上使用RotateAnimation
,並且不要忘記將Animation設置為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"
/>
現在您只需要將動畫擴展到View
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
yourView.startAnimation(rotation);
或者你可以簡單地用API >= 11
來做你的yourView.setRotation(angle)
。
這是旋轉位圖的正確方法: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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.