2011-01-19 74 views
3

我是新来的android开发,我想知道是否有可能在Android中添加一个“秒针”到默认的模拟时钟?Android时钟秒针

我已经有一个拨号,时针和分针的工作时钟。我搜索了一遍,我偶然发现了一些帖子,指出你需要一个在后台运行的服务来每秒钟更新时钟。这是一个时钟小部件,而矿井是一个应用程序。

请问有什么办法可以做到?

回答

0

如果您已经使用时针和分针自己绘制时钟,那么秒针的问题到底是什么?更新它只是更新其他手,只需将更新线程的频率增加到一秒。

0

是的,这是可能把二手机器人模拟时钟我已经在我的应用程序做过,请看到它可以帮助你,如果你发现任何错误而不是评论这里。 这里的链接是you can find it here in link

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()); 
    } 
    } 
    } 
}