Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alpha / Translate Animations

I've got this animation which should move the applied view up out of the view whilst fading, move it back to below the view then back into the view whilst fading.

The problem is that it doesn't seem to fade - the opacity of the applied view is always 0.5.

<?xml version="1.0" encoding="UTF-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="0"
        android:toYDelta="-200"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="1995"
        android:startOffset="3000" />
    <translate
        android:fromYDelta="200"
        android:toYDelta="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="1995"
        android:startOffset="8005" />   

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:repeatMode="reverse"
        android:startOffset="3000"
        android:toAlpha="0.5" />
    <alpha
        android:duration="1995"
        android:fromAlpha="0.5"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:repeatMode="reverse"
        android:startOffset="8005"
        android:toAlpha="1.0" />
</set>

I've tried a number of things, nesting them in sets, removing the interpolator etc... The only thing that worked is using the example given in this SO question: android two alpha animations which leads me to believe it's something to do with running the alpha animation when combined with the translate animation.

Thanks!

like image 969
Jamie Avatar asked Oct 04 '13 14:10

Jamie


People also ask

What is Alpha in android animation?

An Alpha Animation is animation that controls the alpha level of an object, i.e. fading it in and out.

What is translate animation in Android?

Translate Animation can change the visual appearance of an object, but they cannot change the objects themselves. That is, if you apply a translate animation to a view, it would move to a new position, but its click events would not get fired; the click events would still get fired at its previous position.


1 Answers

This is an example:

AnimationSet set = new AnimationSet(true);
Animation trAnimation = new TranslateAnimation(0, 500, 0, 0);
trAnimation.setDuration(6000);

trAnimation.setRepeatMode(Animation.REVERSE); // This will make the view translate in the reverse direction

set.addAnimation(trAnimation);
Animation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(3000);
set.addAnimation(anim); 

txtView.startAnimation(set); // replace this with your view

I hope this helps! You can change this and use the a layout based animation defining the alpha and translate animations under the set tag.

like image 84
Rat-a-tat-a-tat Ratatouille Avatar answered Sep 22 '22 12:09

Rat-a-tat-a-tat Ratatouille