AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
来源:互联网 发布:魔能2汉化补丁mac 编辑:程序博客网 时间:2024/06/05 03:45
在一些需求中,需要在程序运行时动画自动启动,我们也知道在android提供的Tween Animation和frame animation。但是当使用frame animation时候,启动Frame Animation动画的代码anim.start();不能在OnCreate()中,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,在OnCreate()中启动动画,就只能看到第一张图片。现在问题是如何才能让程序启动时自动的启动动画?可以试一下在onStart方法中,但是结果同样不能如我们所愿。这样不行,继续尝试,使用Handler试一下!代码如下:
private
Runnable runnable=
new
Runnable() {
public
void
run() {
frameAnimation.start();
}
};
Handler handler=
new
Handler();
//在onCreate方法中:
handler.post(runnable);
handler对象将通过post方法,将里面的Runnable对象放到UI执行队列中,UI消费这个队列,调用Runnable的run方法。这里并不生成新的线程,此时的 Runnable 是运行在UI所在的主线程中。但是这种方法也是不行!
下面即是总结的三种自动启动frame animation的方法:
首先使用AsyncTask:Handler和AsyncTask,都是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的。AsyncTask使创建需要与用户界面交互的长时间运行的任务变得更简单。不需要借助线程和Handler即可实现。对于AsyncTask这里就不多说了,也就是用到一点。
imageV.setBackgroundResource(R.anim.myframeanimation);
frameAnim = (AnimationDrawable) imageV.getBackground();
class
RunAnim
extends
AsyncTask<String, String, String>{
@Override
protected
String doInBackground(String... params) {
if
(!frameAnim.isRunning()){
frameAnim.stop();
frameAnim.start();
}
return
""
;
}
}
//onCreate方法中执行
RunAnim runAnim=
new
RunAnim();
runAnim.execute(
""
);
这样就能在是程序自动执行frame animation了。
其次使用View.post(Runnable)的方式:
imageV.post(
new
Runnable(){
@Override
public
void
run() {
frameAnim.start();
}
});
文档:boolean android.view.View .post(Runnable action)
Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.即可把你的Runnable对象增加到UI线程中运行。
这样也能正常启动frame Animation。
第三就是使用ViewTreeObserver.OnPreDrawListener listener:当一个视图树将要绘制时产生事件,可以添加一个其事件处理函数:onPreDraw
OnPreDrawListener opdl=
new
OnPreDrawListener(){
@Override
public
boolean
onPreDraw() {
animDraw.start();
return
true
;
}
};
//onCreate方法中
imageV.getViewTreeObserver().addOnPreDrawListener(opdl);
以上即是总结的三种自动启动frame animation的方法,当然,对于android线程的处理,UI更新操作实现,肯定有其他的方法。以上描述中如有错误,还望多多包含与指教!!!
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动!!!
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- Android View.post(Runnable )
- 关于排序的选择题
- 如何去掉OTA provisioning的PIN码验证
- 【技术直通车】ArcGIS for Android离线数据编辑实现原理
- dm3730 qt和tslib 移植记录
- SIP协议简介(比较全面)
- AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
- jasperreport开发手册--ireport教程
- 你只能是你妈的儿子,不是她的老公
- 为应用程序添加停靠面板
- Wince上的进程通信
- 【技术直通车】Android中不能解析GIS类的XML文件或不能引入GIS类问题
- Python的subprocess子进程和管道进行交互
- 如何修改dede模板的默认颜色
- mysql:表注释和字段注释