最近项目中有个需求,需要像《水果忍者》游戏中取得成就后,从屏幕顶部落下一张图片,3秒钟中后自动消失。

我相信大家有很多重方式实现,比如说:TimerTask + Timer。这里我向大家介绍一种比较简单的方式,使用
CountDownTimer这个类,听名字就比较适合我们的需求,闲言少叙,直接看代码
public class MainActivity extends Activity {
private static Handler handler;
private static final int HIDE_IV_TROPHY = 0;
private ImageView ivTrophy;
private Animation animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivTrophy = (ImageView)findViewById(R.id.iv_trophy);
animation = AnimationUtils.loadAnimation(this, R.anim.slide_top_to_bottom);
ivTrophy.setAnimation(animation);
ivTrophy.startAnimation(animation);
new MyCountDownTimer(3000, 1000).start();
handler = new Handler(new Handler.Callback

本文介绍了如何在Android项目中实现倒计时动画,类似《水果忍者》游戏中的效果。通过自定义一个内部类MyCountDownTimer,继承自CountDownTimer,并在onFinish()方法中利用Handler更新UI,使图片在3秒后自动消失。构造方法的参数分别表示总倒计时时间和每次回调的间隔时间。


被折叠的 条评论
为什么被折叠?



