I read the doc http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
我阅读了文档http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
It extends DrawableContainer which seen like a kind of UI, and it implements Runnable, so it should run in a new thread (It should right? since it implements Runnable). Then in this case, should we follow the rule "only update UI component in UI thread"?
它扩展了DrawableContainer,它看起来像一种UI,它实现了Runnable,所以它应该在一个新线程中运行(它应该正确?因为它实现了Runnable)。那么在这种情况下,我们应该遵循规则“只更新UI线程中的UI组件”吗?
I have tested a program that perform a heavy loop after AnimationDrawable.start(), and program crashs (with famous stop responding error). Now I am totally confused, is AnimationDrawable.start() run in new thread?
我测试了一个在AnimationDrawable.start()之后执行重循环的程序,程序崩溃(有着名的停止响应错误)。现在我完全糊涂了,AnimationDrawable.start()在新线程中运行吗?
Edit:
AnimationDrawable.start();
for (int i = 0 ; i< 10000000 ; i ++){
System.out.println(i);
}
2 个解决方案
#1
0
If you look at the source code : http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
如果你看一下源代码:http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
There is no new Thread started within this class. Actually, this component will create a new animation frame everytime it is drawn on screen. Nothing more.
这个类中没有新的Thread。实际上,每次在屏幕上绘制时,此组件都会创建一个新的动画帧。而已。
I don't know what you mean exactly by "a program that perform a heavy loop after AnimationDrawable.start()", but it's clear to me that any operation performed by AnimationDrawable has to be fast, basically nothing more than an image swap must be performed here.
我不知道你的意思是“在AnimationDrawable.start()之后执行重循环的程序”,但我很清楚,AnimationDrawable执行的任何操作都必须很快,基本上只是图像交换必须在这里演出。
#2
0
Now I know why it implements Runnable :
现在我知道为什么它实现了Runnable:
new Thread(){
public void run(){
AnimationDrawable.start();
}
}.start();
for (int i = 0 ; i< 10000000 ; i ++){
System.out.println(i);
}
I thought after I dispatch the start()
to new thread, it will run in a new thread and not causing any "no responding error", but in fact it still crashes. Now I know it implements Runnable because Android posts it back to the UI thread to execute.
在我将start()发送到新线程之后,我认为它将在新线程中运行并且不会导致任何“无响应错误”,但事实上它仍然崩溃。现在我知道它实现了Runnable,因为Android将它发布回UI线程来执行。
#1
0
If you look at the source code : http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
如果你看一下源代码:http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
There is no new Thread started within this class. Actually, this component will create a new animation frame everytime it is drawn on screen. Nothing more.
这个类中没有新的Thread。实际上,每次在屏幕上绘制时,此组件都会创建一个新的动画帧。而已。
I don't know what you mean exactly by "a program that perform a heavy loop after AnimationDrawable.start()", but it's clear to me that any operation performed by AnimationDrawable has to be fast, basically nothing more than an image swap must be performed here.
我不知道你的意思是“在AnimationDrawable.start()之后执行重循环的程序”,但我很清楚,AnimationDrawable执行的任何操作都必须很快,基本上只是图像交换必须在这里演出。
#2
0
Now I know why it implements Runnable :
现在我知道为什么它实现了Runnable:
new Thread(){
public void run(){
AnimationDrawable.start();
}
}.start();
for (int i = 0 ; i< 10000000 ; i ++){
System.out.println(i);
}
I thought after I dispatch the start()
to new thread, it will run in a new thread and not causing any "no responding error", but in fact it still crashes. Now I know it implements Runnable because Android posts it back to the UI thread to execute.
在我将start()发送到新线程之后,我认为它将在新线程中运行并且不会导致任何“无响应错误”,但事实上它仍然崩溃。现在我知道它实现了Runnable,因为Android将它发布回UI线程来执行。