89890

how to make android sweeping second hand on analog clock?

Question:

I'm a newbie in android programing. Is there a way to make this analog clock with second hand do a sweeping (chronograph) movement instead of quartz?

Any sample to download?

I'm using this code for the clock:

public class SecondHand extends Activity { // private Handler mHandler = new Handler(); 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(); } private void doPlay(){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Log.v("log_tag", "this is seocond thread"); } }).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(); 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); } catch (Exception e) { Log.e("log_tag", "Error msg is " + e.toString()); } } }); } class CountDownRunner implements Runnable { // @Override public void run() { while (!Thread.currentThread().isInterrupted()) { try { // Log.v("log_tag", "Roate is going"); doRotate(); Thread.sleep(1000); doPlay(); } catch (InterruptedException e) { // Thread.currentThread().interrupt(); } catch (Exception e) { Log.e("log_tag", "Error is " + e.toString()); } } } } @Override protected void onDestroy() { android.os.Process.killProcess(android.os.Process.myPid()); }

Answer1:

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

<strong>xml file:</strong>

<?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:id="@+id/AnalogClock" android:dial="@drawable/widgetdial" android:hand_hour="@drawable/widgethour" android:layout_height="wrap_content" 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>

Recommend

  • Pthreads in XAMPP in linux
  • How do i add new field other than multer-gridfs-storage default properties
  • Why does getContextPath() under a proxy return the internal path inside HttpServlet but not inside F
  • Is it possible to reorder or ignore parameters in controller routes?
  • How to echo values of an associative array returned by function
  • determine if drive is removable (flash or HDD) knowing only the drive letter
  • hadoop jar command points to local filesystem
  • NOTE or WARNING from package check when README.md includes images
  • this.refs is undefined for shallow tests using enzyme on React native
  • Interactive labeling of images in jupyter notebook
  • Different sized images (with the same name) in one drawable folder
  • How to delete first 7 characters of folder name by using batch script?
  • Passing HTTP Post from AWS API GW to Lambda
  • Execute powershell script on a remote computer using C#
  • Use 2D Text into 3D scenes in JavaFX results in blurry texts
  • Making Django.contrib.auth store plain-text password
  • C# ICMPv6 checksum calculation
  • Filtering out choiceless polls in the Django tutorial causes polls in the index to duplicate
  • Custom progress dialog not working
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • Tkinter tkMessageBox disables Tkinter key bindings
  • NoClassDefFoundError: com.google.gson.GsonBuilder when using google play services with microsoft azu
  • PHP: Need to close STDIN in order to read STDOUT?
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • Autocomplete source from project settings
  • xpath assertion failure with dynamic xpath
  • Multiplying polynomials/simplifying like terms
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • how to read to huge file into buffer
  • C++ STL stack pop operation giving segmentation fault
  • Drag and drop unicode TText in DelphiXe4
  • How to handle div that is created dynamically in a table
  • Spring Boot fails to start
  • Google App Engine backend servlet not responding
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java
  • Spring Boot not autowiring @Repository
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}
  • Time Complexity of Fibonacci Algorithm [duplicate]