自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的view里面,在view里面仅仅须要绘制秒针和分针时针并控其转动就可以。

下面就是自己定义view的主要代码:

  1. package com.example.chl.myapplication;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Matrix;
  8. import android.graphics.Paint;
  9. import android.util.AttributeSet;
  10. import android.view.View;
  11.  
  12. /**
  13. * Created by chl on 16-3-30.
  14. */
  15. public class TimeVIew extends View {
  16. private Paint mPaint;
  17. // private Bitmap bitmap = null;
  18. private Bitmap ssBitmap = null;
  19. private Bitmap sssBitmap = null;
  20. private Bitmap mmBitmap = null;
  21. private Bitmap mmmBitmap = null;
  22. // private int x;
  23. // private int y;
  24. private int ssx;
  25. private int ssy;
  26. private int mmx;
  27. private int mmy;
  28. private Context mContext;
  29. private Matrix matrix = null;
  30. private Matrix mmatrix = null;
  31. private float angle = 0;//秒针每秒偏移的角度
  32. private float mangle = 0;//分针每秒偏移的角度
  33. private MyThread myThread;
  34.  
  35. public TimeVIew(Context context, AttributeSet attrs) {
  36. super(context, attrs);
  37. this.mContext = context;
  38. initBitmap();
  39.  
  40. }
  41.  
  42. public TimeVIew(Context context) {
  43. super(context);
  44. this.mContext = context;
  45. initBitmap();
  46. }
  47.  
  48. public TimeVIew(Context context, AttributeSet attrs, int defStyleAttr) {
  49. super(context, attrs, defStyleAttr);
  50. this.mContext = context;
  51. initBitmap();
  52.  
  53. }
  54.  
  55. private void initBitmap() {
  56. mPaint = new Paint();
  57. mPaint.setAntiAlias(true);
  58. BitmapFactory.Options options = new BitmapFactory.Options();
  59. options.inSampleSize = 2;
  60. // bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.global_dial_day_bg,options);
  61. ssBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.global_second_day_small);
  62. mmBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.global_minute_day_small);
  63. // x = bitmap.getWidth();
  64. // y = bitmap.getHeight();
  65. ssx = ssBitmap.getWidth();//获取bitmap的宽度
  66. ssy = ssBitmap.getHeight();
  67. mmx = mmBitmap.getWidth();
  68. mmy = mmBitmap.getHeight();
  69. matrix = new Matrix();
  70. matrix.setRotate(angle);//设置图片旋转角度
  71. mmatrix = new Matrix();
  72. matrix.setRotate(mangle);
  73. sssBitmap = Bitmap.createBitmap(ssBitmap, 0, 0, ssx, ssy, matrix, true);//创建新的bitmap
  74. mmmBitmap = Bitmap.createBitmap(mmBitmap, 0, 0, mmx, mmy, mmatrix, true);
  75. }
  76.  
  77. @Override
  78. protected void onDraw(Canvas canvas) {
  79. super.onDraw(canvas);
  80. //canvas.drawBitmap(bitmap, Util.getDeviceWidth(mContext) / 2 - x / 2, Util.getDeviceHeight(mContext) / 2 - y / 2, mPaint);
  81.  
  82. if (myThread == null) {
  83. myThread = new MyThread();
  84. myThread.start();
  85. }
  86. canvas.drawBitmap(mmmBitmap, getWidth() / 2 - mmmBitmap.getWidth() / 2, getHeight() / 2 - mmmBitmap.getHeight() / 2, mPaint);
  87. canvas.drawBitmap(sssBitmap, getWidth() / 2 - sssBitmap.getWidth() / 2, getHeight() / 2 - sssBitmap.getHeight() / 2, mPaint);
  88.  
  89. }
  90.  
  91. class MyThread extends Thread {
  92. private int num=0;
  93.  
  94. @Override
  95. public void run() {
  96. while (true) {
  97. if (angle == 360) {
  98. angle = 0;
  99. }
  100. if (mangle == 360) {
  101. mangle = 0;
  102. }
  103.  
  104. matrix.setRotate(angle);
  105. sssBitmap = Bitmap.createBitmap(ssBitmap, 0, 0, ssx, ssy, matrix, true);
  106. mmatrix.setRotate(mangle);
  107. mmmBitmap = Bitmap.createBitmap(mmBitmap, 0, 0, mmx, mmy, mmatrix, true);
  108. angle += 6;
  109. if (num%5==0) {//控制分针五秒移动一个角度,也能够每秒都让其移动
  110. mangle += 0.5;
  111.  
  112. }
  113. num++;
  114. if (num==200){
  115. num=0;
  116. }
  117. postInvalidate();//又一次绘制
  118. try {
  119. Thread.sleep(1000);
  120. } catch (InterruptedException e) {
  121. e.printStackTrace();
  122. }
  123.  
  124. }
  125. }
  126. }
  127.  
  128. }

