AlphaAnimation写在oncreate没效果

来源:互联网 发布:暗黑黎明挂机软件 编辑:程序博客网 时间:2024/04/30 19:31

今天写demo的时候用到了AlphaAnimation

能做出一种透明动画渐变效果

有一个xml文件里有个<ImageView>

一个.java文件

public class aa extends Activity {
    private ImageView imageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageView = (ImageView) View.inflate(this, R.layout.activity_splash, null);
        setContentView(imageView);

//渐变
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);

//动画持续时间
        alphaAnimation.setDuration(2000);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {

//动画执行前
            @Override
            public void onAnimationStart(Animation animation) {
            }
//动画播放完成后调用的函数
            @Override
            public void onAnimationEnd(Animation animation) {
                startMainActivity();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

//设置动画效果到imageview
        imageView.startAnimation(alphaAnimation);
    }



然后  然后问题来了  这个Imageview并没有动画效果

最后在师兄的帮助下 才明白这个动画效果不能放在oncreate()里执行


至于原因可能是因为

在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,

在OnCreate()中启动动画,就只能看到第一张图片。


然后就有了解决方案

方法—:使用Handle(http://www.bkjia.com/gjrj/798077.html 其实不知道有没效果找为什么不能在onCreate使用动画找到的)

方法二:

吧代码写在onWindowFocusChanged()里 好像说这个很重要(http://blog.csdn.net/pi9nc/article/details/9237031)

反正我第一次见。吧动画效果剪切到里面。

public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        //透明动画效果
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f,1.0f);
        //设置动画时间
        alphaAnimation.setDuration(3000);
        //加载动画效果到控件
        imageView.startAnimation(alphaAnimation);
        //
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }
            public void onAnimationEnd(Animation animation) {
                startMainActivity();
            }
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

搞定。

0 0
原创粉丝点击