几天因工作忙,边写边做其它事情。发的贴子也越来越少。不过,我还在努力学习Android的基础知识与大家一起学习和进步。
下面我查了一下资料,编写了时钟源代码。先让我们看一下图先。

 

 

 

逻辑代码类

 

 
  
  1. package com.smart.impl; 
  2.  
  3. import java.util.Calendar; 
  4.  
  5. import android.content.Context; 
  6. import android.graphics.Bitmap; 
  7. import android.graphics.BitmapFactory; 
  8. import android.graphics.Canvas; 
  9. import android.graphics.Paint; 
  10. import android.graphics.Rect; 
  11. import android.os.Handler; 
  12. import android.util.AttributeSet; 
  13. import android.view.View; 
  14.  
  15. public class HandClock  extends View implements Runnable 
  16. { 
  17.     private int clockImageResourceId; 
  18.     private Bitmap bitmap; 
  19.     private float scale; 
  20.     private float handCenterWidthScale; 
  21.     private float handCenterHeightScale; 
  22.     private int minuteHandSize; 
  23.     private int hourHandSize; 
  24.     private Handler handler = new Handler(); 
  25.  
  26.     @Override 
  27.     public void run() 
  28.     { 
  29.         // 重新绘制View 
  30.         invalidate(); 
  31.         // 重新设置定时器,在60秒后调用run方法 
  32.         handler.postDelayed(this, 60 * 1000); 
  33.     } 
  34.  
  35.     @Override 
  36.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
  37.     { 
  38.         super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  39.         // 根据图像的实际大小等比例设置View的大小 
  40.         setMeasuredDimension((int) (bitmap.getWidth() * scale), (int) (bitmap 
  41.                 .getHeight() * scale)); 
  42.     } 
  43.  
  44.     @Override 
  45.     protected void onDraw(Canvas canvas) 
  46.     { 
  47.         super.onDraw(canvas); 
  48.         Paint paint = new Paint(); 
  49.         Rect src = new Rect(); 
  50.         Rect target = new Rect(); 
  51.         src.left = 0; 
  52.         src.top = 0; 
  53.         src.right = bitmap.getWidth(); 
  54.         src.bottom = bitmap.getHeight(); 
  55.  
  56.         target.left = 0; 
  57.         target.top = 0; 
  58.         target.bottom = (int) (src.bottom * scale); 
  59.         target.right = (int) (src.right * scale); 
  60.         // 画表盘图像 
  61.         canvas.drawBitmap(bitmap, src, target, paint); 
  62.         // 计算表盘中心点的横纵坐标 
  63.         float centerX = bitmap.getWidth() * scale * handCenterWidthScale; 
  64.         float centerY = bitmap.getHeight() * scale * handCenterHeightScale; 
  65.         // 表表盘中心点画一个半径为5的实心圆圈 
  66.         canvas.drawCircle(centerX, centerY, 5, paint); 
  67.         // 设置分针为3个象素粗 
  68.         paint.setStrokeWidth(3); 
  69.         Calendar calendar = Calendar.getInstance(); 
  70.         int currentMinute = calendar.get(Calendar.MINUTE); 
  71.         int currentHour = calendar.get(Calendar.HOUR); 
  72.         // 计算分针和时间的角度 
  73.         double minuteRadian = Math 
  74.                 .toRadians((360 - ((currentMinute * 6) - 90)) % 360); 
  75.         double hourRadian = Math.toRadians((360 - ((currentHour * 30) - 90)) 
  76.                 % 360 - (30 * currentMinute / 60)); 
  77.         // 在表盘上画分针 
  78.         canvas.drawLine(centerX, centerY, (int) (centerX + minuteHandSize 
  79.                 * Math.cos(minuteRadian)), (int) (centerY - minuteHandSize 
  80.                 * Math.sin(minuteRadian)), paint); 
  81.         // 设置实针为4个象素粗 
  82.         paint.setStrokeWidth(4); 
  83.         // 在表盘上画分针 
  84.         canvas.drawLine(centerX, centerY, (int) (centerX + hourHandSize 
  85.                 * Math.cos(hourRadian)), (int) (centerY - hourHandSize 
  86.                 * Math.sin(hourRadian)), paint); 
  87.     } 
  88.  
  89.     public HandClock(Context context, AttributeSet attrs) 
  90.     { 
  91.         super(context, attrs); 
  92.         // 读取相应的属性值 
  93.         clockImageResourceId = attrs.getAttributeResourceValue(null, 
  94.                 "clockImageSrc", 0); 
  95.         if (clockImageResourceId > 0) 
  96.             bitmap = BitmapFactory.decodeResource(getResources(), 
  97.                     clockImageResourceId); 
  98.         scale = attrs.getAttributeFloatValue(null, "scale", 1); 
  99.         handCenterWidthScale = attrs.getAttributeFloatValue(null, 
  100.                 "handCenterWidthScale", bitmap.getWidth() / 2); 
  101.         handCenterHeightScale = attrs.getAttributeFloatValue(null, 
  102.                 "handCenterHeightScale", bitmap.getHeight() / 2); 
  103.         //  在读取分针和时针长度后,将其值按图像的缩放比例进行缩放 
  104.         minuteHandSize = (int) (attrs.getAttributeIntValue(null, 
  105.                 "minuteHandSize", 0) * scale); 
  106.         hourHandSize = (int) (attrs.getAttributeIntValue(null, "hourHandSize", 
  107.                 0) * scale); 
  108.         int currentSecond = Calendar.getInstance().get(Calendar.SECOND); 
  109.         //  将定时器设在0分时执行run方法 
  110.         handler.postDelayed(this, (60 - currentSecond) * 1000); 
  111.     } 
  112.  
  113.     @Override 
  114.     protected void onDetachedFromWindow() 
  115.     { 
  116.         super.onDetachedFromWindow(); 
  117.         //  删除回调类 
  118.         handler.removeCallbacks(this); 
  119.     } 
  120.  
  121. } 

关于源代码,由于只能上传2M,上传不上,如果有朋友想要的话。请到

http://dev.10086.cn/cmdn/supesite/?uid-2062537-action-viewspace-itemid-6971  下载