Implementation of Countdown e-clock for Android (Part 2) and later for android

Source: Internet
Author: User

Implementation of Countdown e-clock for Android (Part 2) and later for android

This article continues with the unfinished part, which will achieve dynamic results


In the program, first specify an End Time:

00:00:00

Calculate the time difference between the current time and the end time.

private long getCurrentShowTimeSeconds() {Date curTime = new Date();    long ret = endDate.getTime() - curTime.getTime();    ret =Math.round( ret/1000 );    return ret >= 0 ? ret : 0;}

Then add a loop in the thread and refresh it every 50 ms.

The Code is as follows:

/*** CountDownView. java * Copyright (C) 2014 * creator: cuiran 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;/*** countdown 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; // used to control SurfaceViewpr Ivate Canvas mCanvas; // declare the Canvas private Paint mPaint; // declare the brush private Thread mThread; // declare a Thread private static final int RADIUS = 10; // declare the ball radius 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 = "00:00:00"; private Date endDate = null; private long curShowTimeSeconds = 0; public CountDownView (Context context) {super (context); mHolder = this. getHolder (); // obtain the SurfaceHolder object mHolder. addCallback (this); // Add status listening mPaint = new Paint (); // create a Paint brush object mPaint. setColor (Color. BLUE); // set the paint brush color} private long getCurrentShowTimeSeconds () {Date curTime = new Date (); long ret = endDate. getTime ()-curTime. getTime (); ret = Math. round (ret/1000); return ret> = 0? Ret: 0 ;}@ Overridepublic void surfaceChanged (SurfaceHolder arg0, int arg1, int arg2, int arg3) {}@ Overridepublic void surfaceCreated (SurfaceHolder arg0) {mThread = new Thread (this); // creates a Thread object mThread. start () ;}@ Overridepublic 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;} @ Overridepublic 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) ;}/ *** custom Plotting Method ** 2:22:45 **/public void mDraw () {mCanvas = mHolder. lockCanvas (); // obtain the canvas object and start painting the canvas mCanvas. drawColor (Color. BLACK); // set the canvas color to BLACK canvas (mCanvas); mHolder. unlockCanvasAndPost (mCanvas); // display the canvas on the screen} public void Canvas (canvas mCanvas) {// draw a circle, (x axis, Y axis, radius, brush) 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 = list. get (num); for (int I = 0; I <data. length; I ++) {for (int j = 0; j <data [I]. length; j ++) {if (data [I] [j] = 1) {mCanvas. drawCircle (x + j * 2 * (RADIUS + 1) + (RADIUS + 1), y + I * 2 * (RADIUS + 1) + (RADIUS + 1), RADIUS, mPaint );}}}}}

Detailed code is required to access the specific optimization part, which has not been processed yet.

Github address: https://github.com/cayden/CountDown


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.