Android does not perform up between the animation settings (I stepped on a pit)

Problem Description

Write a transparent animation (AlphaAnimation), it is very simple, is to make a picture from opaque to transparent cycle twice.
Click the button to execute the animation, the animation did not execute. But using Debug found that the code is actually executed, but showed no effect.
There is also a strange situation, is when I clicked on the button, and then execute the code, let go again rewrite the current activity onResume method, the animation will show out.
code show as below

  • One way: a transparent dynamic animation code
    //透明动画(AlphaAnimation)
    public void btAlphaAnimation() {
        //创建透明动画对象
        Animation animation = new AlphaAnimation(1, 0);
        //添加属性:动画播放时长
        animation.setDuration(1000);
        //添加属性:设置重复播放一次(总共播放两次)
        animation.setRepeatCount(1);
        //设置动画重复播放的模式
        animation.setRepeatMode(Animation.RESTART);
        //给图片控件设置动画
        ivPenguin.setAnimation(animation);
        //启动
        animation.start();
}
  • xml configuration and activity of the call in a
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1.0"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:toAlpha="0.0">
</alpha>

//下面是activity中执行代码
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_my_alpha);
ivPenguin.setAnimation(animation);
animation.start();

I do not know the guy who found the problem to see if there are?

The problem lies in: on setAnimation () This method, if you are using: startAnimation () method, the above is not a problem.

The following two methods source

Imation session

    public void setAnimation(Animation animation) {
        mCurrentAnimation = animation;
        if (animation != null) {
            // If the screen is off assume the animation start time is now instead of
            // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
            // would cause the animation to start when the screen turns back on
            if (mAttachInfo != null && mAttachInfo.mDisplayState == Display.STATE_OFF
                    && animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
                animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
            }
            animation.reset();
        }
    }

starting animation

    public void startAnimation(Animation animation) {
        animation.setStartTime(Animation.START_ON_FIRST_FRAME);
        setAnimation(animation);
        invalidateParentCaches();
        invalidate(true);
    }

In fact, we can see through the source code, the underlying startAnimation method also calls setAnimation this method. But startAnimation also implemented invalidate (true), and the role of this method you should know, is a UI refresh. After animation settings, UI refresh certainly need to demonstrate results.

PS: very pleased to share it all over again to solve the BUG article, look forward to the future and to share more meaningful article.

Guess you like

Origin www.cnblogs.com/io1024/p/11580436.html