Android新手入门实例之Android漂亮时钟的源代码
来源:互联网 发布:好八字是什么 知乎 编辑:程序博客网 时间:2024/06/01 22:57
- package com.smart.impl;
- import java.util.Calendar;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.os.Handler;
- import android.util.AttributeSet;
- import android.view.View;
- public class HandClock extends View implements Runnable
- {
- private int clockImageResourceId;
- private Bitmap bitmap;
- private float scale;
- private float handCenterWidthScale;
- private float handCenterHeightScale;
- private int minuteHandSize;
- private int hourHandSize;
- private Handler handler = new Handler();
- @Override
- public void run()
- {
- // 重新绘制View
- invalidate();
- // 重新设置定时器,在60秒后调用run方法
- handler.postDelayed(this, 60 * 1000);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
- {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- // 根据图像的实际大小等比例设置View的大小
- setMeasuredDimension((int) (bitmap.getWidth() * scale), (int) (bitmap
- .getHeight() * scale));
- }
- @Override
- protected void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
- Paint paint = new Paint();
- Rect src = new Rect();
- Rect target = new Rect();
- src.left = 0;
- src.top = 0;
- src.right = bitmap.getWidth();
- src.bottom = bitmap.getHeight();
- target.left = 0;
- target.top = 0;
- target.bottom = (int) (src.bottom * scale);
- target.right = (int) (src.right * scale);
- // 画表盘图像
- canvas.drawBitmap(bitmap, src, target, paint);
- // 计算表盘中心点的横纵坐标
- float centerX = bitmap.getWidth() * scale * handCenterWidthScale;
- float centerY = bitmap.getHeight() * scale * handCenterHeightScale;
- // 表表盘中心点画一个半径为5的实心圆圈
- canvas.drawCircle(centerX, centerY, 5, paint);
- // 设置分针为3个象素粗
- paint.setStrokeWidth(3);
- Calendar calendar = Calendar.getInstance();
- int currentMinute = calendar.get(Calendar.MINUTE);
- int currentHour = calendar.get(Calendar.HOUR);
- // 计算分针和时间的角度
- double minuteRadian = Math
- .toRadians((360 - ((currentMinute * 6) - 90)) % 360);
- double hourRadian = Math.toRadians((360 - ((currentHour * 30) - 90))
- % 360 - (30 * currentMinute / 60));
- // 在表盘上画分针
- canvas.drawLine(centerX, centerY, (int) (centerX + minuteHandSize
- * Math.cos(minuteRadian)), (int) (centerY - minuteHandSize
- * Math.sin(minuteRadian)), paint);
- // 设置实针为4个象素粗
- paint.setStrokeWidth(4);
- // 在表盘上画分针
- canvas.drawLine(centerX, centerY, (int) (centerX + hourHandSize
- * Math.cos(hourRadian)), (int) (centerY - hourHandSize
- * Math.sin(hourRadian)), paint);
- }
- public HandClock(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- // 读取相应的属性值
- clockImageResourceId = attrs.getAttributeResourceValue(null,
- "clockImageSrc", 0);
- if (clockImageResourceId > 0)
- bitmap = BitmapFactory.decodeResource(getResources(),
- clockImageResourceId);
- scale = attrs.getAttributeFloatValue(null, "scale", 1);
- handCenterWidthScale = attrs.getAttributeFloatValue(null,
- "handCenterWidthScale", bitmap.getWidth() / 2);
- handCenterHeightScale = attrs.getAttributeFloatValue(null,
- "handCenterHeightScale", bitmap.getHeight() / 2);
- // 在读取分针和时针长度后,将其值按图像的缩放比例进行缩放
- minuteHandSize = (int) (attrs.getAttributeIntValue(null,
- "minuteHandSize", 0) * scale);
- hourHandSize = (int) (attrs.getAttributeIntValue(null, "hourHandSize",
- 0) * scale);
- int currentSecond = Calendar.getInstance().get(Calendar.SECOND);
- // 将定时器设在0分时执行run方法
- handler.postDelayed(this, (60 - currentSecond) * 1000);
- }
- @Override
- protected void onDetachedFromWindow()
- {
- super.onDetachedFromWindow();
- // 删除回调类
- handler.removeCallbacks(this);
- }}
- Android新手入门实例之Android漂亮时钟的源代码
- [Android实例] 自定义漂亮的圆形进度条
- 新手入门Android之XML
- Android之新手入门篇
- iOS动画之漂亮的时钟
- Android新手入门的十個問題!
- 我的Android进阶之旅------>Android Widget 桌面数字时钟(DigtalClockWidget)实例
- Android之制作漂亮的缓存界面
- Android新手入门进阶之路
- android 漂亮的listview
- 漂亮的时钟
- 漂亮的SVG时钟
- Android 新手入门
- Android 之 时钟组件
- Android实现模拟时钟(简单+漂亮)--时针、分针、秒针
- Android时钟的widget
- Android:制作漂亮的菜单
- Android自定义漂亮的Dialog
- 净(负)荷概念,osi封装
- 九度OJ 清华12真题之广度优先搜索之《玛雅密码》
- 协同过滤介绍和简单推荐系统的实现
- linux minicom配置
- 自动取款机
- Android新手入门实例之Android漂亮时钟的源代码
- http transaction session
- SQL 优化34条建议
- js监听键盘动作 .
- Java容器的线程安全
- 软件系统性能的常见指标
- http 状态码
- 再次写给我们这些浮躁的程序员
- google nexus 手机4.0.4 , sdcard.c分析