首页 > 代码库 > Android倒计时电子钟的实现(下篇)
Android倒计时电子钟的实现(下篇)
本篇继续上篇未完成的部分,这篇文章会实现动态效果如图所示
在程序中先给定一个截止时间:
2014-12-20 00:00:00
然后计算当期时间和截止时间相差的时间
private long getCurrentShowTimeSeconds() { Date curTime = new Date(); long ret = endDate.getTime() - curTime.getTime(); ret =Math.round( ret/1000 ); return ret >= 0 ? ret : 0; }
然后在线程中添加循环,并间隔50ms刷新一次。
具体代码如下
/** * CountDownView.java * Copyright(C) 2014 * creator:cuiran 2014-12-19 下午2:18:59 */ package com.cayden.countdown.view; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import com.cayden.countdown.Constants; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; /** * 倒计时View * @author cuiran * @version 1.0.0 */ public class CountDownView extends SurfaceView implements Runnable, Callback,Constants { private static final String TAG="CountDownView"; private SurfaceHolder mHolder; //用于控制SurfaceView private Canvas mCanvas; //声明画布 private Paint mPaint; //声明画笔 private Thread mThread; //声明一个线程 private static final int RADIUS=10; //声明小球半径 private static final int MARGIN_TOP = 60; private static final int MARGIN_LEFT = 30; private ArrayList<int[][]> list=new ArrayList<int[][]>(); private static final String END="2014-12-20 00:00:00"; private Date endDate=null; private long curShowTimeSeconds=0; public CountDownView(Context context) { super(context); mHolder = this.getHolder(); //获得SurfaceHolder对象 mHolder.addCallback(this); //添加状态监听 mPaint = new Paint(); //创建一个画笔对象 mPaint.setColor(Color.BLUE); //设置画笔的颜色 } private long getCurrentShowTimeSeconds() { Date curTime = new Date(); long ret = endDate.getTime() - curTime.getTime(); ret =Math.round( ret/1000 ); return ret >= 0 ? ret : 0; } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder arg0) { mThread = new Thread(this); //创建线程对象 mThread.start(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { } @SuppressLint("SimpleDateFormat") public Date strToDateLong(String strDate) { if("".equals(strDate)||null==strDate){ return null; } SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } @Override public void run() { try{ list.add(data0); list.add(data1); list.add(data2); list.add(data3); list.add(data4); list.add(data5); list.add(data6); list.add(data7); list.add(data8); list.add(data9); list.add(data10); endDate=strToDateLong(END); while(true){ curShowTimeSeconds=getCurrentShowTimeSeconds(); mDraw(); Thread.sleep(50); } }catch(Exception e){ Log.e(TAG,"run error",e); } } /** * 自定义绘图方法 * 2014-12-19 下午2:22:45 * */ public void mDraw() { mCanvas = mHolder.lockCanvas(); //获得画布对象,开始对画布画画 mCanvas.drawColor(Color.BLACK); //设置画布颜色为黑色 canvas(mCanvas); mHolder.unlockCanvasAndPost(mCanvas); //把画布显示在屏幕上 } public void canvas(Canvas mCanvas) { //画圆,(x轴,y轴,半径,画笔) int hours=(int)curShowTimeSeconds / 3600; int minutes=(int)(curShowTimeSeconds - hours * 3600)/60 ; int seconds=(int)curShowTimeSeconds % 60; canvasDigit( MARGIN_LEFT , MARGIN_TOP , hours/10 , mCanvas ); canvasDigit( MARGIN_LEFT + 15*(RADIUS+1) , MARGIN_TOP , hours%10 , mCanvas ); canvasDigit( MARGIN_LEFT + 30*(RADIUS + 1) , MARGIN_TOP , 10 , mCanvas ); canvasDigit( MARGIN_LEFT + 39*(RADIUS+1) , MARGIN_TOP , minutes/10 , mCanvas); canvasDigit( MARGIN_LEFT + 54*(RADIUS+1) , MARGIN_TOP , minutes%10 , mCanvas); canvasDigit( MARGIN_LEFT + 69*(RADIUS+1) , MARGIN_TOP , 10 , mCanvas); canvasDigit( MARGIN_LEFT + 78*(RADIUS+1) , MARGIN_TOP , seconds/10 , mCanvas); canvasDigit( MARGIN_LEFT + 93*(RADIUS+1) , MARGIN_TOP , seconds%10 , mCanvas); } public void canvasDigit(int x,int y,int num,Canvas mCanvas) { int [][] data=http://www.mamicode.com/list.get(num);>
需要详细代码可以访问 具体优化部分暂时还没有处理。github地址:https://github.com/cayden/CountDown
Android倒计时电子钟的实现(下篇)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。