美文网首页
android自定义控件的学习

android自定义控件的学习

作者: dong_hui | 来源:发表于2016-12-15 17:57 被阅读28次

    一· 屏幕中坐标系的认知:

    贴三张图就明白清楚了,参考地址:
    https://github.com/GcsSloop/AndroidNote/blob/master/CustomView/Base/%5B01%5DCoordinateSystem.md
    
    Paste_Image.png Paste_Image.png Paste_Image.png

    二· 自定义view分类,流程及一些重要函数:

    Paste_Image.png Paste_Image.png

    三· 自定义一个数字时钟

    public class MyClock extends TextView {   
     private final static String TAG = "DigitalClock";   
     private Calendar mCalendar;  
      private String mFormat = "yyyy-MM-dd HH:mm:ss  E";   //根据不同的时间格式显示不同   
     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;   
     }
    }
    

    相关文章

      网友评论

          本文标题:android自定义控件的学习

          本文链接:https://www.haomeiwen.com/subject/ksmimttx.html