我想用源$ C $ AnalogClock的c。通过Android开源项目可用在互联网做一些针对性的AnalogClock类。
我想使时钟设置,我想,而不是当前的时间。我没有找到如何做到这一点,所以也许这篇文章将是有益的一个明显的例子。复制源$ C $ C到新的文件后,我得到了一些错误。下面是AnalogClock原始出处code:
/ *
*版权所有(C)2006年的Android开源项目
*
* Apache许可证下授权,版本2.0(以下简称许可证);
*您可能不能使用这个文件除了在遵守许可。
*您可以在获得许可证的副本
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*许可证下发布分布在原样的基础上,
*无担保或任何形式的条件,无论是EX preSS或暗示的保证。
*请参阅许可的特定语言的管理权限和
*许可下限制。
* /
包android.widget;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.content.BroadcastReceiver;
进口android.content.res.Resources;
进口android.content.res.TypedArray;
进口android.graphics.Canvas;
进口android.graphics.drawable.Drawable;
进口android.os.Handler;
进口android.text.format.Time;
进口android.util.AttributeSet;
进口android.view.View;
进口android.widget.RemoteViews.RemoteView;
进口java.util.TimeZone中;
/ **
*这个小工具显示两只手的类比时钟小时,
*分钟。
* /
@RemoteView
公共类AnalogClock扩展视图{
私人时间mCalendar;
私人绘制对象mHourHand;
私人绘制对象mMinuteHand;
私人绘制对象mDial;
私人诠释mDialWidth;
私人诠释mDialHeight;
私人布尔mAttached;
私人最终处理程序mHandler =新的处理程序();
私人浮动mMinutes;
私人浮动mHour;
私人布尔mChanged;
公共AnalogClock(上下文的背景下){
这(背景下,NULL);
}
公共AnalogClock(上下文的背景下,ATTRS的AttributeSet){
这(背景下,ATTRS,0);
}
!!错误mContext - >更改mContext上下文(我能做到这一点)
? 公共AnalogClock(上下文的背景下,ATTRS的AttributeSet,
INT defStyle){
超(背景下,ATTRS,defStyle);
资源R = mContext.getResources();
!!错误com.android.internal.R不能得到解决。 我找到了解决这个问题
mDial = r.getDrawable(Resources.getSystem()则getIdentifier(clock_dial,绘制,机器人));
和相同的其他线路只是改变名称以及绘制或设置样式。
vvvv的---- 但是我还是有以下code的一个问题。
TypedArray A =
context.obtainStyledAttributes(
的attrs,com.android.internal.R.styleable.AnalogClock,defStyle,0);
^^^^ ---- 我不知道如何去改变它,并使它发挥作用
mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
如果(mDial == NULL){
mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
}
mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour);
如果(mHourHand == NULL){
mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
}
mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute);
如果(mMinuteHand == NULL){
mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
}
mCalendar =新时代();
mDialWidth = mDial.getIntrinsicWidth();
mDialHeight = mDial.getIntrinsicHeight();
}
@覆盖
保护无效onAttachedToWindow(){
super.onAttachedToWindow();
如果(!mAttached){
mAttached = TRUE;
IntentFilter的过滤器=新的IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
。的getContext()registerReceiver(mIntentReceiver,过滤器,空,mHandler);
}
//注意:这是安全的登记接收器,因为接收器总是跑后做这些
//在主线程,因此接收器不能在此方法返回前运行。
//时区可能已经改变,而接收器没有注册,所以更新时间
mCalendar =新时代();
//确保我们更新到当前时间
onTimeChanged();
}
@覆盖
保护无效onDetachedFromWindow(){
super.onDetachedFromWindow();
如果(mAttached){
。的getContext()unregisterReceiver(mIntentReceiver);
mAttached = FALSE;
}
}
@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
INT widthMode = MeasureSpec.getMode(widthMeasureSpec);
INT widthSize = MeasureSpec.getSize(widthMeasureSpec);
INT heightMode = MeasureSpec.getMode(heightMeasureSpec);
INT heightSize = MeasureSpec.getSize(heightMeasureSpec);
浮HSCALE = 1.0F;
浮VSCALE = 1.0F;
如果(widthMode = MeasureSpec.UNSPECIFIED和放大器;!&安培; widthSize< mDialWidth){
HSCALE =(浮点)widthSize /(浮点)mDialWidth;
}
如果(heightMode = MeasureSpec.UNSPECIFIED和放大器;!&安培; heightSize< mDialHeight){
VSCALE =(浮点)heightSize /(浮点)mDialHeight;
}
浮规模= Math.min(HSCALE,VSCALE);
setMeasuredDimension(resolveSize((int)的(mDialWidth *标度),widthMeasureSpec),
resolveSize((int)的(mDialHeight *标度),heightMeasureSpec));
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
mChanged = TRUE;
}
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
布尔改变= mChanged;
如果(改变){
mChanged = FALSE;
}
!错误 - 哪里mRight,MLEFT,mBotton,mTop从何而来?这是不是在这个文件中声明
INT availableWidth = mRight - MLEFT;
INT availableHeight = mBottom - mTop;
INT X = availableWidth / 2;
INT Y = availableHeight / 2;
最终绘制对象表盘= mDial;
INT W = dial.getIntrinsicWidth();
INT H = dial.getIntrinsicHeight();
布尔比例= FALSE;
如果(availableWidth<W¯¯|| availableHeight< H){
缩放= TRUE;
浮规模= Math.min((浮点)availableWidth /(浮点)W,
(浮点)availableHeight /(浮点)H);
canvas.save();
canvas.scale(刻度,刻度,X,Y);
}
如果(改变){
dial.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
}
dial.draw(画布);
canvas.save();
canvas.rotate(mHour / 12.0f * 360.0f,X,Y);
最终绘制对象hourHand = mHourHand;
如果(改变){
瓦特= hourHand.getIntrinsicWidth();
H = hourHand.getIntrinsicHeight();
hourHand.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
}
hourHand.draw(画布);
canvas.restore();
canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f,X,Y);
最终绘制对象minuteHand = mMinuteHand;
如果(改变){
瓦特= minuteHand.getIntrinsicWidth();
H = minuteHand.getIntrinsicHeight();
minuteHand.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
}
minuteHand.draw(画布);
canvas.restore();
如果(缩放){
canvas.restore();
}
}
私人无效onTimeChanged(){
mCalendar.setToNow();
INT小时= mCalendar.hour;
INT分钟= mCalendar.minute;
诠释第二= mCalendar.second;
mMinutes =分钟+秒/ 60.0f;
mHour =小时+ mMinutes / 60.0f;
mChanged = TRUE;
}
私人最终的BroadcastReceiver mIntentReceiver =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getAction()。等于(Intent.ACTION_TIMEZONE_CHANGED)){
字符串TZ = intent.getStringExtra(时区);
mCalendar =新时间(TimeZone.getTimeZone(TZ).getID());
}
onTimeChanged();
无效();
}
};
}
谁能告诉我:
我怎样才能让像一个正常的AnalogClock这个类的工作 如何写一个方法,这个类中,将应用的时间,我会提供给它? 解决方案合作定制MyAnalogClock用的setTime()方法。
花一些时间,我终于成功地解决了这个问题之后。 我希望这将是有用的人。 您可以通过调用的setTime(小时,分,秒)方法来设置时间。
进口android.content.Context;
进口android.content.res.Resources;
进口android.content.res.TypedArray;
进口android.graphics.Canvas;
进口android.graphics.drawable.Drawable;
进口android.text.format.Time;
进口android.util.AttributeSet;
进口android.view.View;
进口android.widget.RemoteViews.RemoteView;
/ **
*这个小工具显示两只手的类比时钟小时,
*分钟。
* /
@RemoteView
公共类MyAnalogClock扩展视图{
私人绘制对象mHourHand;
私人绘制对象mMinuteHand;
私人绘制对象mSecondHand;
私人绘制对象mDial;
私人诠释mDialWidth;
私人诠释mDialHeight;
私人浮动mSeconds;
私人浮动mMinutes;
私人浮动mHour;
语境mContext;
公共MyAnalogClock(上下文的背景下){
超(上下文);
}
公共MyAnalogClock(上下文的背景下,ATTRS的AttributeSet){
这(背景下,ATTRS,0);
}
公共MyAnalogClock(上下文的背景下,ATTRS的AttributeSet,
INT defStyle){
超(背景下,ATTRS,defStyle);
资源R = context.getResources();
TypedArray A =
context.obtainStyledAttributes(
的attrs,R.styleable.AnalogClock,defStyle,0);
mContext =背景;
// mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
//如果(mDial == NULL){
mDial = r.getDrawable(R.drawable.clock_dial);
//}
// mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour);
//如果(mHourHand == NULL){
mHourHand = r.getDrawable(R.drawable.clock_hour);
//}
// mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute);
//如果(mMinuteHand == NULL){
mMinuteHand = r.getDrawable(R.drawable.clock_minute);
mSecondHand = r.getDrawable(R.drawable.clockgoog_minute);
//}
mDialWidth = mDial.getIntrinsicWidth();
mDialHeight = mDial.getIntrinsicHeight();
}
@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
INT widthMode = MeasureSpec.getMode(widthMeasureSpec);
INT widthSize = MeasureSpec.getSize(widthMeasureSpec);
INT heightMode = MeasureSpec.getMode(heightMeasureSpec);
INT heightSize = MeasureSpec.getSize(heightMeasureSpec);
浮HSCALE = 1.0F;
浮VSCALE = 1.0F;
如果(widthMode = MeasureSpec.UNSPECIFIED和放大器;!&安培; widthSize< mDialWidth){
HSCALE =(浮点)widthSize /(浮点)mDialWidth;
}
如果(heightMode = MeasureSpec.UNSPECIFIED和放大器;!&安培; heightSize< mDialHeight){
VSCALE =(浮点)heightSize /(浮点)mDialHeight;
}
浮规模= Math.min(HSCALE,VSCALE);
setMeasuredDimension(resolveSize((int)的(mDialWidth *标度),widthMeasureSpec),
resolveSize((int)的(mDialHeight *标度),heightMeasureSpec));
}
@覆盖
保护无效的OnDraw(帆布油画){
super.onDraw(画布);
//在这里你可以设置你的时钟大小
INT availableWidth = 70;
INT availableHeight = 70;
//实际尺寸
INT X = availableWidth / 2;
INT Y = availableHeight / 2;
最终绘制对象表盘= mDial;
INT W = dial.getIntrinsicWidth();
INT H = dial.getIntrinsicHeight();
布尔比例= FALSE;
如果(availableWidth<W¯¯|| availableHeight< H){
缩放= TRUE;
浮规模= Math.min((浮点)availableWidth /(浮点)W,
(浮点)availableHeight /(浮点)H);
canvas.save();
canvas.scale(刻度,刻度,X,Y);
}
dial.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
dial.draw(画布);
canvas.save();
canvas.rotate(mHour / 12.0f * 360.0f,X,Y);
瓦特= mHourHand.getIntrinsicWidth();
H = mHourHand.getIntrinsicHeight();
mHourHand.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
mHourHand.draw(画布);
canvas.restore();
canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f,X,Y);
瓦特= mMinuteHand.getIntrinsicWidth();
H = mMinuteHand.getIntrinsicHeight();
mMinuteHand.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
mMinuteHand.draw(画布);
canvas.restore();
canvas.save();
canvas.rotate(mSeconds,X,Y);
瓦特= mSecondHand.getIntrinsicWidth();
H = mSecondHand.getIntrinsicHeight();
mSecondHand.setBounds(X - (瓦特/ 2)中,y - (H / 2)中,x +(W / 2)中,y +(H / 2));
mSecondHand.draw(画布);
canvas.restore();
如果(缩放){
canvas.restore();
}
}
公共无效的setTime(INT小时,INT分钟,INT秒)
{
mSeconds = 6.0f *秒;
mMinutes =分钟+秒/ 60.0f;
mHour =小时+ mMinutes / 60.0f;
}
}
您也应该放在绘制文件夹4绘制文件,并创建(或更新)ATTRS文件中值的文件夹下面的code:
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<申报,设置样式名称=AnalogClock>
< attr指示NAME =拨打格式=参考/>
< attr指示NAME =hand_hour格式=参考/>
< attr指示NAME =hand_minute格式=参考/>
< /申报,设置样式>
< /资源>
I want to make a custom AnalogClock class using source code of AnalogClock by Android Open Source Project that is available in the internet.
I want to make the clock set the time that I want, not the current time. I didn't find a clear example on how to do it so maybe this post will be useful. After copying source code to new file, I get some errors. Below is the original source code of AnalogClock:
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews.RemoteView;
import java.util.TimeZone;
/**
* This widget display an analogic clock with two hands for hours and
* minutes.
*/
@RemoteView
public class AnalogClock extends View {
private Time mCalendar;
private Drawable mHourHand;
private Drawable mMinuteHand;
private Drawable mDial;
private int mDialWidth;
private int mDialHeight;
private boolean mAttached;
private final Handler mHandler = new Handler();
private float mMinutes;
private float mHour;
private boolean mChanged;
public AnalogClock(Context context) {
this(context, null);
}
public AnalogClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
!!Error mContext -> changed mContext to context (could I do that)?
public AnalogClock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = mContext.getResources();
!!Error com.android.internal.R cannot be resolved. I found a solution to this problem with
mDial = r.getDrawable(Resources.getSystem().getIdentifier("clock_dial","drawable", "android"));
And same for other lines just changing name and either drawable or styleable.
vvvv----But I still have a problem with the below code.
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.AnalogClock, defStyle, 0);
^^^^----I don't have idea how to change it and make it work
mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
if (mDial == null) {
mDial = r.getDrawable(com.android.internal.R.drawable.clock_dial);
}
mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour);
if (mHourHand == null) {
mHourHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_hour);
}
mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute);
if (mMinuteHand == null) {
mMinuteHand = r.getDrawable(com.android.internal.R.drawable.clock_hand_minute);
}
mCalendar = new Time();
mDialWidth = mDial.getIntrinsicWidth();
mDialHeight = mDial.getIntrinsicHeight();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
getContext().registerReceiver(mIntentReceiver, filter, null, mHandler);
}
// NOTE: It's safe to do these after registering the receiver since the receiver always runs
// in the main thread, therefore the receiver can't run before this method returns.
// The time zone may have changed while the receiver wasn't registered, so update the Time
mCalendar = new Time();
// Make sure we update to the current time
onTimeChanged();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAttached) {
getContext().unregisterReceiver(mIntentReceiver);
mAttached = false;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float hScale = 1.0f;
float vScale = 1.0f;
if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
hScale = (float) widthSize / (float) mDialWidth;
}
if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
vScale = (float )heightSize / (float) mDialHeight;
}
float scale = Math.min(hScale, vScale);
setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mChanged = true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
boolean changed = mChanged;
if (changed) {
mChanged = false;
}
!!!Error - where does mRight, mLeft, mBotton, mTop come from? It is not declared in this file
int availableWidth = mRight - mLeft;
int availableHeight = mBottom - mTop;
int x = availableWidth / 2;
int y = availableHeight / 2;
final Drawable dial = mDial;
int w = dial.getIntrinsicWidth();
int h = dial.getIntrinsicHeight();
boolean scaled = false;
if (availableWidth < w || availableHeight < h) {
scaled = true;
float scale = Math.min((float) availableWidth / (float) w,
(float) availableHeight / (float) h);
canvas.save();
canvas.scale(scale, scale, x, y);
}
if (changed) {
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
dial.draw(canvas);
canvas.save();
canvas.rotate(mHour / 12.0f * 360.0f, x, y);
final Drawable hourHand = mHourHand;
if (changed) {
w = hourHand.getIntrinsicWidth();
h = hourHand.getIntrinsicHeight();
hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
hourHand.draw(canvas);
canvas.restore();
canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);
final Drawable minuteHand = mMinuteHand;
if (changed) {
w = minuteHand.getIntrinsicWidth();
h = minuteHand.getIntrinsicHeight();
minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
}
minuteHand.draw(canvas);
canvas.restore();
if (scaled) {
canvas.restore();
}
}
private void onTimeChanged() {
mCalendar.setToNow();
int hour = mCalendar.hour;
int minute = mCalendar.minute;
int second = mCalendar.second;
mMinutes = minute + second / 60.0f;
mHour = hour + mMinutes / 60.0f;
mChanged = true;
}
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
String tz = intent.getStringExtra("time-zone");
mCalendar = new Time(TimeZone.getTimeZone(tz).getID());
}
onTimeChanged();
invalidate();
}
};
}
Can anyone tell me:
How can I make this class work like a normal AnalogClock How can I write a method inside this class that will apply time that I will provide to it?解决方案
Working custom MyAnalogClock with setTime() method.
After spending some time I finally managed to solve the problem. I hope it will be useful for someone. You can set time simply by calling setTime(hours, minutes, seconds) method.
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews.RemoteView;
/**
* This widget display an analogic clock with two hands for hours and
* minutes.
*/
@RemoteView
public class MyAnalogClock extends View {
private Drawable mHourHand;
private Drawable mMinuteHand;
private Drawable mSecondHand;
private Drawable mDial;
private int mDialWidth;
private int mDialHeight;
private float mSeconds;
private float mMinutes;
private float mHour;
Context mContext;
public MyAnalogClock(Context context) {
super(context);
}
public MyAnalogClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyAnalogClock(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
Resources r = context.getResources();
TypedArray a =
context.obtainStyledAttributes(
attrs, R.styleable.AnalogClock, defStyle, 0);
mContext=context;
// mDial = a.getDrawable(com.android.internal.R.styleable.AnalogClock_dial);
// if (mDial == null) {
mDial = r.getDrawable(R.drawable.clock_dial);
// }
// mHourHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_hour);
// if (mHourHand == null) {
mHourHand = r.getDrawable(R.drawable.clock_hour);
// }
// mMinuteHand = a.getDrawable(com.android.internal.R.styleable.AnalogClock_hand_minute);
// if (mMinuteHand == null) {
mMinuteHand = r.getDrawable(R.drawable.clock_minute);
mSecondHand = r.getDrawable(R.drawable.clockgoog_minute);
// }
mDialWidth = mDial.getIntrinsicWidth();
mDialHeight = mDial.getIntrinsicHeight();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float hScale = 1.0f;
float vScale = 1.0f;
if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) {
hScale = (float) widthSize / (float) mDialWidth;
}
if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) {
vScale = (float )heightSize / (float) mDialHeight;
}
float scale = Math.min(hScale, vScale);
setMeasuredDimension(resolveSize((int) (mDialWidth * scale), widthMeasureSpec),
resolveSize((int) (mDialHeight * scale), heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Here you can set the size of your clock
int availableWidth = 70;
int availableHeight = 70;
//Actual size
int x = availableWidth / 2;
int y = availableHeight / 2;
final Drawable dial = mDial;
int w = dial.getIntrinsicWidth();
int h = dial.getIntrinsicHeight();
boolean scaled = false;
if (availableWidth < w || availableHeight < h) {
scaled = true;
float scale = Math.min((float) availableWidth / (float) w,
(float) availableHeight / (float) h);
canvas.save();
canvas.scale(scale, scale, x, y);
}
dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
dial.draw(canvas);
canvas.save();
canvas.rotate(mHour / 12.0f * 360.0f, x, y);
w = mHourHand.getIntrinsicWidth();
h = mHourHand.getIntrinsicHeight();
mHourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
mHourHand.draw(canvas);
canvas.restore();
canvas.save();
canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);
w = mMinuteHand.getIntrinsicWidth();
h = mMinuteHand.getIntrinsicHeight();
mMinuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
mMinuteHand.draw(canvas);
canvas.restore();
canvas.save();
canvas.rotate(mSeconds, x, y);
w = mSecondHand.getIntrinsicWidth();
h = mSecondHand.getIntrinsicHeight();
mSecondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
mSecondHand.draw(canvas);
canvas.restore();
if (scaled) {
canvas.restore();
}
}
public void setTime(int hours, int minutes, int seconds)
{
mSeconds = 6.0f*seconds;
mMinutes = minutes + seconds / 60.0f;
mHour = hours + mMinutes / 60.0f;
}
}
You should also put 4 drawable files in the drawable folder and create (or update) the attrs file in values folder with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="AnalogClock">
<attr name="dial" format="reference"/>
<attr name="hand_hour" format="reference"/>
<attr name="hand_minute" format="reference"/>
</declare-styleable>
</resources>