Android ruler effect, based on time support left and right sliding (original)

Source: Internet
Author: User
Tags dateformat

After years, to now also did not seriously write code, just the company has the demand, spent an afternoon, wrote a ruler effect of the custom view, the effect is OK, but there are a lot of places to improve, I hope you correct!!! Can swipe left and right, below is the key code!!

Package Com.example.drawrulerview;import Oncustomrurlelistener.oncustomviewlistener;import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.porterduffxfermode;import Android.graphics.rectf;import Android.graphics.paint.fontmetricsint;import Android.graphics.PorterDuff.Mode; Import Android.util.attributeset;import android.util.log;import android.view.motionevent;import Android.view.View; Import Android.view.windowmanager;import Android.widget.Toolbar; @SuppressLint ("Newapi") public class Customruleview Extends View {private final int NUM = 4;//number of bars to display in the entire screen width private Canvas mcanvas;private Paint mpaint;private int Mviewwid th;private int mviewheight;private String middlestrtime;private double millswidthprecent;//the length to represent per second private double timeprecent;//each part length to represent the time (s) private float Xstar, xend;public customruleview (context context, AttributeSet Attrs) { Super (context,ATTRS); Initfirstdata ();} private void Initfirstdata () {//TODO auto-generated Method stubthis.middlestrtime = Utils.timelong2date ( System.currenttimemillis ());} private void InitData () {if (Mpaint = = null) {Mpaint = new Paint (); mviewheight = This.getheight (); mviewwidth = This.getwid Th (); millswidthprecent = mviewwidth/(NUM * * * 1.0); timeprecent = num * * * * 1.0/MVIEWWIDTH; LOG.D ("msg", "---prcetn:" + millswidthprecent);}} @Overrideprotected void OnDraw (canvas canvas) {clear (canvas); InitData ();d rawview (canvas); Super.ondraw (canvas);} private void Drawview (Canvas mcanvas) {This.mcanvas = Mcanvas;drawmiddleline ();d rawtexttopmiddle (middlestrtime); Drawruledeg ();} /** * Clear Interface * * @param mcanvas */public void Clear (Canvas mcanvas) {Paint paint = new Paint ();p Aint.setxfermode (New Porter Duffxfermode (MODE.DST)); Mcanvas.drawpaint (paint);} /* * According to the intermediate scale, draw other ticks */private void Drawruledeg () {mpaint.setcolor (Color.gray); mpaint.settextsize (MVIEWHEIGHT/6); Entityconversiontime Conversiontime = UtIls.getcurrenttimeentity (middlestrtime);//The time of the next hour (unit s) int nxtemisstmp = (60-conversiontime.getminnum ()-1) * 60 + 6 0-conversiontime.getmillnum ()///distance from last hour (unit s) int beforemisstmp = Conversiontime.getminnum () * 60+ Conversiontime.getmillnum (); LOG.D ("msg", "---Width:" + nxtemisstmp * millswidthprecent+ "precent:" + millswidthprecent + "Realwidth:" + mviewwidth) ;d Rawlinetext ((float) (MVIEWWIDTH/2 + (nxtemisstmp) * millswidthprecent), conversiontime.gethournum () + 1); Drawlinetext (float) (MVIEWWIDTH/2 + (nxtemisstmp + 3600) * millswidthprecent), conversiontime.gethournum () + 2);//left DRA Wlinetext (float) (MVIEWWIDTH/2-beforemisstmp* millswidthprecent), Conversiontime.gethournum ());d Rawlinetext (( float) (MVIEWWIDTH/2-(beforemisstmp + 3600)//More than one cycle * millswidthprecent), Conversiontime.gethournum ()-1);} private void Drawlinetext (float left, int. tvtime) {RECTF RECTF = new RECTF (left-20, MVIEWHEIGHT/3, left + 20,mviewheig HT/2);d Rawceninview (RECTF, Mcanvas, Mpaint, TvTIME + ": xx"); Mcanvas.drawline (left, Mviewheight*2/3 -10, left, MVIEWHEIGHT*2/3, mpaint);} private void Drawtexttopmiddle (String tvstr) {this.middlestrtime = Tvstr;mpaint.setcolor (Color.White); Mpaint.settextsize (MVIEWHEIGHT/4); RECTF RECTF = new RECTF (0, 0, mviewwidth, MVIEWHEIGHT/3);d Rawceninview (RECTF, Mcanvas, Mpaint, tvstr);} /** * Draw in the middle of the rectangle * * @param targetRect0 * @param canvas * @param paint * @param tvstr */private void Drawceninview (RECTF ta RgetRect0, canvas canvas, Paint paint,string tvstr) {fontmetricsint fontmetrics = Paint.getfontmetricsint (); int baseline = (int) (targetrect0.top+ (Targetrect0.bottom-targetrect0.top-fontmetrics.bottom + fontmetrics.top)/2-FONTMETRICS.T OP);p aint.settextalign (Paint.Align.CENTER), Canvas.drawtext (Tvstr, Targetrect0.centerx (), baseline, paint);} /** * Draw Middle line */private void Drawmiddleline () {//TODO auto-generated method Stubmpaint.setcolor (Color.yellow); Mpaint.setstrokewidth (2); Mcanvas.drawline (MVIEWWIDTH/2, 0, MVIEWWIDTH/2, MViewHeight, mpaint);} @Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: LOG.D ("msg", "touching1"); Xstar = Event.getx (); Break;case motionevent.action_up:log.d ("msg", "touching2"); xEnd = 0; Xstar = 0;break;case MotionEvent.ACTION_MOVE:xEnd = Event.getx ();d Rawviewtouch (xstar-xend); Xstar = Event.getx (); break ;d Efault:break;} Return Super.ontouchevent (event);} /** * The method to be called when sliding * * @param f */private void Drawviewtouch (float f) {log.d ("msg", "Touching3:---->" + f); if (f! = 0) { Slide call related method long time = (long) (Utils.date2timelong (middlestrtime) + timeprecent* f * +); middlestrtime = Utils.timelong 2Date (time);} LOG.V ("msg", "Middletime:" + middlestrtime); invalidate ();}}

Package Com.example.drawrulerview;import Java.text.dateformat;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.calendar;import Java.util.timezone;public class Utils {/** * 1393445856-- -&GT;2014-05-01 00:01:58 * * @param time * @return */public static String timelong2date (long time) {DateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); return Formatter.format (time);} /** * 2014-05-01 00:01:58--->1393445565 * @param time * @return */public static long Date2timelong (String time) {Calen Dar C = calendar.getinstance (); try {c.settime (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Parse (time);} catch ( ParseException e) {e.printstacktrace ();} return C.gettimeinmillis ();}  /** * Gets the current date entity * * @return */public static entityconversiontime getcurrenttimeentity (String strtime) {final Calendar c = Calendar.getinstance (); C.settimezone (Timezone.gettimezone ("gmt+8:00")); try {c.settime (new SimpleDateFormat (" Yyyy-mm-dd HH:mm:ss "). Parse (strtime)); catch (ParSeexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Entityconversiontime entityconversiontime = new Entityconversiontime (); Entityconversiontime.setyearnum (C.get ( calendar.year));//Gets the current year Entityconversiontime.setmonthnum (C.get (calendar.month) + 1);// Gets the current month Entityconversiontime.setdaynum (C.get (Calendar.day_of_month));//Gets the date number of the current month String Mway = string.valueof ( C.get (Calendar.day_of_week)); Entityconversiontime.sethournum (C.get (calendar.hour_of_day)); Entityconversiontime.setminnum (C.get (Calendar.minute)); Entityconversiontime.setmillnum (C.get (Calendar.SECOND))  ; int tmp = 1;IF ("1". Equals (Mway)) {tmp = 7;} else if ("2". Equals (Mway)) {tmp = 1;} else if ("3". Equals (Mway)) {tmp = 2;}  else if ("4". Equals (Mway)) {tmp = 3;} else if ("5". Equals (Mway)) {tmp = 4;} else if ("6". Equals (Mway)) {tmp = 5;} else if ("7". Equals (Mway)) {tmp = 6;} Entityconversiontime.setweeknum (TMP); tmp = Entityconversiontime.getmonthnum (); if (tmp = = 1 | | tmp = = 3 | | tmp = 5 | | tmp = = 7 | | TMP = = 8| | TMP = = 10 | | TMP = = 12) {//31 days TMP = +,} else if (tmp = = 2) {if (Isleapyear (entityconversiontime)) {tmp =-;} else {tmp =;}} else {tmp = 30;} Entityconversiontime.setmonthallday (TMP);//Set Quarter if (Entityconversiontime.getmonthnum () <= 3) { Entityconversiontime.setfest (1);} else if (Entityconversiontime.getmonthnum () <= 6) {entityconversiontime.setfest (2);} else if ( Entityconversiontime.getmonthnum () <= 9) {entityconversiontime.setfest (3);} else if ( Entityconversiontime.getmonthnum (<=) {entityconversiontime.setfest (4);} + entityconversiontime.getmonthallday () + "" "//+ entityconversiontime.getmonthnum () +" "//+ entityconversiontime.get WeekNUM () + "//+ entityconversiontime.getyearnum () +" "); return entityconversiontime;} /** * Determines if a leap year is false = Not leap */private static Boolean isleapyear (Entityconversiontime conversiontime) {Boolean ret;if (c Onversiontime.getyearnum ()/4 = = 0&& Conversiontime.getyearnum ()/+ = 0) {//ret = true;} else {ret = false;} return ret;}} 

  

Record several questions: 1.

Yyyy-mm-dd HH:mm:ss where HH means 24-hour HH means 12-hour system

Android ruler effect, based on time support left and right sliding (original)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.