自己的AnalogClock 指针 时分秒

 

  1. package  com.android.clockview;  
  2.   
  3. import  java.util.Calendar;     
  4. import  java.util.TimeZone;     
  5.   
  6.   
  7.     
  8. import  android.R.integer;  
  9. import  android.app.Activity;     
  10. import  android.content.Context;     
  11. import  android.graphics.Bitmap;     
  12. import  android.graphics.BitmapFactory;     
  13. import  android.graphics.Canvas;     
  14. import  android.graphics.Color;     
  15. import  android.graphics.Matrix;     
  16. import  android.graphics.Paint;     
  17. import  android.graphics.drawable.BitmapDrawable;  
  18. import  android.os.Bundle;     
  19. import  android.os.Handler;     
  20. import  android.util.Log;  
  21. import  android.view.View;     
  22. import  android.view.ViewGroup;  
  23. import  android.widget.LinearLayout;  
  24. import  android.widget.RelativeLayout;     
  25.     
  26. public   class  ClockView  extends  Activity {     
  27.       
  28.     private   static  String TAG =  "ClockView" ;      
  29.     
  30.     private   final   int  FP = ViewGroup.LayoutParams.FILL_PARENT;  
  31.     private   final   int  WC = ViewGroup.LayoutParams.WRAP_CONTENT;  
  32.       
  33.     private  QAnalogClock clock1;          
  34.       
  35.     public   void  onCreate(Bundle savedInstanceState) {     
  36.         super .onCreate(savedInstanceState);     
  37.         LinearLayout lLayout = new  LinearLayout( this );  
  38.         lLayout.setLayoutParams(new  LinearLayout.LayoutParams(FP, FP));  
  39.         lLayout.setOrientation(LinearLayout.VERTICAL);  
  40.           
  41.         clock1 = new  QAnalogClock( this , "GMT+8:00" );      
  42.         lLayout.addView(clock1, new  LinearLayout.LayoutParams(WC,WC));            
  43.   
  44.         setContentView(lLayout);          
  45.           
  46.     }     
  47.     /*  
  48.      * Customized AnalogClock View  
  49.      * 1)Draw dial by bitmap  
  50.      * 2)Draw hour/minute/second by BitmapDrawable  
  51.      */   
  52.       
  53.     class  QAnalogClock  extends  View {     
  54.         //Original bitmap of dial,hour,minute,second   
  55.         Bitmap mBmpDial;    
  56.         Bitmap mBmpHour;     
  57.         Bitmap mBmpMinute;     
  58.         Bitmap mBmpSecond;  
  59.           
  60.         //Drawable of dial,hour,minute,second   
  61.         BitmapDrawable bmdHour;  
  62.         BitmapDrawable bmdMinute;  
  63.         BitmapDrawable bmdSecond;  
  64.         BitmapDrawable bmdDial;  
  65.           
  66.         Paint mPaint;//Paint draw on canvas   
  67.           
  68.         Handler tickHandler;          
  69.           
  70.         int  mWidth;    //Dial width   
  71.         int  mHeigh;    //Dial height   
  72.         int  mTempWidth;    //Hour/Minute/Second width   
  73.         int  mTempHeigh;    //Hour/Minute/Second height   
  74.         int  centerX ;  //Hour/Minute/Second picture center x     
  75.         int  centerY ;  //Hour/Minute/Second picture center y   
  76.           
  77.         int  availableWidth =  100 ; //Available width of the dial   
  78.         int  availableHeight =  100 ; //Available height of the dial   
  79.           
  80.         private  String sTimeZoneString;  
  81.           
  82.         public  QAnalogClock(Context context,String sTime_Zone) {     
  83.             super (context);     
  84.             sTimeZoneString = sTime_Zone;  
  85.               
  86.             mBmpHour = BitmapFactory.decodeResource(getResources(), R.drawable.clockdroid2_hour);     
  87.             bmdHour = new  BitmapDrawable(mBmpHour);  
  88.               
  89.             mBmpMinute = BitmapFactory.decodeResource(getResources(), R.drawable.clockdroid2_minute);    
  90.             bmdMinute = new  BitmapDrawable(mBmpMinute);  
  91.               
  92.             mBmpSecond = BitmapFactory.decodeResource(getResources(), R.drawable.clockdroid2_minute);    
  93.             bmdSecond = new  BitmapDrawable(mBmpSecond);  
  94.               
  95.             mBmpDial = BitmapFactory.decodeResource(getResources(), R.drawable.clockdroid2_dial);   
  96.             bmdDial = new  BitmapDrawable(mBmpDial);  
  97.             mWidth = mBmpDial.getWidth();  
  98.             mHeigh = mBmpDial.getHeight();  
  99.             centerX= availableWidth/2 ;  
  100.             centerY = availableHeight/2 ;  
  101.               
  102.             mPaint = new  Paint();     
  103.             mPaint.setColor(Color.BLUE);             
  104.             run();//Begin to send update event ,how to sync?   
  105.         }        
  106.     
  107.   
  108.     
  109.        public   void  run() {     
  110.            tickHandler = new  Handler();     
  111.            tickHandler.post(tickRunnable);      
  112.        }     
  113.    
  114.           
  115.         private  Runnable tickRunnable =  new  Runnable() {     
  116.             public   void  run() {     
  117.                 postInvalidate();   
  118.                 tickHandler.postDelayed(tickRunnable, 1000 );     
  119.             }     
  120.         };     
  121.    
  122.           
  123.         protected   void  onDraw(Canvas canvas) {     
  124.             super .onDraw(canvas);        
  125.     
  126.             Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(sTimeZoneString));     
  127.             int  hour = cal.get(Calendar.HOUR);     
  128.             int  minute = cal.get(Calendar.MINUTE);     
  129.             int  second = cal.get(Calendar.SECOND);  
  130.             float  hourRotate =  hour *  30 .0f + minute /  60 .0f *  30 .0f;     
  131.             float  minuteRotate = minute *  6 .0f;     
  132.             float  secondRotate = second *  6 .0f;  
  133.               
  134.          //   Log.d(TAG, "hour is "+hour+" minute is "+minute);   
  135.          //   Log.d(TAG, "hourRotate is "+hourRotate+" minuteRotate is "+minuteRotate+" secondRotate is "+secondRotate);   
  136.             boolean  scaled =  false ;  
  137.               
  138.             if  (availableWidth < mWidth || availableHeight < mHeigh) {  
  139.                 scaled = true ;  
  140.                 float  scale = Math.min(( float ) availableWidth / ( float ) mWidth,  
  141.                                        (float ) availableHeight / ( float ) mHeigh);  
  142.                 canvas.save();  
  143.                 canvas.scale(scale, scale, centerX, centerY);  
  144.             }  
  145.               
  146.           //  Log.d(TAG,"Canvas's height is "+canvas.getHeight() +" width is "+canvas.getWidth());   
  147.              
  148.             //Draw dial on view   
  149.             bmdDial.setBounds(centerX-(mWidth/2 ),centerY-(mHeigh/ 2 ),centerX+(mWidth/ 2 ),centerY+(mHeigh/ 2 ));              
  150.             bmdDial.draw(canvas);  
  151.               
  152.             //Draw hour on view   
  153.             mTempWidth = bmdHour.getIntrinsicWidth();  
  154.             mTempHeigh = bmdHour.getIntrinsicHeight();  
  155.             canvas.save();//Save non rotated canvas   
  156.             canvas.rotate(hourRotate,  centerX, centerY);         
  157.             bmdHour.setBounds(centerX-(mTempWidth/2 ),centerY-(mTempHeigh/ 2 ),centerX+(mTempWidth/ 2 ),centerY+(mTempHeigh/ 2 ));              
  158.             bmdHour.draw(canvas);     
  159.               
  160.             canvas.restore(); //restore canvas to last saved state          
  161.               
  162.             //Draw Minute on view   
  163.             mTempWidth = bmdMinute.getIntrinsicWidth();  
  164.             mTempHeigh = bmdMinute.getIntrinsicHeight();  
  165.             canvas.save();  
  166.             canvas.rotate(minuteRotate, centerX, centerY);          
  167.             bmdMinute.setBounds(centerX-(mTempWidth/2 ),centerY-(mTempHeigh/ 2 ),centerX+(mTempWidth/ 2 ),centerY+(mTempHeigh/ 2 ));          
  168.             bmdMinute.draw(canvas);    
  169.               
  170.              canvas.restore();     
  171.                
  172.             //Draw Second on view   
  173.             mTempWidth = bmdSecond.getIntrinsicWidth();  
  174.             mTempHeigh = bmdSecond.getIntrinsicHeight();                      
  175.             canvas.rotate(secondRotate,  centerX, centerY);        
  176.             bmdSecond.setBounds(centerX-(mTempWidth/2 ),centerY-(mTempHeigh/ 2 ),centerX+(mTempWidth/ 2 ),centerY+(mTempHeigh/ 2 ));              
  177.             bmdSecond.draw(canvas);   
  178.               
  179.             if  (scaled)  
  180.             {  
  181.                 canvas.restore();  
  182.             }  
  183.        }     
  184.     }     
  185. }   






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值