主布局文件:

  1. <?
  2.  
  3. xml version="1.0" encoding="utf-8"?>
  4. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:background="#000000"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. >
  10. <ImageView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:src="@drawable/global_dial_day_bg"
  14. />
  15. <com.example.chl.myapplication.TimeVIew
  16.  
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent" />
  19.  
  20. </FrameLayout>

图片资源下载:http://download.csdn.net/detail/cao185493676/9482843

android自己定义圆盘时钟的更多相关文章

  1. 【FZSZ2017暑假提高组Day2】圆盘时钟

    [问题描述] 作为出题人的小Z相信大家对上图这样的圆盘时钟都不会陌生——在理想圆盘时钟上,秒针每一分钟转一圈,分针每一小时转一圈,时针每12小时转一圈,它们均是匀速转动的,在0点时三条针均指向表盘上的 ...

  2. Android自己定义DataTimePicker(日期选择器)

    Android自己定义DataTimePicker(日期选择器)  笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...

  3. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  4. Android自己定义组件系列【7】——进阶实践(4)

    上一篇<Android自己定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...

  5. ANDROID自己定义视图——onLayout源代码 流程 思路具体解释

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...

  6. Android 自己定义View (二) 进阶

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...

  7. Android 自己定义ScrollView ListView 体验各种纵向滑动的需求

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38950509.本文出自[张鸿洋的博客] 1.概述 群里的一个哥们有个需求是这种: ...

  8. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  9. 15个超强悍的CSS3圆盘时钟动画赏析

    在网页上,特别是个人博客中经常会用到时钟插件,一款个性化的时钟插件不仅可以让页面显得美观,而且可以让访客看到当前的日期和时间.今天我们给大家收集了15个超强悍的圆盘时钟动画,很多都是基于CSS3,也有 ...

随机推荐

  1. 小白神器 - 两篇博客读懂JavaScript (一基础篇)

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一. 编写格式 1, ...

  2. Tire树总结(模板+例题)

    题目来自<算法竞赛设计指南> Tire树是一种可以快速查找字符串的数据结构 模板 #include<cstdio> #include<algorithm> #inc ...

  3. Struts2SpringHibernate整合示例,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)

    Struts2,Spring,Hibernate是Java Web开发中最为常见的3种框架,掌握这3种框架是每个Java Web开发人员的基本功. 然而,很多初学者在集成这3个框架的时候,总是会遇到各 ...

  4. Nuxt开发经验分享

    Nuxt开发经验分享 本文章基于starter-template模板进行讲解,面向有vue-cli开发经验的宝宝 vue init nuxt-community/starter-template   ...

  5. 【codeforces 229C】Triangles

    [题目链接]:http://codeforces.com/problemset/problem/229/C [题意] 给你一张完全图; 然后1个人从中选择m条边; 然后另外一个人从中选择剩余的n*(n ...

  6. android studio2.2 配置NDK

    1.配置环境: Android studio2.2 配置NDK NDK版本[android-ndk-r13b-windows-x86_64.zip] NDK下载网址:[https://dl.googl ...

  7. 【转】Visual Studio單元測試小應用-測執行時間

    [转]Visual Studio單元測試小應用-測執行時間 Visual Studio的單元測試會記錄每一個測試的執行時間,如果有幾個Method要測效能,以前我會用Stopwatch,最近我都改用單 ...

  8. myeclipse导入工程 Some projects cannot be imported because they already exist in the workspace

    问题描述: 1 第一次从外部导入工程或者新建工程,成功: 2 删除该工程,但是没有选择delete project contents on disk 3 再次需要该工程,导入该工程时出现警告:Some ...

  9. ZOJ 2705

    这题,找找规律,可以发现一个斐波那契数列.按照斐波那契数列求和,知道, SUM=Fn+2-F1,于是,该长度为Fn+2的倍数.因为斐波那契数列不一定是从1开始的,而从2开始的每个数都是从1开始的倍数. ...

  10. 杭电1596 find the safest road

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...