Android: Rotate image in imageview by an angle

前端 未结 25 2600
野趣味
野趣味 2020-11-22 06:35

I am using the following code to rotate a image in ImageView by an angle. Is there any simpler and less complex method available.

ImageView iv = (ImageView)f         


        
相关标签:
25条回答
  • 2020-11-22 07:07

    This is my implementation of RotatableImageView. Usage is very easy: just copy attrs.xml and RotatableImageView.java into your project and add RotatableImageView to your layout. Set desired rotation angle using example:angle parameter.

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

    If you have some problems with displaying image, try change code in RotatableImageView.onDraw() method or use draw() method instead.

    0 讨论(0)
  • 2020-11-22 07:10

    mImageView.setRotation(angle) with API>=11

    0 讨论(0)
  • 2020-11-22 07:11

    Another possible solution is to create your own custom Image view(say RotateableImageView extends ImageView )...and override the onDraw() to rotate either the canvas/bitmaps before redering on to the canvas.Don't forget to restore the canvas back.

    But if you are going to rotate only a single instance of image view,your solution should be good enough.

    0 讨论(0)
  • 2020-11-22 07:13

    You can simply use rotation atribute of ImageView

    Below is the attribute from ImageView with details from Android source

    <!-- rotation of the view, in degrees. -->
    <attr name="rotation" format="float" />
    
    0 讨论(0)
  • 2020-11-22 07:14

    Sadly, I don't think there is. The Matrix class is responsible for all image manipulations, whether it's rotating, shrinking/growing, skewing, etc.

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

    My apologies, but I can't think of an alternative. Maybe someone else might be able to, but the times I've had to manipulate an image I've used a Matrix.

    Best of luck!

    0 讨论(0)
  • 2020-11-22 07:15

    Try this code 100% working;

    On rotate button click write this code:

            @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);
                }
            }
    
    0 讨论(0)
提交回复
热议问题