Android slow down TranslateAnimation over time - android

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

Related

support-v4:27.1.0 fragment custom animations do not work as expected

Fragment animations do not work properly with support-v4:27.1.0
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(ENTER_ANIM , LEAVE_ANIM)
.replace(R.id.main_activity_fragment_place_holder, fragment)
.addToBackStack(tag)
.commitAllowingStateLoss();
enter animation
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
leave animation
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="500" />
I just hit the same problem. Support library 27.1.0 seems to have a problem with anim transitions that use the alpha property.
My impression is that the transition engine does not correctly implement a "fill-after", so that the fragment alpha quickly bounces back to 1 before the fragment is removed. This causes a blinking effect where the replaced fragment is briefly visible and then disappears.
I resolved the issue switching to animator transitions.
I.e. replaced my /res/anim/fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="500"
/>
with an analogous /res/animator/fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="alpha"
android:valueFrom="0"
android:valueTo="1"
android:duration="500"
/>
I did the same for the fade_out transition.
The blinking effect was fixed in the latest support library version 27.1.1. (see issue 74051124)
I have the exact same problem after upgrading the support library from 27.0.2 to 27.1.0. Instead of fading smoothly, the fragments blink several times.
It seems that all animators work as expected, except alpha animators.
However, I have found a workaround for this bug: If you disable the enter animation, the transition still fades. It does not fade in the exact same way as before, but it looks good (or even better) in my opinion.
The new enter animation (which I have named nothing.xml) is:
<?xml version="1.0" encoding="utf-8"?>
<set/>

android; setting in/out animations on AdapterViewFlipper: Unknown animator name translate

I have some very simple animations that work perfectly with a ViewFlipper, but if I try setting them on an AdapterViewFlipper in/out, I get a runtime error "Unknown animator name translate". In looking at the respective methods on each, it looks like ViewFlipper expects a ViewAnimation, and AdapterViewFlipper expects an AdapterViewAnimation. The api's are otherwise the same, and both build without error. Here's the xml for one of the animations:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="800"/>
</set>
and I set it on the flipper like:
vf.setOutAnimation(this, R.anim.out_to_left);
I can guess this might mean that I can't use translate, type, but then how would I accomplish the same animation? Lame...
Found the answer here: https://stackoverflow.com/a/26197426/1534666
It appears that a ViewFlipperAdapter needs a objectAnimator, not a set.
Example left_in.xml, declared in animator folder
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1500"
android:valueTo="0"
android:duration="600"/>

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.

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

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