AlphaAnimation写在oncreate没效果

今天写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) {
            }
        });
    }

搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值