5

I have add a ViewFlipper in which has 2 linearlayout,and I have made an animation xml: left_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="3000"/>
</set>

right_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="3000"/>
</set>

left_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="3000"/>
</set>

right_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="3000"/>
</set>

the "Next" button in one linearlayout which shows when first load the app:

mNext.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
                //mViewFlipper.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
                mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));
                mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_out));
                mViewFlipper.showNext();
            }

        });

and the "Prev" button:

mPrev.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mViewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
            mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_in));
            mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_out));
            mViewFlipper.showPrevious();
        }       
    });

The "Next" Button goes well, But the "Prev" Button goes strange: when I click the "prev",it first change into the previous view and then start the animation ,and at last it changes into the Previous view again! How to solve it?? Thanks in advance!!

Rocky
  • 1,287
  • 3
  • 15
  • 37

2 Answers2

4

You want to use setOutAnimation() and setInAnimation().

Glendon Trullinger
  • 4,052
  • 1
  • 28
  • 35
  • But when I press the other button(which show the Previous),it goes strange:the current view change into the previous one and then start the animation! Why? – Rocky Jul 13 '11 at 02:51
  • On the second button, you switched the animations in your `setOutAnimation()` and `setInAnimation()`. – Glendon Trullinger Jul 13 '11 at 03:13
  • Nothing changed when you put the `R.anim.left_out` on your `setOutAnimation()` and the `R.anim.right_in` on your `setInAnimation()`? I'm not talking about the order of the statements. – Glendon Trullinger Jul 13 '11 at 03:22
3

Well this a very old post. but still the fix is here:

you need to call viewFlipper.setOutAnimation(null) and viewFlipper.setInAnimation(null) to reset the animation .

   @Override
        public void onClick(View v)
        {
        if (v.equals(mNext))
        {
            mViewFlipper.setOutAnimation(null);

             mViewFlipper.setInAnimation(null);
              mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_in));

        vf.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_out));

            mViewFlipper.showNext();

        }
        else if (v.equals(mPrev))
        {

            mViewFlipper.setOutAnimation(null);

             mViewFlipper.setInAnimation(null);

            mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_in));

        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_out));

             mViewFlipper.showPrevious();
        }

        }
App Work
  • 21,899
  • 5
  • 25
  • 38