Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give animation to the ViewSwitcher

I have created one advertisement control which consists of ViewSwitcher....

in that control i have ImageView and TextView because advertisement are of either text or images..

Now i have to give animation to the advetisements..

I have tried following

Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left); inAnimation.setDuration(1500);

Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); outAnimation.setDuration(1500);

And i set it to the switcher as

ViewSwitcher switcher;

switcher.setInAnimation(inAnimation);

switcher.setOutAnimation(outAnimation);

but it won't work..

Please give me any other alternative.. Or if use of above code is wrong then how to use it??

like image 720
NullPointerException Avatar asked May 10 '12 10:05

NullPointerException


People also ask

What is ViewSwitcher in android studio?

ViewAnimator that switches between two views, and has a factory from which these views are created. You can either use the factory to create the views, or add them yourself. A ViewSwitcher can only have two child views, of which only one is shown at a time.

What is a View switcher?

In Android, ViewSwitcher is a sub class of ViewAnimator that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views. It is mainly useful to animate a view on screen.


3 Answers

Try setting animation inside xml as

<ViewSwitcher
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:inAnimation="@android:anim/slide_in_left"
    android:outAnimation="@android:anim/slide_out_right" >
like image 96
pbielik Avatar answered Nov 07 '22 04:11

pbielik


In addition to this :

Take care of switcher.showNext(); or switcher.showPrevious();

If you set an animation to the switcher, both action will result in the same animation.

like image 44
Tobliug Avatar answered Nov 07 '22 04:11

Tobliug


I am giving an alternative as mentioned in the question. Using this you will achieve the same animation that ViewPager have.

Instead of showNext, you can set displayedChild to 1.

switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_right)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_left)
switcherView?.displayedChild = 1

Instead of showPrevious, you can set displayedChild to 0.

switcherView?.inAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_in_left)
switcherView?.outAnimation = AnimationUtils.loadAnimation(baseActivity, R.anim.slide_out_right)
switcherView?.displayedChild = 0

Animation Files: R.anim.slide_in_right:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

R.anim.slide_out_left

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>

R.anim.slide_in_left

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
</set>

R.anim.slide_out_right

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_shortAnimTime"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>

Each time you set displayedChild, you need to set the animation. If you don't want the animation, you can simply neglect it.That's all. Happy Coding!

PS: Kotlin code

like image 28
Swathi Avatar answered Nov 07 '22 03:11

Swathi