【问题标题】:Android: Rotate image in imageview by an angleAndroid:将imageview中的图像旋转一个角度
【发布时间】:2012-02-17 09:43:37
【问题描述】:

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

【问题讨论】:

  • PS for 2014,看起来你可以在 Android Studio 的 XML 中简单地设置“旋转”。 (如果您不介意使用“文本”布局,您甚至可以单击右侧的“专家属性”按钮!)
  • 在这里找到答案。 stackoverflow.com/a/52983423/5872337

标签: android bitmap rotation imageview


【解决方案1】:

另一种旋转 ImageView 的简单方法:
更新:
所需的进口:

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

代码:(假设imageViewanglepivotX & pivotY 已定义)

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

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

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

【讨论】:

  • 为了完整起见,这里是基于ImageView 值的行:matrix.postRotate( 180f, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
  • 如何在每个触摸事件上旋转图像视图?
  • 对我来说,如果在 relativeLayout 中旋转 imageView 会增长到包含的图像的大小,不再适合布局。有没有人有解决这个问题的经验?
  • 注意:为此,您必须设置 ImageViewsrc 属性。 getDrawable() 为我返回 null,直到我意识到我设置了 ImageViewbackground 属性而不是 src掌心
  • 它只在 imageview 中旋转图像。我应该怎么做才能旋转图像本身?
【解决方案2】:

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

【讨论】:

  • 它还会将视图的宽度和高度更改为正确的大小吗?还是只改变它的外观?
  • 有没有办法在旋转时产生“旋转动画”?
  • @IvanMorgillo 可以看一下ViewPropertyAnimator,用法跟aView.animate().rotation(20).start()一样
  • @IvanMorgillo:使用rotationBy()。例如,view.animate().rotationBy(180f).start()。但是如果连续点击视图就会出现问题。
  • 我在按钮中使用此代码。第一次点击没问题,但下一次点击不再旋转。我应该换一条线来解决这个问题吗?
【解决方案3】:

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

android:rotation="90"

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

【讨论】:

  • 关于 xml 预览的提示对我帮助很大
  • 应用旋转后,XML 预览会正确显示给我。
  • 这将旋转视图而不是图像!这将在图像旁边添加空白区域。加个background就可以看到效果了。
【解决方案4】:

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

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 上使用RotateAnimation,并确保动画设置为fillAfter=trueduration=0fromDegrees=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);

【讨论】:

  • danx .. 但是动态地在视图上执行 RotateAnimation 是不合适的方法..我的意思是动态设置 d 角度..
  • 如果您需要旋转的图像在 ListView 中,这比接受的答案更好
【解决方案5】:

我知道这太晚了,但它对我很有帮助,所以它可能会帮助其他人。

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

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

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

【讨论】:

  • 我在旋转时的图像在页面上自然会高一点。怎么设置?
  • 您的图像是否正确居中?如果图像的透明度不均匀,旋转会导致图像在旋转时出现位置变化
  • 没有人,当图像旋转时它使用轴。想象一下你的手机在你手上的垂直位置,现在将它旋转到水平位置。它不再接触你的手。所以有一个空间......但我通过使用 setPivotX() 解决了它
【解决方案6】:

对于 Kotlin,

mImageView.rotation = 90f //angle in float

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

另外,虽然它是View 类中的一个方法。因此,您几乎可以使用它旋转任何视图。

