改变透明度的动画,创建动画时需要指定动画开始和结束的透明度,以及动画持续时间,透明度取值范围是0到1。

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

</alpha>

AlphaAnimation 的构造函数只有两个参数,分别是初始的透明度和结束的透明度:

    /**
     * Constructor to use when building an AlphaAnimation from code
     * 
     * @param fromAlpha Starting alpha value for the animation, where 1.0 means
     *        fully opaque and 0.0 means fully transparent.
     * @param toAlpha Ending alpha value for the animation.
     */
    public AlphaAnimation(float fromAlpha, float toAlpha) {
        mFromAlpha = fromAlpha;
        mToAlpha = toAlpha;
    }

在代码中实现透明度动画很简单,只需创建一个AlphaAnimation 实例,然后设置动画持续时间即可,语句如下:

                AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);
                //动画播放的时间间隔
                alphaAnimation.setDuration(3000);
                //动画结束后保留结束状态
                alphaAnimation.setFillAfter(true); 
                imageView.startAnimation(alphaAnimation);

results matching ""

    No results matching ""