Android时钟秒针

时间:2011-01-19 19:03:01

标签: android android-widget

我是Android开发的新手,我想知道是否可以在android中为默认模拟时钟添加“秒针”?

我已经有一个带拨号,时针和分针的工作时钟。我一直在搜索,我发现了一些帖子,说明你需要在后台运行的服务每秒更新时钟。这是一个时钟小部件,矿山是一个应用程序。

有什么方法可以做到吗?

4 个答案:

答案 0 :(得分:1)

以下是如何操作:http://developer.android.com/resources/articles/timed-ui-updates.html

警告:这样做会非常快地耗尽电池!

答案 1 :(得分:0)

如果您已经用时针和分针自己渲染时钟,那么秒针的问题究竟是什么?更新它就像更新另一只手一样,只需将更新线程的频率增加到一秒。

答案 2 :(得分:0)

是的,我可以把二手放在Android模拟时钟中,我已经在我的应用程序中完成了它,请看它帮助你,如果你发现任何错误而不是在这里评论它。 这里的链接是you can find it here in link

答案 3 :(得分:0)

private ImageView img;
 Handler mHandler;

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Thread myThread = null;

  Runnable runnable = new CountDownRunner();
  myThread = new Thread(runnable);
  myThread.start();

 }

 public void doRotate() {

  runOnUiThread(new Runnable() {
   public void run() {
    try {

     Date dt = new Date();
     int hours = dt.getHours();
     int minutes = dt.getMinutes();
     int seconds = dt.getSeconds();
     String curTime = hours + ":" + minutes + "::" + seconds;
     Log.v("log_tag", "Log is here Time is now" + curTime);
     img = (ImageView) findViewById(R.id.imgsecond);
     RotateAnimation rotateAnimation = new RotateAnimation(
       (seconds - 1) * 6, seconds * 6,
       Animation.RELATIVE_TO_SELF, 0.5f,
       Animation.RELATIVE_TO_SELF, 0.5f);

     rotateAnimation.setInterpolator(new LinearInterpolator());
     rotateAnimation.setDuration(1000);
     rotateAnimation.setFillAfter(true);

     img.startAnimation(rotateAnimation);
    } catch (Exception e) {

    }
   }
  });
 }

 class CountDownRunner implements Runnable {
  // @Override
  public void run() {
   while (!Thread.currentThread().isInterrupted()) {
    try {

     doRotate();
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     Thread.currentThread().interrupt();
    } catch (Exception e) {
     Log.e("log_tag", "Error is " + e.toString());
    }
   }
  }
 }
相关问题