我正在使用下面的代码在ImageView中旋转一个角度的图像。有没有更简单、更简单的方法。
ImageView iv = (ImageView)findViewById(imageviewid);
TextView tv = (TextView)findViewById(txtViewsid);
Matrix mat = new Matrix();
Bitmap bMap = BitmapFactory.decodeResource(getResources(),imageid);
mat.postRotate(Integer.parseInt(degree));===>angle to be rotated
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,bMap.getWidth(),bMap.getHeight(), mat, true);
iv.setImageBitmap(bMapRotate);
发布于 2013-01-25 03:39:47
使用API>=11的mImageView.setRotation(angle)
发布于 2014-10-11 00:26:13
如果您支持API 11或更高版本,则只需使用以下XML属性:
android:rotation="90"
它可能无法在Android Studio xml预览中正确显示,但它可以正常工作。
发布于 2012-01-25 08:56:08
有两种方法可以做到这点:
1使用Matrix
创建新的位图:
imageView = (ImageView) findViewById(R.id.imageView);
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Matrix matrix = new Matrix();
matrix.postRotate(30);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
matrix, true);
imageView.setImageBitmap(rotated);
2在要旋转的View
上使用动画,并确保动画设置为fillAfter=true
、duration=0
和fromDegrees=toDgrees
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="50%"
android:pivotY="50%"
android:duration="0"
android:startOffset="0"
/>
并在代码中膨胀动画:
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
myView.startAnimation(rotation);
https://stackoverflow.com/questions/8981845
复制相似问题