Android: Rotate image in ImageView by 90degrees but without delay

二次信任 提交于 2019-12-03 19:36:35

问题


I am developing a game where a user needs to tap on the image in ImageView to rotate it. On each tap image rotates by 90 degrees in clockwise direction. But image is taking time to rotate from old to new position. This is hampering the gameplay experience. I have used the following:

protected void onCreate(Bundle savedInstanceState)
{  
... 
...  
    imgview = (ImageView)findViewById(R.id.imageView1);  
    imgview.setOnClickListener(new OnClickListener() {
         @Override 
         public void onClick(View arg0) {
              Matrix matrix = new Matrix();
              matrix.postRotate(90); 
              Bitmap myImg = getBitmapFromDrawable(imgview.getDrawable());
              Bitmap rotated = Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),matrix,true);
              imgview.setImageBitmap(rotated);
         }
});

I want to know is there any other way to rotate the image without creating any delay in rotation.


回答1:


I also tried to do it once and couldn't find any other solution but using animation. Here how I'd do.

private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    imgview.startAnimation(rotateAnim);
}



回答2:


You don't need to rotate the object, rotating the view should be enough. If you are aiming API>=11 you can always do this.

mImageView.setRotation(angle);

Cheers.




回答3:


Short method

View.animate().rotation(90).setDuration(0);




回答4:


did you try to use a custom view extending imageview and rotating the image in a background thread?



来源:https://stackoverflow.com/questions/18735280/android-rotate-image-in-imageview-by-90degrees-but-without-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!