解决ImageView代码中循环播放动画最后一帧闪烁问题

小弟在做项目的过程中碰到了一个很奇怪的问题,我先描述一下问题:我用xml写了一个动画的set,里面的内容如下所示:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillBefore="true"
     android:fillAfter="true">
    <scale
        android:duration="2000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:toXScale="0.1"
        android:toYScale="0.1" />
    <scale
        android:duration="2000"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:toXScale="2.0"
        android:toYScale="2.0" 
        android:startOffset="2000"/>
    <scale
        android:duration="2000"
        android:fromXScale="2.0"
        android:fromYScale="2.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:toXScale="1.0"
        android:toYScale="1.0" 
        android:startOffset="4000"/>
</set>

动画执行一遍时,动画播放完毕最后一帧并不会闪烁,但是当我为该animation增加一个监听,在onAnimationEnd方法里面再次执行该动画的时候,动画在执行完毕后的最后一帧时会闪烁,问题找了好久,最后确定问题所在为,一般我们都会把动画播放玩一遍的处理逻辑放在onAnimationEnd中,例如这样:

package com.example.imageanimationtext;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private ImageView image;
	private Animation setAnimation;
	private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView)findViewById(R.id.image);
        setAnimation = AnimationUtils.loadAnimation(this, R.anim.white_anim1);
        setAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
			<span style="white-space:pre">	</span>image.clearAnimation();
				setAnimation.reset();
				image.startAnimation(setAnimation);
			}
		});
        image.startAnimation(setAnimation);
    } 
}
这样动画久循环播放了,但是问题来了,正如上面所说的一样,出现了烦人的闪烁问题。

最后确定了问题,原理还不是很清楚,猜测:因为动画播放完毕之后给我们的回调onAnimationEnd函数里面可能系统有一些逻辑没有执行,我们就执行了清除动画等操作,没有给系统留出一定的时间去处理,所以给出了下面的写法:

package com.example.imageanimationtext;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private ImageView image;
	private Animation setAnimation;
	private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView)findViewById(R.id.image);
        setAnimation = AnimationUtils.loadAnimation(this, R.anim.white_anim1);
        setAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				//一定要这样做,不然动画在播放完毕的最后一帧会闪烁闪烁
				//初步猜测可能onAnimationEnd函数里面动画还没有播放完毕
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						image.clearAnimation();
						setAnimation.reset();
						image.startAnimation(setAnimation);
					}
				}, 10);
			}
		});
        image.startAnimation(setAnimation);
    } 
}

包括我上传的资源中有一个NiceListViewDemo的例子,也是因为这个原因,动画播放完一边后就不动了,也找了好久的问题,结果用这种方法解决完美的解决了。如果遇到动画的问题没有很好的解决的,可以试一试这种写法,没准可以得到意想不到的效果呢。

下面给出资源链接:

http://download.csdn.net/detail/ly985557461/7691591

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值