从另一个 Runnable 中的处理程序更新 UI

发布于 2024-11-26 18:46:09 字数 1518 浏览 1 评论 0原文

我遇到了一些问题,它应该作为计时器工作。我根据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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-03 18:46:09

您无法从工作线程调用 UI 方法。相反,您需要将 mUpdateTimeTask 中的代码更改为

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {            
         seconds++;             
         timeFlow.post(new Runnable() {
               timeFlow.setText(String.valueOf(seconds));
         });
         // It's not clear if handler is accessible here, but you get the picture.
         handler.postDelayed(mUpdateTimeTask, 100); 
    }
};

工作线程Android 开发指南中的 部分有更多示例。

You cannot call a UI method from a worker thread. Instead you need to change the code in mUpdateTimeTask to

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {            
         seconds++;             
         timeFlow.post(new Runnable() {
               timeFlow.setText(String.valueOf(seconds));
         });
         // It's not clear if handler is accessible here, but you get the picture.
         handler.postDelayed(mUpdateTimeTask, 100); 
    }
};

The Worker Thread section in the android dev guide has more examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文