AsyncTask中的android动画 [英] android animation in AsyncTask

查看:91
本文介绍了AsyncTask中的android动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的动画

img = (ImageView) findViewById(R.id.animView);
anim_up = AnimationUtils.loadAnimation(this, R.drawable.bounce);

public void upAnim(View view){

    img.clearAnimation();
    img.setImageResource(R.drawable.up);
    anim_up.setRepeatCount(Animation.INFINITE);
    anim_up.setRepeatMode(Animation.REVERSE);
    img.startAnimation(anim_up);

}

,我想在asynctask中制作动画,但是我不知道该怎么做.我尝试了很多解决方案... 有人可以分享一些示例代码来实现该目标吗?

and i want to do this animation in asynctask, but I have no idea how do this. I tried a lot of solutions... Can someone please share some sample code on how to achieve that?

谢谢

推荐答案

正如马蒂亚斯(Matthias)在问题的评论中提到的那样,所有动画应在UI线程上完成.对于AsyncTask,在后台线程上调用doInBackground()方法,而在UI线程上调用onPreExecute(),onProgressUpdate(),onPostExecute()和onCancelled()之类的方法.根据您希望显示动画的时间,重写适当的AsyncTask方法并进行相应的调用.

As Matthias has mentioned in the question's comments, all animations should be done on the UI thread. For an AsyncTask, the doInBackground() method is called on the background thread whereas methods like onPreExecute(), onProgressUpdate(), onPostExecute() and onCancelled() are called on UI thread. Based on when you want to show your animation, override the appropriate AsyncTask method and make the call accordingly.

详细了解AsyncTask的生命周期此处.

Learn more about the lifecycle of AsyncTask here.

这篇关于AsyncTask中的android动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