Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:ViewFlipper animation

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!!

like image 460
Rocky Avatar asked Jul 13 '11 02:07

Rocky


People also ask

Is ViewFlipper deprecated?

Deprecated: The view drawing cache was largely made obsolete with the introduction of hardware-accelerated rendering in API 11. Low quality.

What is ViewFlipper in Android?

android.widget.ViewFlipper. Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

Can we do animation in Android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.

What is animation in Android with example?

Animation is the process of adding a motion effect to any view, image, or text. With the help of an animation, you can add motion or can change the shape of a specific view. Animation in Android is generally used to give your UI a rich look and feel.


2 Answers

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

like image 154
Glendon Trullinger Avatar answered Sep 27 '22 22:09

Glendon Trullinger


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();
        }

        }
like image 40
App Work Avatar answered Sep 27 '22 22:09

App Work