首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:按一定角度旋转imageview中的图片

Android:按一定角度旋转imageview中的图片
EN

Stack Overflow用户
提问于 2012-01-24 12:00:00
回答 18查看 343.4K关注 0票数 174

我正在使用下面的代码在ImageView中旋转一个角度的图像。有没有更简单、更简单的方法。

代码语言:javascript
复制
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);
EN

回答 18

Stack Overflow用户

发布于 2013-01-25 03:39:47

使用API>=11的mImageView.setRotation(angle)

票数 203
EN

Stack Overflow用户

发布于 2014-10-11 00:26:13

如果您支持API 11或更高版本,则只需使用以下XML属性:

代码语言:javascript
复制
android:rotation="90"

它可能无法在Android Studio xml预览中正确显示,但它可以正常工作。

票数 89
EN

Stack Overflow用户

发布于 2012-01-25 08:56:08

有两种方法可以做到这点:

1使用Matrix创建新的位图:

代码语言:javascript
复制
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=trueduration=0fromDegrees=toDgrees

代码语言:javascript
复制
 <?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"
/>

并在代码中膨胀动画:

代码语言:javascript
复制
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
myView.startAnimation(rotation);
票数 44
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8981845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档