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

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

package com.example.chl.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View; /**
* Created by chl on 16-3-30.
*/
public class TimeVIew extends View {
private Paint mPaint;
// private Bitmap bitmap = null;
private Bitmap ssBitmap = null;
private Bitmap sssBitmap = null;
private Bitmap mmBitmap = null;
private Bitmap mmmBitmap = null;
// private int x;
// private int y;
private int ssx;
private int ssy;
private int mmx;
private int mmy;
private Context mContext;
private Matrix matrix = null;
private Matrix mmatrix = null;
private float angle = 0;//秒针每秒偏移的角度
private float mangle = 0;//分针每秒偏移的角度
private MyThread myThread; public TimeVIew(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initBitmap(); } public TimeVIew(Context context) {
super(context);
this.mContext = context;
initBitmap();
} public TimeVIew(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
initBitmap(); } private void initBitmap() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
// bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.global_dial_day_bg,options);
ssBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.global_second_day_small);
mmBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.global_minute_day_small);
// x = bitmap.getWidth();
// y = bitmap.getHeight();
ssx = ssBitmap.getWidth();//获取bitmap的宽度
ssy = ssBitmap.getHeight();
mmx = mmBitmap.getWidth();
mmy = mmBitmap.getHeight();
matrix = new Matrix();
matrix.setRotate(angle);//设置图片旋转角度
mmatrix = new Matrix();
matrix.setRotate(mangle);
sssBitmap = Bitmap.createBitmap(ssBitmap, 0, 0, ssx, ssy, matrix, true);//创建新的bitmap
mmmBitmap = Bitmap.createBitmap(mmBitmap, 0, 0, mmx, mmy, mmatrix, true);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawBitmap(bitmap, Util.getDeviceWidth(mContext) / 2 - x / 2, Util.getDeviceHeight(mContext) / 2 - y / 2, mPaint); if (myThread == null) {
myThread = new MyThread();
myThread.start();
}
canvas.drawBitmap(mmmBitmap, getWidth() / 2 - mmmBitmap.getWidth() / 2, getHeight() / 2 - mmmBitmap.getHeight() / 2, mPaint);
canvas.drawBitmap(sssBitmap, getWidth() / 2 - sssBitmap.getWidth() / 2, getHeight() / 2 - sssBitmap.getHeight() / 2, mPaint); } class MyThread extends Thread {
private int num=0; @Override
public void run() {
while (true) {
if (angle == 360) {
angle = 0;
}
if (mangle == 360) {
mangle = 0;
} matrix.setRotate(angle);
sssBitmap = Bitmap.createBitmap(ssBitmap, 0, 0, ssx, ssy, matrix, true);
mmatrix.setRotate(mangle);
mmmBitmap = Bitmap.createBitmap(mmBitmap, 0, 0, mmx, mmy, mmatrix, true);
angle += 6;
if (num%5==0) {//控制分针五秒移动一个角度,也能够每秒都让其移动
mangle += 0.5; }
num++;
if (num==200){
num=0;
}
postInvalidate();//又一次绘制
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
} }

主布局文件:

<?

xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/global_dial_day_bg"
/>
<com.example.chl.myapplication.TimeVIew android:layout_width="match_parent"
android:layout_height="match_parent" /> </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. 单元测试Struts2Spring项目的Action和Service(包含源码)

    最近,认真实践了单元测试Struts2.Spring等Java项目,今天特意写的是单元测试Struts2Spring项目的Action和Service. 由于已经写过不少Web开发框架单元测试的代码, ...

  2. %02x与%2x之间的区别

    输出最小宽度用十进制整数来表示输出的最少位数.若实际位数多于定义的宽度,则按实际位数输出,若实际位数少于定义的宽度则补以空格或0(当最小宽度数值以0开头时). X 表示以十六进制形式输出02表示不足两 ...

  3. tcpdump的使用以及参数详解

    平时分析客户端和服务器网络交互的问题时,很多情况下需要在客户端和服务器抓包分析报文.一般win下抓包使用WireShark即可,但是linux下就需要用到tcpdump了,下面是一些对于tcpdump ...

  4. HAVING使用子查询

    HAVING使用子查询     //查询各部门平均工资,显示平均工资大于   //公司整体平均工资的记录   select deptno,avg(sal)   from emp   group by ...

  5. synchronized的实现原理及锁优化

    记得刚刚开始学习Java的时候,一遇到多线程情况就是synchronized.对于当时的我们来说,synchronized是如此的神奇且强大.我们赋予它一个名字“同步”,也成为我们解决多线程情况的良药 ...

  6. Android开发之AlarmManager具体解释

    AlarmManager实质是一个全局的定时器,是Android中经常使用的一种系统级别的提示服务,在指定时间或周期性启动其他组件(包含Activity,Service,BroadcastReceiv ...

  7. Hibernate的多种关系映射(oto、otm、mtm)

    前提:使用注解映射 一.一对一(夫妻关系表) 两个表:hus1和wife1表,外键为id,各自有名字hname和wname 映射得到两个类:Hus1和Wife1类 Hus1类(主表): package ...

  8. Linux下处理JSON的命令行工具:jq---安装

    转自:https://blog.csdn.net/Sunny_much/article/details/50668871      JSON是前端编程经常用到的格式.Linux下也有处理处理JSON的 ...

  9. BZOJ 3339 线段树

    思路: 考虑离线处理 显然 l固定时 r越大 ans越大 那我们不妨按照l从小到大排序 l->l+1的时候 l到next[l]这段区间都跟a[l]取min就好了 搞颗线段树维护一下 //By S ...

  10. Ubuntu16.04下Mongodb官网卸载部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 前期博客 Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐) https://docs.mongodb.com/manual/tutorial/ins ...