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,也有 ...
随机推荐
- LaTeX 图片色偏解决方法
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50327113 在LaTeX的编辑模式中 ...
- 洛谷1387 二维dp 不是特别简略的题解 智商题
洛谷1387 dp题目,刚开始写的时候使用了前缀和加搜索,复杂度大概在O(n ^ 3)级别,感觉这么写还是比较对得起普及/提高-的难度的..后来看了题解区各位大神的题解,开始一脸mb,之后备受启发. ...
- call to OpenGL ES API with no current context 和Fatal signal 11
近日在用cocos2dx3.4的时候使用了JNI调用,发现一个现象 当不使用jni的时候全然正常.使用了jni后回去的全部文字都变成黑块,而且有概率程序崩溃.附带出了两个log call to Ope ...
- Spring表达式语言SpEL简单介绍
Spring3引入了Spring表达式语言(Spring Expression Language,SpEL). SpEL有非常多特性.比較经常使用的包含: 1.使用bean的id来引用bean, 以下 ...
- log4j.propertie配置具体解释
1.log4j.rootCategory=INFO, stdout , R 此句为将等级为INFO的日志信息输出到stdout和R这两个目的地,stdout和R的定义在以下的代码,能够随意起名.等级可 ...
- String转换成int型
private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...
- sql/plus无法显示数据库问题
登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ( ...
- Spark SQL概念学习系列之Spark SQL概述
很多人一个误区,Spark SQL重点不是在SQL啊,而是在结构化数据处理! Spark SQL结构化数据处理 概要: 01 Spark SQL概述 02 Spark SQL基本原理 03 Spark ...
- Linux java9 jshell操作
1.上传 2.解压 配不配环境变量都行 进入到jdk-9.0.4的bin目录下 执行./jshell命令 我第一次出现如下的情况 等了一会没反应就ctrl+z了.然后又重新执行./shell命令 超时 ...
- SqlServer存储过程加密与解密
★ 加密存储过程 ★: IF EXISTS (SELECT name FROM sysobjects WHERE name = 'encrypt_this' AND type = 'P') DRO ...