13

This is java code.I am getting image from image gallery.I have one Button and one ImageView. It is rotating only one time.When I again click button it is not rotating image.

public class EditActivity extends ActionBarActivity
{
private Button rotate;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);
    rotate=(Button)findViewById(R.id.btn_rotate1);
    imageView = (ImageView) findViewById(R.id.selectedImage);
    String path = getIntent().getExtras().getString("path");
    final Bitmap bitmap = BitmapFactory.decodeFile(path);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 510, 500,
            false));
    rotate.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
          {                  
            imageView.setRotation(90);


        }
    });



}
user2095748
  • 267
  • 1
  • 5
  • 15
  • possible duplicate of [ANDROID::Rotate image in imageview by an angle](http://stackoverflow.com/questions/8981845/androidrotate-image-in-imageview-by-an-angle) – Dhinakaran Thennarasu Feb 01 '15 at 04:45
  • I'd just like to bring to notice that this question requires successive rotations that then complete a full circle. The proposed duplicate on the other hand will rotate by a quadrant and keep repeating the same over and over again. – Ravi K Thapliyal Jul 08 '15 at 18:07

1 Answers1

40

Change your onClick() method to

@Override
public void onClick(View v)
{                  
    imageView.setRotation(imageView.getRotation() + 90);
}

Notice, what the docs say

Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.


I'd like to update my answer to show how to use RotateAnimation to achieve the same effect in case you're also targeting Android devices running Gingerbread (v10) or below.

private int mCurrRotation = 0; // takes the place of getRotation()

Introduce an instance field to track the rotation degrees as above and use it as:

mCurrRotation %= 360;
float fromRotation = mCurrRotation;
float toRotation = mCurrRotation += 90;

final RotateAnimation rotateAnim = new RotateAnimation(
        fromRotation, toRotation, imageview.getWidth()/2, imageView.getHeight()/2);

rotateAnim.setDuration(1000); // Use 0 ms to rotate instantly 
rotateAnim.setFillAfter(true); // Must be true or the animation will reset

imageView.startAnimation(rotateAnim);

Usually one can setup such View animations through XML as well. But, since you have to specify absolute degree values in there, successive rotations will repeat themselves instead of building upon the previous one to complete a full circle. Hence, I chose to show how to do it in code above.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • @Darpan Thanks for your feedback. I've added an update to show how to support the rotate animation on Gingerbread or below. – Ravi K Thapliyal Jul 08 '15 at 18:01
  • http://chat.stackoverflow.com/rooms/82768/android-question wanted to ask you some things, glad if you help, read in chats. will delete the comment later. – Darpan Jul 09 '15 at 03:54