package com.duke.myclockview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import java.util.Calendar;
/**
* Author: duke
* Date: 2016-04-28 19:07
* Description: 模拟时钟
*/
public class MyClock extends View {
//画圆画线的画笔
private Paint mPaint;
//画文本的画笔
private Paint mTextPaint;
private Rect textBound;
private int bgResId = -1;//背景图片资源ref
private int bgColor = Color.WHITE;//背景颜色color,ref
private int outCircleColor = Color.RED;//外圆的颜色color,ref
private int smallDegreeColor = Color.GRAY;//小刻度的颜色color,ref
private int bigDegreeColor = Color.BLUE;//大刻度的颜色color,ref
private int textColor = Color.RED;//字体颜色color,ref
private int hourPointerColor = Color.parseColor("#ff6100");//时针颜色color,ref
private int minutePointerColor = Color.MAGENTA;//分针颜色color,ref
private int secondPointerColor = Color.BLACK;//秒针颜色color,ref
private int centerCircleColor = Color.GREEN;//中心点颜色color,ref
//外圆宽度
private int outCircleWidth = getContext().getResources().getDimensionPixelSize(R.dimen.dp_6);
//小刻度宽度
private int smallDegreeWidth = getContext().getResources().getDimensionPixelSize(R.dimen.dp_4);
//小刻度长度
private int smallDegreeLength = getContext().getResources().getDimensionPixelSize(R.dimen.dp_15);
//大刻度宽度
private int bigDegreeWidth = getContext().getResources().getDimensionPixelSize(R.dimen.dp_6);
//大刻度长度
private int bigDegreeLength = getContext().getResources().getDimensionPixelSize(R.dimen.dp_25);
//时针宽度
private int hourPointerWidth = getContext().getResources().getDimensionPixelSize(R.dimen.dp_10);
//时针长度
private int hourPointerLength = getContext().getResources().getDimensionPixelSize(R.dimen.dp_75);
//分针宽度
private int minutePointerWidth = getContext().getResources().getDimensionPixelSize(R.dimen.dp_6);
//分针长度
private int minutePointerLength = getContext().getResources().getDimensionPixelSize(R.dimen.dp_105);
//秒针宽度
private int secondPointerWidth = getContext().getResources().getDimensionPixelSize(R.dimen.dp_3);
//秒针长度
private int secondPointerLength = getContext().getResources().getDimensionPixelSize(R.dimen.dp_140);
//中心圆大小
private int centerCircleRadius = getContext().getResources().getDimensionPixelSize(R.dimen.dp_8);
private int textSize = 16;//字体大小
//获取当前的时分秒
private int hour;
private int minute;
private int second;
private StringBuilder timeString;//HH:mm:ss
public MyClock(Context context) {
this(context, null, 0);
}
public MyClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyClock(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取属性值
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyClock, defStyleAttr, 0);
int count = array.getIndexCount();
for (int i = 0; i < count; i++) {
int attr = array.getIndex(i);
switch (attr) {
case R.styleable.MyClock_bgResId:
bgResId = array.getResourceId(attr, -1);
break;
case R.styleable.MyClock_bgColor:
bgColor = array.getColor(attr, Color.WHITE);
break;
case R.styleable.MyClock_outCircleColor:
outCircleColor = array.getColor(attr, Color.RED);
break;
case R.styleable.MyClock_smallDegreeColor:
smallDegreeColor = array.getColor(attr, Color.GRAY);
break;
case R.styleable.MyClock_bigDegreeColor:
bigDegreeColor = array.getColor(attr, Color.BLUE);
break;
case R.styleable.MyClock_textColor:
textColor = array.getColor(attr, Color.RED);
break;
case R.styleable.MyClock_hourPointerColor:
hourPointerColor = array.getColor(attr, Color.DKGRAY);
break;
case R.styleable.MyClock_minutePointerColor:
minutePointerColor = array.getColor(attr, Color.MAGENTA);
break;
case R.styleable.MyClock_secondPointerColor:
secondPointerColor = array.getColor(attr, Color.BLACK);
break;
case R.styleable.MyClock_centerCircleColor:
centerCircleColor = array.getColor(attr, Color.YELLOW);
break;
}
}
array.recycle();
//获取默认字体大小
textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, getResources().getDisplayMetrics());
mPaint = new Paint();
mTextPaint = new Paint();
mTextPaint.setColor(textColor);
mTextPaint.setTextSize(textSize);
textBound = new Rect();
}
//计算时分秒
private void computTime() {
Calendar calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
//拼接 HH:mm:ss时间串
timeString = new StringBuilder();
if (hour < 10) {
timeString.append("0");
}
timeString.append(hour);
timeString.append(":");
if (minute < 10) {
timeString.append("0");
}
timeString.append(minute);
timeString.append(":");
if (second < 10) {
timeString.append("0");
}
timeString.append(second);
}
/**
* 测量,确定view最终的宽高
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int targetWidthHeight = Math.min(widthSize, heightSize);
//取正方形
setMeasuredDimension(targetWidthHeight, targetWidthHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取时间
computTime();
//绘制背景
if (bgResId > 0) {
setBackgroundResource(bgResId);
} else {
//颜色值可以是负数,故不用判断颜色值是否大于0
setBackgroundColor(bgColor);
}
//画大圆
mPaint.reset();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(outCircleColor);
mPaint.setStrokeWidth(outCircleWidth);
//设置画笔空心
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getMeasuredWidth() >> 1, getMeasuredHeight() >> 1, (getMeasuredWidth() >> 1) - (outCircleWidth >> 1), mPaint);
//画小刻度(总共60个)
mPaint.setColor(smallDegreeColor);
mPaint.setStrokeWidth(smallDegreeWidth);
//刻度半径
float degreeRadius = (getMeasuredWidth() >>
没有合适的资源?快使用搜索试试~ 我知道了~
Android自定义模拟时钟
共81个文件
xml:36个
bin:10个
lock:7个
需积分: 37 12 下载量 152 浏览量
2016-09-13
23:26:42
上传
评论
收藏 256KB RAR 举报
温馨提示
最近研究自定义控件一段时间了,看到网上有自定义模拟时钟效果,于是自已本着已于技术试了下,终于搞定了,特上传代码,一起分享。更多详细解释请关注我的csdn:http://blog.csdn.net/fesdgasdgasdg?viewmode=contents
资源推荐
资源详情
资源评论
收起资源包目录
MyClockView.rar (81个子文件)
MyClockView
gradlew 5KB
settings.gradle 16B
.idea
libraries
junit_4_12.xml 477B
support_compat_24_2_0.xml 798B
support_v4_24_2_0.xml 420B
appcompat_v7_24_2_0.xml 593B
support_media_compat_24_2_0.xml 834B
support_annotations_24_2_0.xml 511B
support_core_ui_24_2_0.xml 804B
hamcrest_core_1_3.xml 526B
animated_vector_drawable_24_2_0.xml 653B
support_fragment_24_2_0.xml 810B
support_vector_drawable_24_2_0.xml 648B
support_core_utils_24_2_0.xml 822B
runConfigurations.xml 564B
misc.xml 2KB
compiler.xml 686B
workspace.xml 172KB
gradle.xml 739B
.name 11B
encodings.xml 159B
modules.xml 359B
copyright
profiles_settings.xml 76B
gradle
wrapper
gradle-wrapper.properties 239B
gradle-wrapper.jar 52KB
MyClockView.iml 942B
.gradle
2.14.1
tasks
_app_compileDebugAndroidTestJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 18KB
localClassSetAnalysis.lock 17B
_app_compileDebugUnitTestJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 18KB
localClassSetAnalysis.lock 17B
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 50KB
localClassSetAnalysis.lock 17B
taskArtifacts
fileSnapshotsToTreeSnapshotsIndex.bin 29KB
cache.properties.lock 17B
fileHashes.bin 137KB
taskArtifacts.bin 153KB
fileSnapshots.bin 5.05MB
cache.properties 31B
local.properties 435B
gradlew.bat 2KB
gradle.properties 872B
.gitignore 105B
app
src
androidTest
java
com
duke
myclockview
ApplicationTest.java 363B
test
java
com
duke
myclockview
ExampleUnitTest.java 327B
main
screenMatchDP.jar 7KB
screenMatchDP.bat 44B
AndroidManifest.xml 679B
res
mipmap-hdpi
ic_launcher.png 3KB
values-w533dp
dimens.xml 8KB
values-w411dp
dimens.xml 8KB
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
mipmap-mdpi
ic_launcher.png 2KB
drawable
layout
activity_main.xml 458B
values-w768dp
dimens.xml 8KB
values-w820dp
dimens.xml 8KB
values
colors.xml 214B
attrs.xml 771B
strings.xml 77B
styles.xml 394B
dimens.xml 8KB
values-w640dp
dimens.xml 8KB
values-w384dp
dimens.xml 8KB
values-w720dp
dimens.xml 8KB
mipmap-xxhdpi
ic_launcher.png 8KB
bg.jpg 7KB
values-w400dp
dimens.xml 8KB
values-w960dp
dimens.xml 8KB
java
com
duke
myclockview
MyClock.java 13KB
MainActivity.java 346B
libs
app.iml 11KB
.gitignore 8B
build.gradle 647B
proguard-rules.pro 670B
build.gradle 521B
共 81 条
- 1
资源评论
android阿杜
- 粉丝: 424
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功