使用AnimationUtils.loadAnimation在Android中淡入文本

时间:2012-07-10 17:58:04

标签: android android-animation

如果有人能向我解释为什么这样有效,我将非常感激:

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);
        }

但这对我来说根本不起作用:

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

与上述相关的fade_in.xml是:

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

感谢您的帮助!     

1 个答案:

答案 0 :(得分:12)

适合我: 在文件夹 / res / anim - fadein.xml fadeout.xml

中创建两个文件

淡入:

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

淡出:

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

初始化代码:

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

... 使用:

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

case R.id.imgBtnHideContent:
    rlOrderBtns.startAnimation(animFadeOut);
    rlOrderBtns.setVisibility(View.INVISIBLE);
break;