Android: Rotate image in imageview by an angle

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

    There are two ways to do that:

    1 Using Matrix to create a new bitmap:

    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 use RotateAnimation on the View you want to Rotate, and make sure the Animation set to fillAfter=true, duration=0, and fromDegrees=toDgrees

     
    
    

    and Inflate the Animation in code:

    Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
    myView.startAnimation(rotation);
    

提交回复
热议问题