从另一个 Runnable 中的处理程序更新 UI
我遇到了一些问题,它应该作为计时器工作。我根据Android中使用“定时器”阅读了这篇文章: http://developer.android.com/resources/articles/timed-ui -updates.html
我的布局中有 TextView 和 ImageView。我在这个 ImageView 中有 AnimationDrawable 。我已经重写了 AnimationDrawable 类,因为我想知道我的动画何时完成。但是,我想在动画结束时调用的可运行对象 - 可以正常工作。但以防万一,当我想每秒更新 TextView 时,另一个可运行对象(在下面的代码中)仅调用一次(我可以在所有动画期间看到数字“1”)。
TextView timeFlow;
int seconds;
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
seconds++;
timeFlow.setText(String.valueOf(seconds));
}
};
private void startAnimation() {
image = (ImageView) this.findViewById(R.id.image);
recordImage.setBackgroundResource(R.drawable.record_animation);
timeFlow = (TextView) this.findViewById(R.id.time_flow);
timeFlow.setText("...");
image.post(new Runnable() {
@Override
public void run() {
CustomAnimationDrawable currentAnimation = new CustomAnimationDrawable((AnimationDrawable) recordImage.getBackground());
currentAnimation.setOnFinishCallback(runnable);
recordImage.setBackgroundDrawable(currentAnimation);
currentAnimation.start();
handler.removeCallbacks(mUpdateTimeTask);
handler.postDelayed(mUpdateTimeTask, 100);
}
});
}
I have problem with something, which should work as a timer. I have read this article according to the using "timer" in Android:
http://developer.android.com/resources/articles/timed-ui-updates.html
I have the TextView and ImageView in my layout. I have AnimationDrawable in this ImageView. I have overrided the AnimationDrawable class, because I want to know when my animation is completed. However, the runnable I want to call when my animation is ended - work properly. But in case, when I want to upadate TextView each second, another runnable (in the code below) is calling only once (I can see the number "1" during all the animation).
TextView timeFlow;
int seconds;
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
seconds++;
timeFlow.setText(String.valueOf(seconds));
}
};
private void startAnimation() {
image = (ImageView) this.findViewById(R.id.image);
recordImage.setBackgroundResource(R.drawable.record_animation);
timeFlow = (TextView) this.findViewById(R.id.time_flow);
timeFlow.setText("...");
image.post(new Runnable() {
@Override
public void run() {
CustomAnimationDrawable currentAnimation = new CustomAnimationDrawable((AnimationDrawable) recordImage.getBackground());
currentAnimation.setOnFinishCallback(runnable);
recordImage.setBackgroundDrawable(currentAnimation);
currentAnimation.start();
handler.removeCallbacks(mUpdateTimeTask);
handler.postDelayed(mUpdateTimeTask, 100);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法从工作线程调用 UI 方法。相反,您需要将 mUpdateTimeTask 中的代码更改为
工作线程Android 开发指南中的 部分有更多示例。
You cannot call a UI method from a worker thread. Instead you need to change the code in mUpdateTimeTask to
The Worker Thread section in the android dev guide has more examples.