Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fading in text in Android using AnimationUtils.loadAnimation

I would be very grateful if someone could explain to me why this works:

private void startAnimating() {
    TextView logo1 = (TextView) findViewById(R.id.Shizzle);   
        final Animation fade1 = new AlphaAnimation(0.0f, 1.0f);  
        fade1.setDuration(3000);
        logo1.startAnimation(fade1);
        }

But this doesn't work at all for me:

private void startAnimating() {
    TextView logo1 = (TextView) findViewById(R.id.Shizzle);   
        Animation fade1 = AnimationUtils.loadAnimation(this,R.anim.fade_in);  
    logo1.startAnimation(fade1);    
    }

The fade_in.xml associated with the above is:

    <?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/android"
    android:shareInterpolator="false">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="3000">
    </alpha>

Thanks for your help!

like image 485
BryanJ Avatar asked Jul 10 '12 17:07

BryanJ


People also ask

How do you fade animation on android?

To use Fade In or Fade Out animations in our android applications, we need to define a new XML file with <alpha> tag like as shown below. For Fade In animation, we need to increase the alpha value from 0 to 1 like as shown below. <? xml version="1.0" encoding="utf-8"?>

What is Alpha animation in android?

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

What is Property animation in android?

A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.


1 Answers

Works for me: Create two file in folder /res/anim - fadein.xml, fadeout.xml

fadein:

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

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" >
    </alpha>

</set>

fadeout:

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

    <alpha
        android:duration="200"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" >
    </alpha>

</set>

initialize code:

Animation animFadeIn, animFadeOut;
...
animFadeIn=AnimationUtils.loadAnimation(this, R.anim.fadein);
animFadeOut=AnimationUtils.loadAnimation(this, R.anim.fadeout);

... using:

case R.id.imgBtnShowContent:
    rlOrderBtns.startAnimation(animFadeIn);
    rlOrderBtns.setVisibility(View.VISIBLE);
break;

case R.id.imgBtnHideContent:
    rlOrderBtns.startAnimation(animFadeOut);
    rlOrderBtns.setVisibility(View.INVISIBLE);
break;
like image 101
xck Avatar answered Sep 23 '22 00:09

xck