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

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"/>

Related

How to make slide animation with a fixed screen and new screen?

I made three xml files for the transition.
enter_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
none.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
exit_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
However, It just works fine in Activities not in Fragments.
When I click the back button, the backward transition(pop) works fine. But it doesn't work properly when I call the new fragment. It just blinks when the screen changes.
I tried changing the duration to 50 of none.xml. And I see the new screen comes in from the right side. And also tried with 10000. But it just delays the changing time.
I am using navigation component. And I defined like this:
<action
android:id="#+id/action_initFragment_to_settingFragment"
app:destination="#id/settingFragment"
app:enterAnim="#anim/enter_from_right"
app:exitAnim="#anim/none"
app:popExitAnim="#anim/exit_to_right"
app:popEnterAnim="#anim/none"/>
What's wrong with it?
I think this is because of Z index. Is there any way to give Z index attribute?
This happened because of the z index. Activities have different depth. However, I guess Fragments have the same depth. So, When A Fragment is switched to B Fragment, They are on the same depth and transition is not shown properly.
The solution is giving Z index to the screen programmatically like below:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ViewCompat.setTranslationZ(view, 1F)
}
1F in here, it is the index of depth.
The interesting thing is the higher value creates the bigger shadow. If you give 100F, then it creates huge shadow underneath. And I can't see any shadows visibly when the value is 1F.

Define xml animation into Java

Is it possible to write slide_out_left animation using java?
Here is my animation xml.
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="300" android:fromXDelta="0%" android:toXDelta="-100%" />
</set>
yes it is possible:
view.animate().translationX(_amount_).setDuration(_time in ms_).start();
So, you will have to calculate translation distance by yourself.
Note: if you translate to 0 later, after some other translations - it will move to starting position.
To complete slide in effect try to use withStartAction also.

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

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