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

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

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