Android: Rotate image in imageview by an angle

前端 未结 25 2610
野趣味
野趣味 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: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);
       }
    }
    

提交回复
热议问题