Android clock seconds hand
I'm new to android development and I was wondering whether it's possible to add a "seconds hand" to the default analog clock in android?
I already have a working clock with a开发者_开发百科 dial, hour and minute hands. I've searched around and I came across some posts stating that you need a service running in the background to update the clock every second. This was for a clock widget though and mines is for an app.
Is there any way it can be done?
Here's how to do it: http://developer.android.com/resources/articles/timed-ui-updates.html
Warning: doing that will drain the battery really fast!
If you already render the clock yourself with hour and minute hands, what exactly is the problem with the seconds hand? Update it just exactly as you update the other hands, just increase the frequency of update thread to one second.
yes it is possible to put second hand in android analog clock i have done that in my application please see it help you, if you find any error than comment here for it. here the link is you can find it here in link
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());
}
}
}
}
精彩评论