Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 951
Prasoon Shrivastav Avatar asked Sep 11 '13 07:09

Prasoon Shrivastav


2 Answers

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);
}
like image 104
eluleci Avatar answered Nov 01 '22 11:11

eluleci


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.

like image 43
Sandokan El Cojo Avatar answered Nov 01 '22 13:11

Sandokan El Cojo