android自定義控件的學(xué)習(xí)

一· 屏幕中坐標(biāo)系的認(rèn)知:

貼三張圖就明白清楚了,參考地址:
https://github.com/GcsSloop/AndroidNote/blob/master/CustomView/Base/%5B01%5DCoordinateSystem.md
Paste_Image.png
Paste_Image.png
Paste_Image.png

二· 自定義view分類,流程及一些重要函數(shù):

Paste_Image.png
Paste_Image.png

三· 自定義一個數(shù)字時鐘

public class MyClock extends TextView {   
 private final static String TAG = "DigitalClock";   
 private Calendar mCalendar;  
  private String mFormat = "yyyy-MM-dd HH:mm:ss  E";   //根據(jù)不同的時間格式顯示不同   
 private Runnable mTicker;   
 private Handler mHandler;  
  private boolean mTickerStopped = false;   
 public MyClock(Context context) {  
      super(context);     
   initClock(context);   
 }   
 public MyClock(Context context, AttributeSet attrs) {      
  super(context, attrs);     
   initClock(context);   
 } 
   private void initClock(Context context) {    
    if (mCalendar == null) {      
      mCalendar = Calendar.getInstance();    
    }
    } 
   @Override  
  protected void onAttachedToWindow() {      
  mTickerStopped = false;       
 super.onAttachedToWindow();     
   mHandler = new Handler();      
  mTicker = new Runnable() {     
       public void run() {       
         if (mTickerStopped)            
        return;                
      mCalendar.setTimeInMillis(System.currentTimeMillis());      
      setText(DateFormat.format(mFormat, mCalendar));              
      invalidate();       
      long now = SystemClock.uptimeMillis();          
      long next = now + (1000 - System.currentTimeMillis() % 1000);    
      mHandler.postAtTime(mTicker, next);       
     } 
       };   
     mTicker.run(); 
   } 
   @Override 
   protected void onDetachedFromWindow() {        
super.onDetachedFromWindow();  
      mTickerStopped = true;   
 }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容