android - 如何让android在模拟时钟上扫秒针?

标签 android

我是 android 编程的新手。有没有办法让这个带秒针的模拟时钟代替 quartz 进行扫频(计时码表)运动?

有任何示例要下载吗?

我将此代码用于时钟:

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

最佳答案

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

xml文件:

      <?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>

关于android - 如何让android在模拟时钟上扫秒针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13719890/

相关文章:

JAVA_HOME 问题。 JDK好像没有正确安装

android - 在 Android 手机休眠时处理 NFC

java - 启动画面后的 Activity

android - 在 Google TV 上流式传输音频

android - 在 android 中为 textview ScrollView

android - 在Android中如何在滑动时获取BottomSheet的当前高度?

java - EditText 获取 toString

android - OpenGL 二维水模拟

android - 根据屏幕分辨率使用适当的布局标记

java - 保存后图像未添加到图库