【讨论】:

    【解决方案7】:

    这是我的implementation of 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 中。
    • 这个 imageView 在 gridView 中使用时会出现奇怪的问题。在您滚动之前,它一直不可见。
    【解决方案8】:

    在 android 中延迟旋转图像:

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

    【讨论】:

      【解决方案9】:

      也可以这样:-

      imageView.animate().rotation(180).start();
      

      got from here.

      【讨论】:

      • 谢谢!这非常简单快捷
      【解决方案10】:

      我对此有一个solution。 其实这是解决旋转后出现的问题(矩形图像不适合ImagView) 但它也涵盖了你的问题.. 虽然这个解决方案的动画是好是坏

          int h,w;
          Boolean safe=true;
      

      在初始化活动时无法获取 imageView 的参数 为此,请参阅此solution 在像这样的按钮的 onClick 处设置尺寸

          rotateButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(imageView.getRotation()/90%2==0){
                      h=imageView.getHeight();
                      w=imageView.getWidth();
      
                  }
              .
              .//Insert the code Snippet below here 
             }
      

      以及当我们要旋转 ImageView 时要运行的代码

      if(safe)     
      imageView.animate().rotationBy(90).scaleX(imageView.getRotation()/90%2==0?(w*1.0f/h):1).scaleY(imageView.getRotation()/90%2==0?(w*1.0f/h):1).setDuration(2000).setInterpolator(new LinearInterpolator()).setListener(new Animator.AnimatorListener() {
                      @Override
                      public void onAnimationStart(Animator animation) {
                            safe=false;
                      }
      
                      @Override
                      public void onAnimationEnd(Animator animation) {
                            safe=true;
      
                      }
      
                      @Override
                      public void onAnimationCancel(Animator animation) {
      
                      }
      
                      @Override
                      public void onAnimationRepeat(Animator animation) {
      
                      }
                  }).start();
              }
          });
      

      这个解决方案对于上面的问题已经足够了。虽然它会缩小imageView,即使它不是必需的(当高度小于宽度时)。如果它打扰你,你可以在scaleX / scaleY中添加另一个三元运算符。

      【讨论】:

        【解决方案11】:

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

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

        1f 值用于将 ImageView 恢复到正常状态:

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

        【讨论】:

          【解决方案12】:

          我认为最好的方法:)

          int angle = 0;
          imageView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      angle = angle + 90;
                      imageView.setRotation(angle);
                  }
              });
          

          【讨论】:

            【解决方案13】:

            你可以简单地使用ImageView的rotation属性

            以下是 ImageView 中的属性以及来自 Android 源代码的详细信息

            <!-- rotation of the view, in degrees. -->
            <attr name="rotation" format="float" />
            

            【讨论】:

              【解决方案14】:

              遗憾的是,我认为没有。 Matrix 类负责所有图像操作,无论是旋转、缩小/增长、倾斜等。

              http://developer.android.com/reference/android/graphics/Matrix.html

              我很抱歉,但我想不出替代方案。也许其他人可以做到,但我不得不使用矩阵来处理图像。

              祝你好运!

              【讨论】:

                【解决方案15】:

                在自定义视图上试试这个

                public class DrawView extends View {
                
                
                    public DrawView(Context context,AttributeSet attributeSet){
                        super(context, attributeSet);
                    }
                
                    @Override
                    public void onDraw(Canvas canvas) {
                        /*Canvas c=new Canvas(BitmapFactory.decodeResource(getResources(), R.drawable.new_minute1)    );
                
                        c.rotate(45);*/
                
                        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.new_minute1), 0, 0, null);
                        canvas.rotate(45);
                    }
                }
                

                【讨论】:

                  【解决方案16】:

                  这是为 imageView 放置旋转可绘制对象的一个​​很好的解决方案:

                  Drawable getRotateDrawable(final Bitmap b, final float angle) {
                      final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) {
                          @Override
                          public void draw(final Canvas canvas) {
                              canvas.save();
                              canvas.rotate(angle, b.getWidth() / 2, b.getHeight() / 2);
                              super.draw(canvas);
                              canvas.restore();
                          }
                      };
                      return drawable;
                  }
                  

                  用法:

                  Bitmap b=...
                  float angle=...
                  final Drawable rotatedDrawable = getRotateDrawable(b,angle);
                  root.setImageDrawable(rotatedDrawable);
                  

                  另一种选择:

                  private Drawable getRotateDrawable(final Drawable d, final float angle) {
                      final Drawable[] arD = { d };
                      return new LayerDrawable(arD) {
                          @Override
                          public void draw(final Canvas canvas) {
                              canvas.save();
                              canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2);
                              super.draw(canvas);
                              canvas.restore();
                          }
                      };
                  }
                  

                  另外,如果你想旋转位图,但又怕 OOM,你可以使用我制作的 NDK 解决方案here

                  【讨论】:

                    【解决方案17】:

                    如果您只想直观地旋转视图,您可以使用:

                    iv.setRotation(float)
                    

                    【讨论】:

                      【解决方案18】:

                      另一种可能的解决方案是创建自己的自定义图像视图(例如 RotateableImageView extends ImageView )...并覆盖 onDraw() 以在重新绘制到画布之前旋转画布/位图。不要忘记恢复画布背面。

                      但是,如果您只想旋转单个图像视图实例,您的解决方案应该足够好。

                      【讨论】:

                        【解决方案19】:

                        没有矩阵和动画:

                        {
                            img_view = (ImageView) findViewById(R.id.imageView);
                            rotate = new RotateAnimation(0 ,300);
                            rotate.setDuration(500);
                            img_view.startAnimation(rotate);
                        }
                        

                        【讨论】:

                          【解决方案20】:

                          把这个写在你的 onactivityResult 中

                                      Bitmap yourSelectedImage= BitmapFactory.decodeFile(filePath);
                                      Matrix mat = new Matrix();
                                      mat.postRotate((270)); //degree how much you rotate i rotate 270
                                      Bitmap bMapRotate=Bitmap.createBitmap(yourSelectedImage, 0,0,yourSelectedImage.getWidth(),yourSelectedImage.getHeight(), mat, true);
                                      image.setImageBitmap(bMapRotate);
                                      Drawable d=new BitmapDrawable(yourSelectedImage);
                                      image.setBackground(d); 
                          

                          【讨论】:

                            【解决方案21】:

                            试试这个代码 100% 工作;

                            在旋转按钮上点击写下这段代码:

                                    @Override
                                    public void onClick(View view) {
                                        if(bitmap==null){
                                            Toast.makeText(getApplicationContext(), "Image photo is not yet set", Toast.LENGTH_LONG).show();
                                        }
                                        else {
                                            Matrix matrix = new Matrix();
                                            ivImageProduct.setScaleType(ImageView.ScaleType.MATRIX);   //required
                                            matrix.postRotate(90,ivImageProduct.getDrawable().getBounds().width()/2,ivImageProduct.getDrawable().getBounds().height()/2);
                                            Bitmap bmp=Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                                            bitmap.recycle();
                                            bitmap=bmp;
                                            ivImageProduct.setImageBitmap(bitmap);
                                        }
                                    }
                            

                            【讨论】:

                              【解决方案22】:

                              而不是将图像转换为位图然后旋转它尝试像下面的代码一样旋转直接图像视图。

                              ImageView myImageView = (ImageView)findViewById(R.id.my_imageview);
                              
                              AnimationSet animSet = new AnimationSet(true);
                              animSet.setInterpolator(new DecelerateInterpolator());
                              animSet.setFillAfter(true);
                              animSet.setFillEnabled(true);
                              
                              final RotateAnimation animRotate = new RotateAnimation(0.0f, -90.0f,
                                  RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
                                  RotateAnimation.RELATIVE_TO_SELF, 0.5f);
                              
                              animRotate.setDuration(1500);
                              animRotate.setFillAfter(true);
                              animSet.addAnimation(animRotate);
                              
                              myImageView.startAnimation(animSet);
                              

                              【讨论】:

                                【解决方案23】:

                                按照以下答案进行图像视图的连续旋转

                                int i=0;
                                

                                如果点击了旋转按钮

                                imageView.setRotation(i+90);
                                i=i+90;
                                

                                【讨论】:

                                  【解决方案24】:

                                  如果你想将图像旋转 180 度然后将这两个值放在 imageview 标签中:-

                                  android:scaleX="-1"
                                  android:scaleY="-1"
                                  

                                  说明:- scaleX = 1 和 scaleY = 1 表示它是正常状态,但如果我们将 -1 放在 scaleX/scaleY 属性上,那么它将旋转 180 度

                                  【讨论】:

                                    【解决方案25】:
                                    Matrix matrix = new Matrix();
                                    imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
                                    matrix.postRotate((float) 20, imageView.getDrawable().getBounds().width()/2, imageView.getDrawable().getBounds().height()/2);
                                    imageView.setImageMatrix(matrix);
                                    

                                    怎么用?

                                    public class MainActivity extends AppCompatActivity {
                                       int view = R.layout.activity_main;
                                       TextView textChanger;
                                       ImageView imageView;
                                       @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                                       @Override
                                       protected void onCreate(Bundle savedInstanceState) {
                                          super.onCreate(savedInstanceState);
                                          setContentView(view);
                                          textChanger = findViewById(R.id.textChanger);
                                          imageView=findViewById(R.id.imageView);
                                          textChanger.setOnClickListener(new View.OnClickListener() {
                                             @Override
                                             public void onClick(View v) {
                                                roateImage(imageView);
                                             }
                                          });
                                       }
                                       private void roateImage(ImageView imageView) {
                                          Matrix matrix = new Matrix();
                                          imageView.setScaleType(ImageView.ScaleType.MATRIX); //required
                                          matrix.postRotate((float) 20, imageView.getDrawable().getBounds().width()/2,    imageView.getDrawable().getBounds().height()/2);
                                          imageView.setImageMatrix(matrix);
                                       }
                                    }
                                    

                                    【讨论】:

                                      猜你喜欢
                                      • 1970-01-01
                                      • 2013-09-15
                                      • 2016-10-19
                                      • 1970-01-01
                                      • 2021-04-12
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多