The implementation of the Android Countdown electronic clock (next article)

Source: Internet
Author: User

This article continues with the unfinished part of the story, which will achieve dynamic effects


A deadline is given in the program first:

2014-12-20 00:00:00

Then calculate the time difference between the current time and the cutoff time

Private Long Getcurrentshowtimeseconds () {Date curtime = new Date ();    LONG ret = Enddate.gettime ()-curtime.gettime ();    RET =math.round (ret/1000);    return ret >= 0? ret:0;}

The loop is then added to the thread, and the interval is 50ms refreshed once.

The specific code is as follows

/** * Countdownview.java * Copyright (C) * Creator:cuiran 2014-12-19 pm 2:18:59 */package Com.cayden.countdown.view;im Port 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, Callbac    k,constants {private static final String tag= "Countdownview";p rivate surfaceholder Mholder;    Used to control surfaceviewprivate Canvas Mcanvas;    Declaration Canvas private Paint Mpaint;    Declaration Brush private Thread Mthread; Declares a thread private static final int radius=10;//declaration sphere 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= "2014-12-20 00:00:00";    Private Date Enddate=null;  Private Long Curshowtimeseconds=0;public Countdownview (context context) {super (context);    Mholder = This.getholder ();    Get Surfaceholder Object Mholder.addcallback (this);    Add state listener Mpaint = new Paint ();   Create a Brush object Mpaint.setcolor (Color.Blue);    Sets the color of the brush}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 Surfacecreate    D (Surfaceholder arg0) {mthread = new Thread (this); Create 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);D ate 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 Drawing method * 2014-12-19 pm 2:22:45 * */public void Mdraw () {Mcanvas = Mholder.lockcanvas ();     Get 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, 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, 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 section that has not been processed yet.

GitHub Address: Https://github.com/cayden/CountDown


The implementation of the Android Countdown electronic clock (next article)

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.