Android: Rotate image in imageview by an angle

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

    If you only want to rotate the view visually you can use:

    iv.setRotation(float)
    
    0 讨论(0)
  • 2020-11-22 07:21

    I think the best method :)

    int angle = 0;
    imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                angle = angle + 90;
                imageView.setRotation(angle);
            }
        });
    
    0 讨论(0)
  • 2020-11-22 07:22

    try this on a custom view

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

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

    Another simple way to rotate an ImageView:
    UPDATE:
    Required imports:

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

    Code: (Assuming imageView, angle, pivotX & pivotY are already defined)

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

    This method does not require creating a new bitmap each time.

    NOTE: To rotate an ImageView on ontouch at runtime you can set onTouchListener on ImageView & rotate it by adding last two lines(i.e. postRotate matrix & set it on imageView) in above code section in your touch listener ACTION_MOVE part.

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

    just write this in your 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); 
    
    0 讨论(0)
  • 2020-11-22 07:25

    without matrix and animated:

    {
        img_view = (ImageView) findViewById(R.id.imageView);
        rotate = new RotateAnimation(0 ,300);
        rotate.setDuration(500);
        img_view.startAnimation(rotate);
    }
    
    0 讨论(0)
提交回复
热议问题