Android: Rotate image in imageview by an angle

前端 未结 25 2601
野趣味
野趣味 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:16

    I have a solution to this. Actually it is a solution to a problem that arises after rotation(Rectangular image doesn't fit ImagView) but it covers your problem too.. Although this Solution has Animation for better or for worse

        int h,w;
        Boolean safe=true;
    

    Getting the parameters of imageView is not possible at initialisation of activity To do so please refer to this solution OR set the dimensions at onClick of a Button Like this

        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 
           }
    

    And the code to be run when we want to rotate 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();
            }
        });
    

    This solution is sufficient for the Problem above.Although it will shrink the imageView even if it is not necessary(when height is smaller than Width).If it bothers you,you can add another ternary operator inside scaleX/scaleY.

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

    here's a nice solution for putting a rotated drawable for an 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;
    }
    

    usage:

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

    another alternative:

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

    also, if you wish to rotate the bitmap, but afraid of OOM, you can use an NDK solution i've made here

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

    if u want to rotate an image by 180 degrees then put these two value in imageview tag:-

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

    Explanation:- scaleX = 1 and scaleY = 1 repesent it's normal state but if we put -1 on scaleX/scaleY property then it will be rotated by 180 degrees

    0 讨论(0)
  • 2020-11-22 07:18
    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);
    

    how to use?

    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);
       }
    }
    
    0 讨论(0)
  • 2020-11-22 07:19

    Follow the below answer for continuous rotation of an imageview

    int i=0;
    

    If rotate button clicked

    imageView.setRotation(i+90);
    i=i+90;
    
    0 讨论(0)
  • 2020-11-22 07:20

    Rotate an image in android with delay:

    imgSplash.animate().rotationBy(360f).setDuration(3000).setInterpolator(new LinearInterpolator()).start();
    
    0 讨论(0)
提交回复
热议问题