Android:通过角度旋转ImageView中的图像

190

我正在使用以下代码旋转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);

6
对于2014年的PS版本,在Android Studio中,似乎可以直接在XML中设置“rotation”属性。(如果你不想使用“Text”布局,甚至可以直接点击右侧的“高级属性”按钮!) - Fattie
在这里找到答案。 - Geet Thakur
28个回答

221

mImageView.setRotation(angle) 适用于API>=11


3
它是否还会将视图的宽度和高度更改为正确的大小?还是只改变外观而不更改尺寸? - android developer
5
旋转时是否有“旋转动画”的方法? - Ivan Morgillo
16
您可以查看ViewPropertyAnimator,它的使用类似于aView.animate().rotation(20).start()。我会将其翻译为“您可以查看ViewPropertyAnimator,它的用法类似于aView.animate().rotation(20).start()。” - Jokester
7
使用 rotationBy()。例如,view.animate().rotationBy(180f).start()。但如果视图被连续点击,则会出现问题。 - Mangesh
我正在按钮中使用此代码。第一次点击可以,但是下一次点击就不再旋转了。 我应该加入其他代码来修复这个问题吗? - Eggakin Baconwalker
显示剩余2条评论

207

旋转 ImageView 的另一种简单方法:
更新:
必需的导入:

import android.graphics.Matrix;
import android.widget.ImageView;

代码:(假设imageViewanglepivotXpivotY已经定义)

Matrix matrix = new Matrix();
imageView.setScaleType(ImageView.ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivotX, pivotY);
imageView.setImageMatrix(matrix);

这个方法不需要每次创建一个新的位图。

注意:要在运行时通过ontouch旋转一个ImageView,你可以在ImageView上设置onTouchListener,并在触摸监听器的ACTION_MOVE部分添加最后两行代码(即postRotate矩阵和将其设置在imageView上)来旋转它。


114
为了完整起见,这是基于ImageView的值的代码行:matrix.postRotate( 180f, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);。 (翻译已完成,不回答除翻译外的任何问题或提供其他信息) - Stefan Hoth
2
如何在每个触摸事件上旋转ImageView? - Saurabh
15
如果在RelativeLayout中旋转ImageView,它会增长到所包含图像的大小,不再适合布局。有没有人有解决方法的经验? - Makibo
8
请注意:要使其工作,您需要设置ImageViewsrc属性。 直到我意识到我设置了ImageView的背景属性而不是src属性之前,我的getDrawable()方法一直返回null。扇自己一个耳光 - Gary Sheppard
1
它只在ImageView中旋转图像。我应该怎么做才能旋转图像本身? - Ege
显示剩余10条评论

107

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

android:rotation="90"

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


3
关于XML预览的提示对我帮助很大。 - ngu
旋转应用后,XML预览正常显示。 - Dick Lucas
这将旋转视图但不旋转图像!这将在图像旁添加空白区域。只需添加一个“background”即可查看效果。 - YBrush
https://stackoverflow.com/a/77402429/19856629 - undefined

47

有两种方法可以实现:

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);

使用RotateAnimation在你想要旋转的View上进行旋转,确保动画设置为fillAfter=trueduration=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);

但是有没有办法在视图上动态地执行RotateAnimation?我的意思是动态设置角度。 - rijinrv
如果您需要旋转的图像位于ListView中,则此方法比已接受的答案更有效。 - Daren
@rijinrv 动画 rotateAnimation = new RotateAnimation(currentRotation, currentRotation -= 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(100); rotateAnimation.setFillAfter(true); photoView.startAnimation(rotateAnimation); - undefined

11

我知道这个回答可能有些晚了,但对我很有帮助,希望也能对其他人有所帮助。

从API 11开始,你可以使用imageView.setRotation(angleInDegrees);方法在代码中编程设置ImageView的绝对旋转角度。

所谓绝对旋转角度是指,你可以反复调用该函数而无需跟踪当前的旋转角度。也就是说,如果我通过向setRotation()方法传递15F来旋转图像,然后再次使用setRotation()方法传递30F,那么图片的旋转角度将是30度,而不是45度。

注意: 这实际上适用于View对象的任何子类,并非仅限于ImageView。


我的旋转图像在页面上自然地变高了一点。我该如何将它下移? - I Wanna Know
你的图像是否居中?如果你的图像透明度不均匀,旋转时会导致它在旋转时似乎改变位置。 - thomaspsk
不是这样的,当图像旋转时它会使用轴。想象一下你手上竖直放置的手机,现在将其旋转到水平位置。它不再接触你的手了。所以有一个空间...但我通过使用setPivotX()方法解决了这个问题。 - I Wanna Know

10

对于 Kotlin,

mImageView.rotation = 90f //angle in float

这将旋转imageView而不是旋转图像本身。

另外,虽然它是View类中的一个方法。但你可以使用它轻松旋转任何视图。


7
也可以这样做:-
imageView.animate().rotation(180).start();

got from here.


谢谢!这非常简单和快捷。 - zinonX
我们能设置一个锚点吗?我计划不提供动画,而是使用锚点来实现旋转的拖拽效果,如果可能的话?@Debasish - gumuruh

7

在Android上延迟旋转图像:

imgSplash.animate().rotationBy(360f).setDuration(3000).setInterpolator(new LinearInterpolator()).start();

5
这是我旋转图片视图(RotatableImageView)的实现。使用非常简单:只需复制attrs.xmlRotatableImageView.java到您的项目中,并将 RotatableImageView 添加到您的布局中。使用 example:angle 参数设置所需的旋转角度。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:example="http://schemas.android.com/apk/res/com.example"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.example.views.RotatableImageView
        android:id="@+id/layout_example_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_layout_arrow"
        example:angle="180" />
</FrameLayout>

如果您在显示图像时遇到问题,请尝试更改 RotatableImageView.onDraw() 方法中的代码或使用draw()方法。

在使用RotatableImageView时,我遇到了NullPointerException。protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int w=getDrawable().getIntrinsicWidth(); ... }顺便说一句,我是在代码中使用它的(并且有一个默认的给定图像),而不是在xml中。 - RRTW
这个imageView在gridView中使用时有奇怪的问题。它会一直保持不可见,直到你滚动。 - android developer

4

此外,如果您想将ImageView垂直或水平旋转180度,可以使用scaleYscaleX属性,并将其设置为-1f。以下是一个Kotlin示例:

imageView.scaleY = -1f
imageView.scaleX = -1f

1f 值用于将 ImageView 返回到其正常状态:

imageView.scaleY = 1f
imageView.scaleX = 1f

如何旋转90度? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接