android自己定义圆盘时钟
自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的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自己定义圆盘时钟的更多相关文章
- 【FZSZ2017暑假提高组Day2】圆盘时钟
[问题描述] 作为出题人的小Z相信大家对上图这样的圆盘时钟都不会陌生——在理想圆盘时钟上,秒针每一分钟转一圈,分针每一小时转一圈,时针每12小时转一圈,它们均是匀速转动的,在0点时三条针均指向表盘上的 ...
- Android自己定义DataTimePicker(日期选择器)
Android自己定义DataTimePicker(日期选择器) 笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- Android自己定义组件系列【7】——进阶实践(4)
上一篇<Android自己定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...
- ANDROID自己定义视图——onLayout源代码 流程 思路具体解释
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候.事实上非常easy.仅仅须要知道3步骤: 1.測量- ...
- Android 自己定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...
- Android 自己定义ScrollView ListView 体验各种纵向滑动的需求
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38950509.本文出自[张鸿洋的博客] 1.概述 群里的一个哥们有个需求是这种: ...
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- 15个超强悍的CSS3圆盘时钟动画赏析
在网页上,特别是个人博客中经常会用到时钟插件,一款个性化的时钟插件不仅可以让页面显得美观,而且可以让访客看到当前的日期和时间.今天我们给大家收集了15个超强悍的圆盘时钟动画,很多都是基于CSS3,也有 ...
随机推荐
- 小白神器 - 两篇博客读懂JavaScript (一基础篇)
JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一. 编写格式 1, ...
- Tire树总结(模板+例题)
题目来自<算法竞赛设计指南> Tire树是一种可以快速查找字符串的数据结构 模板 #include<cstdio> #include<algorithm> #inc ...
- Struts2SpringHibernate整合示例,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)
Struts2,Spring,Hibernate是Java Web开发中最为常见的3种框架,掌握这3种框架是每个Java Web开发人员的基本功. 然而,很多初学者在集成这3个框架的时候,总是会遇到各 ...
- Nuxt开发经验分享
Nuxt开发经验分享 本文章基于starter-template模板进行讲解,面向有vue-cli开发经验的宝宝 vue init nuxt-community/starter-template ...
- 【codeforces 229C】Triangles
[题目链接]:http://codeforces.com/problemset/problem/229/C [题意] 给你一张完全图; 然后1个人从中选择m条边; 然后另外一个人从中选择剩余的n*(n ...
- android studio2.2 配置NDK
1.配置环境: Android studio2.2 配置NDK NDK版本[android-ndk-r13b-windows-x86_64.zip] NDK下载网址:[https://dl.googl ...
- 【转】Visual Studio單元測試小應用-測執行時間
[转]Visual Studio單元測試小應用-測執行時間 Visual Studio的單元測試會記錄每一個測試的執行時間,如果有幾個Method要測效能,以前我會用Stopwatch,最近我都改用單 ...
- myeclipse导入工程 Some projects cannot be imported because they already exist in the workspace
问题描述: 1 第一次从外部导入工程或者新建工程,成功: 2 删除该工程,但是没有选择delete project contents on disk 3 再次需要该工程,导入该工程时出现警告:Some ...
- ZOJ 2705
这题,找找规律,可以发现一个斐波那契数列.按照斐波那契数列求和,知道, SUM=Fn+2-F1,于是,该长度为Fn+2的倍数.因为斐波那契数列不一定是从1开始的,而从2开始的每个数都是从1开始的倍数. ...
- 杭电1596 find the safest road
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...