0

I have an ImageView image. I need to rotate this image 90 degrease right and after that to move this image from left to right. I managed how to do that. I used AnnimationListener and after rotation finished I started moveAnimation(). But before moving image returns to it original look(before rotation).

xml code for rotation rotation.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="0"
  android:interpolator="@android:anim/linear_interpolator"
  android:toDegrees="90"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="1000"
  android:startOffset="0"
/>

rotateAnimation()

    private void rotateAnimation(){

        Animation rotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
        rotation.setRepeatCount(0);
        rotation.setFillAfter(true);

        rotation.setAnimationListener(new AnimationListener() {


        public void onAnimationEnd(Animation animation) {
            moveAnnimation();
        }
    });

moveAnnimation()

     private void moveAnnimation(){

    TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 2000, 0, 0);
        moveLefttoRight.setDuration(1000);
        moveLefttoRight.setFillAfter(true);

        moveLefttoRight.setAnimationListener(new AnimationListener() {

        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

        }
    });


    image.startAnimation(moveLefttoRight);
}
haawa
  • 3,078
  • 1
  • 26
  • 35
  • can you give sample code that you are using for rotation and moving the image. – Shachillies Aug 10 '12 at 09:45
  • Okay but I give you my opinion .. try to use matrix for rotation and similar matrix for translation , in this way your matrix will be centralized and image view will not be reset . dont forget to set imageview's property scaletype to matrix before performing any matrix operation. – Shachillies Aug 10 '12 at 09:46

1 Answers1

0

You need to setFillAfter(true) for both the rotation and translation Animation object.

Animation.html#setFillAfter(boolean)

If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set. Note that this applies to individual animations and when using an AnimationSet to chain animations.

Following is my code, you need AnimationSet to achieve the chaining Animation Effect.

public class MainActivity extends Activity {

    ImageView image = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView)findViewById(R.id.imageview);

        animateImageView();
    }

    private void animateImageView() {
        AnimationSet set = new AnimationSet(true);
        set.setFillAfter(true);

        Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotation);
        TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
        moveLefttoRight.setDuration(1000);
        moveLefttoRight.setStartOffset(1000);

        set.addAnimation(rotation);
        set.addAnimation(moveLefttoRight);

        image.startAnimation( set );
    }   
} 
NcJie
  • 812
  • 7
  • 14
  • sure I done It! thats the problem! My bad not that i didn't said it. – haawa Aug 10 '12 at 09:22
  • I will add some code later but to say in short I use xml animation file for rotation and simple move animation without xml file. – haawa Aug 10 '12 at 09:25
  • It should work, I've tested the code. As what I understand from your requirement, it should rotate for 90 degree (no reset) then translate to the right (no reset). Maybe I'm wrong. – NcJie Aug 14 '12 at 01:16