In android We Don't Have Second Clock In Analog Clock.
So I Find This Code For That
Here is The Code
protected static final String TAG = SecondHand.class.getName();
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());
}
}
}
}
-----------------------------------------------------------------------------------------------------------
Here is The Xml File For It
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_gravity="center">
<AnalogClock android:layout_width="wrap_content"
android:hand_minute="@drawable/widgetminute" android:dial="@drawable/widgetdial"
android:hand_hour="@drawable/widgethour" android:layout_height="wrap_content"
android:id="@+id/AnalogClock" android:layout_centerInParent="true">
</AnalogClock>
<ImageView android:src="@drawable/widgetseocond1" android:id="@+id/imgsecond"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------
Here the Look is
So I Find This Code For That
Here is The Code
protected static final String TAG = SecondHand.class.getName();
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());
}
}
}
}
-----------------------------------------------------------------------------------------------------------
Here is The Xml File For It
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_gravity="center">
<AnalogClock android:layout_width="wrap_content"
android:hand_minute="@drawable/widgetminute" android:dial="@drawable/widgetdial"
android:hand_hour="@drawable/widgethour" android:layout_height="wrap_content"
android:id="@+id/AnalogClock" android:layout_centerInParent="true">
</AnalogClock>
<ImageView android:src="@drawable/widgetseocond1" android:id="@+id/imgsecond"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
-------------------------------------------------------------------------------------------------------------
Here the Look is
you can download the tutorial for this code here the link Download
interesting post!
ReplyDeleteit would be nice to arrange this to a home-widget. not so easy as it may look like.
nice post,
ReplyDeletei make a widget from this, but why the seconds hand not moving at all?
what do we have to do to arrange this to a home-widget?
ReplyDeleteThank you very much, your application running nicely.
ReplyDeleteReally thanks.
http://www.moonreturn.com
Nice app, but how make it work...? I'm a new android user. Thanks. Can u give me a little tutor...
ReplyDeleteThanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
ReplyDeleteThanks for nice post. I just simplified ur code, it should be like this.
ReplyDeletepublic class SecondHand extends Activity {
// private Handler mHandler = new Handler();
protected static final String TAG = SecondHand.class.getName();
private ImageView img;
private Handler handler = new Handler();
@Override
protected void onResume() {
handler.removeCallbacks(mRunnable);
handler.postDelayed(mRunnable, 0);
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(mRunnable);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Runnable mRunnable = new Runnable() {
@Override
public void run() {
handler.removeCallbacks(mRunnable);
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.setFillAfter(true);
img.startAnimation(rotateAnimation);
handler.postDelayed(mRunnable, 1000);
}
};
}
Thanks for this tutorial.
ReplyDeleteHi,is there a way to make a widget with this?
ReplyDeleteHi,how to make this a widget?
ReplyDeleteHello,how to make this a widget?pls reply
ReplyDeletehow to download source code?
ReplyDeleteThanks
ReplyDeleteHey dudes,
ReplyDeletehow can i make rotate the second to 36 seconds instead of 60?
Any help will be appreciated