Android:以一定角度在imageview中旋转图像

rij*_*nrv 147 android bitmap rotation imageview

我使用以下代码在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);
Run Code Online (Sandbox Code Playgroud)

Aks*_*Aks 188

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

import android.graphics.Matrix;
import android.widget.ImageView;
Run Code Online (Sandbox Code Playgroud)

代码:(假设imageView,angle,pivotXpivotY已经定义)

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

此方法不需要每次都创建新的位图.

注意:要旋转ImageViewontouch在运行时可以设置onTouchListenerImageView并旋转它通过添加最后两行(即postRotate矩阵设置上的ImageView)在上面的代码段在您触摸听众ACTION_MOVE一部分.

  • 为了完整性,这里是基于`ImageView`的值的行:`matrix.postRotate(180f,imageView.getDrawable().getBounds().width()/ 2,imageView.getDrawable().getBounds().height ()/ 2);` (105认同)
  • 对我来说,如果在relativeLayout中旋转,则imageView会增长到所包含图像的大小,不再适合布局.有没有人有经验如何解决这个问题? (14认同)
  • 注意:要使其工作,您必须设置`ImageView`的`src`属性.`getDrawable()`为我返回null,直到我意识到我已经设置了`ImageView`的`background属性而不是`src`.*捂脸* (7认同)
  • 如何在每次触摸事件中旋转imageview? (2认同)

Fro*_*lik 170

mImageView.setRotation(angle) API> = 11

  • @IvanMorgillo你可以看一下`ViewPropertyAnimator`,它的用法就像`aView.animate().rotation(20).start()` (13认同)
  • 有没有办法在旋转时有一个"旋转动画"? (5认同)
  • @IvanMorgillo:使用`rotationBy()`.例如,`view.animate().rotationBy(180f).start()`.但是,如果连续点击视图,则会出现问题. (5认同)
  • 它还会将视图的宽度和高度更改为正确的大小吗?或者它只改变它的外观? (3认同)

Ole*_*siy 64

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

android:rotation="90"
Run Code Online (Sandbox Code Playgroud)

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

  • 有关xml预览的提示帮助了我很多 (3认同)

Rot*_*miz 42

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

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);
Run Code Online (Sandbox Code Playgroud)

2使用RotateAnimationView要旋转,并确保动画设置为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"
/>
Run Code Online (Sandbox Code Playgroud)

并在代码中膨胀动画:

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
myView.startAnimation(rotation);
Run Code Online (Sandbox Code Playgroud)


tho*_*psk 8

我知道这是非常晚,但它对我有帮助,所以它可以帮助别人.

从API 11开始,您可以使用该imageView.setRotation(angleInDegrees);方法以编程方式设置ImageView的绝对旋转.

绝对地,我的意思是你可以反复调用这个函数,而不必跟踪当前的旋转.意思是,如果我通过传递15FsetRotation()方法旋转,然后setRotation()再次调用30F,则图像的旋转为30度,而不是45度.

注意:这实际上适用于View对象的任何子类,而不仅仅是ImageView.


Azi*_*ziz 8

对于科特林,

mImageView.rotation = 90f //angle in float
Run Code Online (Sandbox Code Playgroud)

这将旋转 imageView 而不是旋转图像

另外,虽然它是View课堂上的一个方法。所以你几乎可以使用它旋转任何视图。


Deb*_*osh 7

也可以这样完成:-

imageView.animate().rotation(180).start();
Run Code Online (Sandbox Code Playgroud)

从这里来的。


小智 7

在android中延迟旋转图像:

imgSplash.animate().rotationBy(360f).setDuration(3000).setInterpolator(new LinearInterpolator()).start();
Run Code Online (Sandbox Code Playgroud)


pet*_*ejl 5

这是我对RotatableImageView的实现.用法很简单:只要复制attrs.xmlRotatableImageView.java到您的项目,并添加RotatableImageView到您的布局.使用示例:角度参数设置所需的旋转角度.

<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>
Run Code Online (Sandbox Code Playgroud)

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