之后按钮的第二次点击,为什么不会animationDrawable运行 [英] why wont animationDrawable run after second click of button
问题描述
大家好希望你能帮助并希望感谢 -
hi everyone hope you can help and thanks for looking -
我有一个线程来运行一个硬币旋转的动画是这个
i have a thread to run a coin spinning animation which is this
public class MainActivity extends Activity {
static AnimationDrawable frameAnimation;
public boolean currentSpin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//is this where you want me to do it
ImageView coinAnima = (ImageView) findViewById(R.id.imageView1);
Button bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinCoin1();
}
});
}
//end of onCreate
public void spinCoin1(){
coinAnima = (ImageView) findViewById(R.id.imageView1);
coinAnima.setBackgroundResource(R.anim.coin_spin_heads);
new Thread(new Runnable() {
public void run() {
frameAnimation = (AnimationDrawable) coinAnima.getBackground();
frameAnimation.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
frameAnimation.stop();
//end of run
}
//starts the thread
}).start();
//end of method
}
//end of class
}
它从一个按钮的一个onclick运行,
当我点击按钮,它会导致与第二时间误差
it runs from an onclick of a button, when i click the button a second time it causes and error
这是
03-10 23:33:28.804: E/AndroidRuntime(13689): FATAL EXCEPTION: Thread-12
03-10 23:33:28.804: E/AndroidRuntime(13689): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewRoot.checkThread(ViewRoot.java:2936)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.View.invalidate(View.java:5272)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.view.View.invalidateDrawable(View.java:7310)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.widget.ImageView.invalidateDrawable(ImageView.java:176)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:300)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:227)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.setFrame(AnimationDrawable.java:211)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.nextFrame(AnimationDrawable.java:203)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.run(AnimationDrawable.java:140)
03-10 23:33:28.804: E/AndroidRuntime(13689): at android.graphics.drawable.AnimationDrawable.start(AnimationDrawable.java:107)
03-10 23:33:28.804: E/AndroidRuntime(13689): at com.example.testanima.MainActivity$2.run(MainActivity.java:61)
03-10 23:33:28.804: E/AndroidRuntime(13689): at java.lang.Thread.run(Thread.java:1019)
任何人都可以再告诉我这个错误,并感谢修复为寻找
can anyone tell me a fix for this error and thanks again for looking
推荐答案
您不能maniuplate在另一个线程GUI元素。这是在文档明确规定。
You can't maniuplate gui elements in another thread. This is clearly stated in the docs.
您可以通过更新您的run()GUI元素:
You can update gui elements in your run() by using:
public void run()
...
runOnUiThread(new Runnable() {
@Override
public void run() {
update gui elements
}
});
....
也在你的动画你的第一个动画完成后调用clear()。然后你可以使用它不止一次。
Also call clear() on your animation after your first animation has completed. Then you can use it more than once.
这篇关于之后按钮的第二次点击,为什么不会animationDrawable运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!