8

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.

Anas Azeem
  • 2,820
  • 3
  • 24
  • 37
Prasoon Shrivastav
  • 157
  • 1
  • 2
  • 15
  • 1
    You can try this: [ANDROID::Rotate image in imageview by an angle][1] [1]: http://stackoverflow.com/questions/8981845/androidrotate-image-in-imageview-by-an-angle – E Player Plus Oct 08 '13 at 15:16

4 Answers4

37

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);
}
eluleci
  • 3,489
  • 1
  • 26
  • 24
  • 1
    No, you can put this inside the onclick method directly. You don't need to do anything else. imgview.startAnimation(rotateAnim); directly rotates the image – eluleci Sep 11 '13 at 09:11
  • Ok, now I am rotating the image by 90, 180, 270 and 360 degrees using degree = degree + 90 . But the image is first going back to its original position than rotating to the given deree – Prasoon Shrivastav Sep 11 '13 at 09:31
  • 2
    For that (I am not sure) you can set the from-degree of the animation. new RotateAnimation(0.0f, degree, ... The first two parameters are the degrees "from" and "to". If you know that what degree that you are rotating from you can set the first param with that value. That may solve this problem. – eluleci Sep 11 '13 at 09:41
  • 1
    Thanks, it worked, I used rotate(float fromDegree,float toDegree){...} and stored fromDegree = toDegree and toDegree = toDegree + 90; – Prasoon Shrivastav Sep 11 '13 at 10:04
18

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.

Sandokan El Cojo
  • 796
  • 1
  • 9
  • 22
  • if this is clicked again, the rotate button, it doesn't work. That is, two clicks doesn't rotate the view by 180. Instead keeps still at 90. Is there a way to accomplish multiple rotations on button click ? – Krithi07 Apr 09 '15 at 11:03
  • 1
    @KritikaJalan For this you simply need to have a counter that counts the number of clicks. `onClick(){ counter++; imageView.setRotation(counter*90)};` – Barthy Apr 23 '15 at 21:48
  • Yeah, I did it instead using: img.setRotation(img.getRotation()- 90f); Thanks, @BarthyB. – Krithi07 Apr 24 '15 at 19:20
5

Short method

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

Codelaby
  • 2,604
  • 1
  • 25
  • 25
-3

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

MineConsulting SRL
  • 2,340
  • 2
  • 17
  • 32
  • please give an example for doing that, the image should be roatated on user's tap only – Prasoon Shrivastav Sep 11 '13 at 07:51
  • As such, this is rather a comment than an answer. I know you don't have the rep yet for commenting though. – laalto Sep 11 '13 at 08:07
  • @laalto yes i know i apologize for that. Anyway you should create a rotatio method in your custom view with a degree parameter and then from such method you launch an asynctask which do something like this: 1- rotates the bitmap of 1 or more degrees 2- set the bitmap as imageview resource 3- invalidate the view. Do this all thing until you reach the value of degrees passed in this method. In this way you should have an animation without delays and you can even decide the duration of the animation and the smoothness – MineConsulting SRL Sep 11 '13 at 08:57