0

我需要爲我的佈局實現數字時鐘。我希望小時和分鐘之間的點消失,並在每秒鐘的一半左右重新出現。我複製了android時鐘代碼並改變了一下。這個想法是基於一些布爾值來改變時間格式。但由於某種原因,這種方法不起作用......如果你是一個並行主,請幫助我! 這是代碼。如何讓android數字時鐘閃爍?

public class CustomDigitalClock extends TextView { 

    Calendar mCalendar; 
    private final static String m12 = "h:mm aa"; 
    private final static String m24 = "k:mm"; 
    private final static String m24space = "k mm"; 
    private static final String LOG_TAG = "Clock"; 
    private FormatChangeObserver mFormatChangeObserver; 

    private Runnable mTicker; 
    private Runnable dotsRunner; 
    private Handler mHandler; 
    private AtomicBoolean dotsVisible; 

    private volatile boolean mTickerStopped = false; 

    String mFormat; 

    public CustomDigitalClock(Context context) { 
     super(context); 
     initClock(context); 
    } 

    public CustomDigitalClock(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initClock(context); 
    } 

    private void initClock(Context context) { 
     Resources r = context.getResources(); 
     dotsVisible = new AtomicBoolean(); 


     if (mCalendar == null) { 
      mCalendar = Calendar.getInstance(); 
     } 

     mFormatChangeObserver = new FormatChangeObserver(); 
     getContext().getContentResolver().registerContentObserver(
       Settings.System.CONTENT_URI, true, mFormatChangeObserver); 

     setFormat(); 
    } 

    @Override 
    protected void onAttachedToWindow() { 
     mTickerStopped = false; 
     super.onAttachedToWindow(); 
     mHandler = new Handler(); 

     /** 
     * requests a tick on the next hard-second boundary 
     */ 
     mTicker = new Runnable() { 
      public void run() { 
       if (mTickerStopped) return; 
       mCalendar.setTimeInMillis(System.currentTimeMillis()); 

       if (dotsVisible.get()) { 
        setText(DateFormat.format(m24, mCalendar)); 
       } else { 
        setText(DateFormat.format(m24space, mCalendar)); 
       } 

       invalidate(); 
       long now = SystemClock.uptimeMillis(); 
       long next = now + (1000 - now % 1000); 
       mHandler.postAtTime(mTicker, next); 
      } 
     }; 
     dotsRunner = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Thread.sleep(1000); 
        dotsVisible.compareAndSet(true, false); 
        dotsVisible.compareAndSet(false, true); 
       } catch (InterruptedException ex) { 
        Log.e(LOG_TAG, ex.getMessage()); 
       } 
      } 
     }; 
     dotsRunner.run(); 
     mTicker.run(); 

    } 

    @Override 
    protected void onDetachedFromWindow() { 
     super.onDetachedFromWindow(); 
     mTickerStopped = true; 
    } 

    /** 
    * Pulls 12/24 mode from system settings 
    */ 
    private boolean get24HourMode() { 
     return android.text.format.DateFormat.is24HourFormat(getContext()); 
    } 

    private void setFormat() { 
     if (get24HourMode()) { 
      mFormat = m24; 
     } else { 
      mFormat = m12; 
     } 
    } 

    private class FormatChangeObserver extends ContentObserver { 
     public FormatChangeObserver() { 
      super(new Handler()); 
     } 

     @Override 
     public void onChange(boolean selfChange) { 
      setFormat(); 
     } 
    } 

} 

回答

0

所以我自己解決了這個問題。如果你需要有一個點閃爍的時鐘,你可以拿走我的。

public class CustomDigitalClock extends TextView { 

    Calendar mCalendar; 
    private final static String m12 = "h:mm aa"; 
    private final static String m24 = "k:mm"; 
    private final static String m24space = "k mm"; 
    private static final String LOG_TAG = "Clock"; 
    private FormatChangeObserver mFormatChangeObserver; 

    private Runnable mTicker; 
    private Runnable dotsRunner; 
    private Handler mHandler; 
    private AtomicBoolean dotsVisible; 

    private volatile boolean mTickerStopped = false; 
    private volatile boolean dotsRunnerStopped = false; 

    String mFormat; 

    public CustomDigitalClock(Context context) { 
     super(context); 
     initClock(context); 
    } 

    public CustomDigitalClock(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initClock(context); 
    } 

    private void initClock(Context context) { 
     Resources r = context.getResources(); 
     dotsVisible = new AtomicBoolean(true); 


     if (mCalendar == null) { 
      mCalendar = Calendar.getInstance(); 
     } 

     mFormatChangeObserver = new FormatChangeObserver(); 
     getContext().getContentResolver().registerContentObserver(
       Settings.System.CONTENT_URI, true, mFormatChangeObserver); 

     setFormat(); 
    } 

    @Override 
    protected void onAttachedToWindow() { 
     mTickerStopped = false; 
     dotsRunnerStopped = false; 
     super.onAttachedToWindow(); 
     mHandler = new Handler(); 

     /** 
     * requests a tick on the next hard-second boundary 
     */ 
     mTicker = new Runnable() { 
      public void run() { 
       if (mTickerStopped) return; 
       mCalendar.setTimeInMillis(System.currentTimeMillis()); 

       if (dotsVisible.get()) { 
        setText(DateFormat.format(m24, mCalendar)); 
       } else { 
        setText(DateFormat.format(m24space, mCalendar)); 
       } 

       invalidate(); 
       long now = SystemClock.uptimeMillis(); 
       //long next = now + (1000 - now % 1000); 
       long next = now + 800; 

       mHandler.postAtTime(mTicker, next); 
      } 
     }; 
     dotsRunner = new Runnable() { 
      @Override 
      public void run() { 
       while(!dotsRunnerStopped) { 
        try { 
         Thread.sleep(800); 
         Log.i(LOG_TAG, "slept for 1 sec"); 
         if (dotsVisible.compareAndSet(true, false)) { 
          Log.i(LOG_TAG, "set visible to " + dotsVisible.get()); 
         } else { 
          dotsVisible.compareAndSet(false, true); 
          Log.i(LOG_TAG, "Set visible to " + dotsVisible.get()); 
         } 
        } catch (InterruptedException ex) { 
         Log.e(LOG_TAG, ex.getMessage()); 
        } 
       } 
      } 
     }; 
     mTicker.run(); 
     new Thread(dotsRunner).start(); 

    } 

    @Override 
    protected void onDetachedFromWindow() { 
     super.onDetachedFromWindow(); 
     mTickerStopped = true; 
     dotsRunnerStopped = true; 
    } 

    /** 
    * Pulls 12/24 mode from system settings 
    */ 
    private boolean get24HourMode() { 
     return android.text.format.DateFormat.is24HourFormat(getContext()); 
    } 

    private void setFormat() { 
     if (get24HourMode()) { 
      mFormat = m24; 
     } else { 
      mFormat = m12; 
     } 
    } 

    private class FormatChangeObserver extends ContentObserver { 
     public FormatChangeObserver() { 
      super(new Handler()); 
     } 

     @Override 
     public void onChange(boolean selfChange) { 
      setFormat(); 
     } 
    } 

}