自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的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. STM32 软件模拟 IIC 代码,标准库、HAL库可用

    #ifndef _IIC_H #define _IIC_H #include "stdio.h" #include "stm32f1xx_hal.h" /* 定 ...

  2. Js原生实现抽奖功能

    <div>代码 按钮代码 JS原生代码  完整的代码: <div style="width:365px;height:300px;border:2px solid gree ...

  3. 【【henuacm2016级暑期训练】动态规划专题 N】Valid Sets

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给你一棵树. 让你统计其中子树T的数量. 这个子树T要满足最大值和最小值之差小于等于d 树形DP 可以枚举点root为子树的根. 统 ...

  4. RabbitMQ学习总结(2)——安装、配置与监控

    一.安装 1.安装Erlang 1)系统编译环境(这里采用linux/unix 环境) ① 安装环境 虚拟机:VMware® Workstation 10.0.1 build Linux系统:Cent ...

  5. COGS——T 438. 烦人的幻灯片

    http://www.cogs.pro/cogs/problem/problem.php?pid=438 ★☆   输入文件:slides.in   输出文件:slides.out   简单对比时间限 ...

  6. java语言MySQL批处理

    本质来讲就是使用Statement和PreStatement的addBatch()方法 代码 import java.sql.*; public class GetConnection{ public ...

  7. Qt 3D教程(三)实现对模型材质參数的控制

    Qt 3D教程(三)实现对模型材质參数的控制 蒋彩阳原创文章,首发地址:http://blog.csdn.net/gamesdev/article/details/47131841.欢迎同行前来探讨. ...

  8. GitHub客户端和Shell的基本操作和理解

    GitHub客户端和Shell指令的简单实用 客户端操作, web端操作, shell指令操作. 掌握了这三种操作,基本上就可以很好的运用gitHub了. 创建项目, 可以通过web端进行创建. 可以 ...

  9. Linux就该这么学 20181004(第六章磁盘管理)

    参考链接https://www.linuxprobe.com/ /boot 开机锁需要文件-内核.开机菜单以及所需配置文件 /dev 以文件形式存放的任何设备与接口 /etc 配置文件 /home 用 ...

  10. 138.安全退出的异常,要用throw 尽量不用exit(0)

    #include<iostream> #include<cstdlib> using namespace std; ////非安全退出,结束进程, //C++ 必须释放对象,最 ...