How to give animation to the ViewSwitcher - android

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

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

Nothing happen or you got some error? What u mean by it won't work?
Did you start animation with switcher.showNext(); or switcher.showPrevious();
Hope it will help.. Cheers ;)

A "switcher.showNext();" from the last layout and a "switcher.showPrevious();" from the first layout gives error. It must be similar to the stackoverflow and stackunderflow situation in a stack.so before you call "showNext()" check its not the last layout that you are currently in and also that you are not in the first layout when calling "showPrevious()". I came across this simple mistake.
Sorry for making this a post, I am (a rookie) not yet authorised to comment on posts

Related

Android slow down TranslateAnimation over time

I wanted to make my own animations for fragment transitions. I want it to come in from the bottom fast and then slow down as it reaches its position. I have done something similar in the past but i don't have the code anymore and i can't reproduce it either :/ I remember it had something to do with the interpolator.
This is my current animation set
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200">
<translate
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
Add the interpolator to your <set> like this:
android:interpolator="#android:anim/decelerate_interpolator"
Source: Animation resources

Slide up activity

I have an activity and another activity.
I want my first activity to end when I slide up the screen. The animation should be like the activity is sliding up too. Like the notification screen.
Is that possible? I have done many Google searches before posting this question, but could not get anything.
P.S - I don't want this to be seen as a casual question since there is no code shown. I just need some point to start and I am completely baffled.
make an anim folder in res->
Make an xml file in anim folder slide_up_info.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
slide_down_info.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%p" />
</set>
no_change.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%p" android:toYDelta="0" android:duration="500"/>
</set>
Now when you want to activity up then write below code
Intent intent_info = new Intent(MainActivity.this,ToActivity.class);
startActivity(intent_info);
overridePendingTransition(R.anim.slide_up_info,R.anim.no_change);
For down Activity animation
Intent intent_home=new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent_home);
overridePendingTransition(R.anim.no_change,R.anim.slide_down_info);
Animations can be introduced to Activity Launch using OverridePendingTransition. Take a look at this example where they have showed a sample code. Animation between activities
To end an activity, you can call finish() on the first activity.
The know about the slideup, you can check onFling from GestureDetector or onTouch from OnTouchListener
You can also do it this way : in your animation, override the onAnimationEnd method, and use startActivity in it to launch your new activity after the end of the animation

Android Activity Transition Animation

What I am trying to achieve is : start a new activity with a transition animation for the exiting activity only.
I would like to slide up the current activity, where the new activity will be behind the current one.
Here is the slide up animation : R.layout.slide_up
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0%"
android:toYDelta="100%" />
</set>
Here is how I am applying the activity animation transition :
overridePendingTransition ( 0 , R.anim.slide_up );
I am using 0 for the entering activity since I do not want any animation for the new activity, and it is not working (the animation is not performed). If I use an animation for entering activity too, it works (both animations are performed), like such :
overridePendingTransition ( R.anim.slide_out , R.anim.slide_up );
where R.anim.slide_out :
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%"
android:toYDelta="0%" />
</set>
Any ideas ?
I am working on Android 4.1.2 and Android 4.0.4
Alter your exit animation so that it renders over top of the entering activity.
R.anim.slide_up
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:zAdjustment="top">
<translate
android:duration="1000"
android:fromYDelta="0%"
android:toYDelta="100%" />
</set>
Then you can do what you were originally doing to set the animation.
overridePendingTransition ( 0 , R.anim.slide_up );
I have exactly the same transition and such animation works for me:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromYDelta="0%" android:toYDelta="100%" android:zAdjustment="top"
android:duration="300" />
Call the below method after startActivity method.
overridePendingTransition(0,0);
This will override the default animation and do no animation. You can also give some custom animation if you like
overridePendingTransition(R.anim.animation1,R.anim.animation2);
Wherever you are calling the intent to start the Activity, you need to modify the intent.
ActivityOptions options =
ActivityOptions.makeSceneTransitionAnimation(AlbumListActivity.this);
startActivity(intent, options.toBundle());
If you have any dedicated method to setupTransitions() You can put the next two lines of code there, Else you may put them in onCreate()
getWindow().setEnterTransition(new Slide(Gravity.RIGHT).setDuration(800));
Gravity.RIGHT is what determines the direction from which you want to start your next Activity. setDuration() method is optional, for smoother Transition I used it, You don't have to.
Explore more by playing around with different Gravity and setDuration Properties.

Android - Animation offset - How to prevent the view from being drawn while the offset has not yet passed?

I am trying to start an animation AFTER 1 second. I have used the attribute "android:startOffset" in my XML file, but it does not work completely the way I expected. I was expecting the view to NOT EVEN BE DRAW in its initial position (that is, the position set in the attributes "fromXDelta" and "fromYDelta") before the offset I set has passed. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
android:shareInterpolator="false" >
<translate
android:duration="2000"
android:startOffset="1000"
android:fromXDelta="-70%p"
android:fromYDelta="0%p"
android:interpolator="#android:anim/linear_interpolator"
android:toXDelta="+0%p"
android:toYDelta="0%p" />
</set>
If I try to move my view using the above animation, the view is drawn IMMEDIATELY at the position -70% of the screen. Then the one second passes and then, as expected, the animation kicks in and starts to move the view. However, I DO NOT want the view to be drawn at all before that 1 second!. How can I achieve this?
Thank you in advance.
UPDATE
I am calling the above XML just after a startActivity call (the *R.anim.animation_coming_in* below), like this:
startActivity(new Intent(this, ThankYouActivity.class));
overridePendingTransition(R.anim.animation_coming_in, R.anim.animation_coming_out);
You could try using a pair of alpha animations with very short duration so that the view is hidden until it's needed. Something like this:
<set ...>
<alpha
android:fromAlpha="0.0"
android:toAlpha="0.0"
android:duration="1"
android:startOffset="0" />
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1"
android:startOffset="1000" />
<translate
...
/>
</set>
Alternatively, you could implement this set of animations in code. Doing so would enable you to use a Handler to start the animation after a delay so that the view is hidden until the animation starts.

Android animation startOffset makes the target view blink and not animate

I'm trying to create an animation which will slide a textview out to the left and slide in again from the right. Essentially, this would be the same text effect used in the Stopwatch & Timer app (sportstracklive is the developer).
I can use either of these animation sets exclusive of the other and it works fine, does exactly what I want. But as soon as I try using them together, the TextView just blinks over the course about around 1 second. Removing the startOffset works as expected. Both animation sets run simultaneously.
Here's the XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="-25%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="#android:integer/config_shortAnimTime"
/>
<set>
<translate
android:fromXDelta="25%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:startOffset="#android:integer/config_shortAnimTime"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:startOffset="#android:integer/config_shortAnimTime"
android:duration="#android:integer/config_shortAnimTime"
/>
</set>
</set>
And here's the lengthy Java code that runs it:
AnimationSet mSlideRightToLeft =
(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.slide_right_to_left);
mMyTextView.startAnimation(mSlideRightToLeft);
Justinl's comment is correct. I had the exact same problem a couple months ago. Remove the set tags around the other animations, and keep the startOffsets.
Next remove the animation set in your code and just do a normal load animation:
Animation a = AnimationUtils.loadAnimation(this, R.anim.slide_right_to_left);
mMyTextView.startAnimation(a);
Edit: Yea, it looks like Android simply doesn't like this setup when there are multiple animations at the same time. I think you might have to create separate files for each set of animations and then configure them via an AnimationSet inside your program.

Categories

Resources