Android精美时钟代码

  • D8_554930
    了解作者
  • 34.2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-16 01:39
    上传日期
Android自定义控件,精美时钟下载,图片和代码都包含在里面了,自己放到对应文件夹运行即可。
时钟.zip
  • 时钟
  • clock_dial.png
    27.5KB
  • 时钟.txt
    5.9KB
  • clock_hand_second.png
    1.3KB
  • clock_hand_hour.png
    476B
  • clock_hand_minute.png
    3.1KB
内容介绍
public class Clock extends View { //private Time mCalendar; private Drawable mHourHand; private Drawable mMinuteHand; private Drawable mSecondHand; private Drawable mDial; private int mDialWidth; private int mDialHeight; private boolean mAttached; private float mMinutes; private float mHour; private boolean mChanged; private static final int SECONDS_FLAG = 0; private Message secondsMsg; private float mSeconds; private boolean bGMTTime = false; @SuppressLint("HandlerLeak") private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SECONDS_FLAG: invalidate(); break; default: break; } super.handleMessage(msg); } }; public CustomeAnalogClock(Context context) { this(context, null); } public CustomeAnalogClock(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomeAnalogClock(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); Resources r = context.getResources(); if (mDial == null) { mDial = r.getDrawable(R.drawable.clock_dial); } if (mHourHand == null) { mHourHand = r.getDrawable(R.drawable.clock_hand_hour); } if (mMinuteHand == null) { mMinuteHand = r.getDrawable(R.drawable.clock_hand_minute); } if (mSecondHand == null) { mSecondHand = r.getDrawable(R.drawable.clock_hand_second); } mDialWidth = mDial.getIntrinsicWidth(); mDialHeight = mDial.getIntrinsicHeight(); } public void setGMTTime(boolean bGMT) { bGMTTime = bGMT; } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (!mAttached) { mAttached = true; } onTimeChanged(); initSecondsThread(); } private void initSecondsThread() { Thread newThread = new Thread() { @Override public void run() { while (mAttached) { onTimeChanged(); secondsMsg = mHandler.obtainMessage(SECONDS_FLAG); mHandler.sendMessage(secondsMsg); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; newThread.start(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (mAttached) { 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; } int availableWidth = getWidth(); int availableHeight = getHeight(); 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(); //add second draw canvas.save(); canvas.rotate(mSeconds / 60.0f * 360.0f, x, y); final Drawable secondHand = mSecondHand; if (changed) { w = secondHand.getIntrinsicWidth(); h = secondHand.getIntrinsicHeight(); secondHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2)); } secondHand.draw(canvas); canvas.restore(); if (scaled) { canvas.restore(); } } private void onTimeChanged() { int hour = 0; int minute = 0; int second = 0; if (bGMTTime) { Date gmtDate = SystemTimerService.getInstance(getContext()).getGmtTimer(); hour = gmtDate.getHours(); minute = gmtDate.getMinutes(); second = gmtDate.getSeconds() + 1; } else { Date curDate = Calendar.getInstance().getTime(); hour = curDate.getHours(); minute = curDate.getMinutes(); second = curDate.getSeconds() + 1; } mSeconds = second; mMinutes = minute + second / 60.0f; mHour = hour + mMinutes / 60.0f; mChanged = true; } }
评论
    相关推荐
    • Android学习
      Android学习Android学习Android学习Android学习Android学习Android学习
    • Android678
      Android678Android678Android678Android678Android678Android678Android678Android678Android678Android678
    • android 例子
      android 例子android 例子android 例子android 例子android 例子
    • android图片
      android精美小图片android精美小图片android精美小图片android精美小图片
    • android课件
      清华老师上课的android课件。ch_01android概述ch_02android开发基础ch_03资源的访问ch_04android用户界面ch_05Ativity和Intent。其他自己看。
    • AndroidKiller
      安卓App反编译工具AndroidKiller\AndroidKiller\AndroidKiller
    • AndroidSample
      Android示例 这是一个示例Android应用,用于降低Google Play服务和Android Volley的使用率。 有关更多详细信息,请参阅以下链接: Google Play服务: Android凌空抽射:
    • android source
      android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source
    • Android 教程
      Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android ...
    • android资料
      Android程序员指南 Android_UI开发专题 Android程序开发初级教程 android的五大类各种常用类的资料整理 Android高手进阶教程