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??
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.
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.
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" >
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With