Android uses canvas to draw work schedules

Source: Internet
Author: User

There is a project to use the worksheet, choose to use canvas to draw, to achieve the selection of display work schedule, you can click Add and Cancel.

Custom Control FormView:

Package Com.sun.elderly.comm.widget;import Com.sun.elderly.comm.ui.interfaces.iformlistener;import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.util.sparsearray;import android.view.motionevent;import android.view.view;/** * Draw form * * @author Jwzhangjie */ public class FormView extends View {private int firstx = 0;//starting point xprivate int firsty = 0;//starting point yprivate int secondx = 80 ; 2nd xprivate int secondy = 50; 2nd yprivate int widthnum = 8; column private int heightnum = 10; Line private int secondsidex = 150; The second column of the wide private int sidey = 50; Line height private int firstsidesx = 80;  The first column of the wide private int workcolor = 0xffadff2f;private int restcolor = 0xffad0f2f;private int selectcolor = 0xffad00ff;private string[] Rowtext = null;private string[] Coltext = null;private String noData = "1";p ublic void Setrowandcoltext (string[ ] Rowtext, string[]Coltext) {This.rowtext = Rowtext;this.coltext = Coltext;invalidate ();} Public sparsearray<string> list = new sparsearray<string> ();p rivate sparsearray<string> backList = New Sparsearray<string> (); Iformlistener myformlistener;public Iformlistener Getformlistener () {return Myformlistener;} public void Setformlistener (Iformlistener myformlistener) {this.myformlistener = Myformlistener;} Public FormView (Context context) {super (context);} Public FormView (context context, AttributeSet Attrs) {Super (context, attrs);} public void init (int width, int height) {secondsidex = (width-firstsidesx)/(widthNum-1);//Invalidate ();} public void setlist (sparsearray<string> list) {this.list = List;backlist.clear ();//reset} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); if (coltext! = NULL && rowtext! = null) {drawform (canvas);}} private void Drawform (canvas canvas) {Paint paint = new Paint ();p Aint.setantialias (True);p Aint.setcolor (Color.Black); paint.seTstyle (Paint.Style.STROKE);p aint.setstrokewidth (2);p Aint.setstyle (Paint.Style.FILL);p Aint.setcolor (color.black );p Aint.setstyle (Paint.Style.STROKE); int cellx = 0, celly = 0, cellbx = 0, Cellby = 0;for (int i = 0; i < widthnum; i++  {for (int j = 0; J < Heightnum, J + +) {if (i = = 0) {//) if the first column is drawn the width of the first column CELLX = Firstx + i * firstsidesx;celly = firsty + J * SIDEY;CELLBX = Firstx + (i + 1) * Firstsidesx;cellby = Firsty + (j + 1) * Sidey;} else {cellx = Secondx + (i-1) * secondsidex;celly = Secondy + (j-1) * SIDEY;CELLBX = secondx + i * Secondsidex;cellby = Secondy + J * Sidey;} Canvas.drawrect (CELLX, celly, CELLBX, Cellby, paint); int cellsnum = i + J * widthnum;if (J = = 0) {drawcelltext (canvas, cel LX, celly, CELLBX, Cellby,rowtext[i]);} if (cellsnum% widthnum! = 0) {if (!nodata.equals (List.get (Cellsnum, NoData))) {if (List.get (Cellsnum). Equals ("0")) {Draw CellColor (Canvas, Cellx, celly, CELLBX, Cellby,workcolor);} else {drawcellcolor (canvas, Cellx, celly, CELLBX, Cellby,restcolor);}} ELSE if (!nodata.equals (Backlist.get (Cellsnum, NoData))) {Drawcellcolor (canvas, Cellx, celly, CELLBX, Cellby, Selectcolor);} else {drawcellcolor (canvas, Cellx, celly, CELLBX, CELLBY,0XFFF);}} else {//Draw first column drawcelltext (canvas, Cellx, celly, CELLBX, Cellby,coltext[cellsnum/widthnum]);}}} Draw the text in a cell private void drawcelltext (canvas canvas, int cellx, int celly, int cellbx,int Cellby, String text) {Paint pain t = new Paint ();p aint.setflags (Paint.anti_alias_flag);p Aint.setcolor (color.blue); int textSize = (cellby-celly)/5 * 2;p Aint.settextsize (textSize); int textx = Cellx + (CELLBX-CELLX)/10;int texty = Cellby-(cellby-celly)/3;canvas.draw Text (text, TEXTX, texty, paint);}  Draw a color in a cell private void drawcellcolor (canvas canvas, int cellx, int celly, int cellbx,int cellby, int color) {Paint paint = new paint ();//Paint the preparation of the selected color border and the color Paint.setcolor (colour);p aint.setstyle (Paint.Style.FILL); Canvas.drawrect (Cellx + 1, Celly + 1, cellBX-1, cellBY-1, paint);} @SuppressLint ("clickableviewaccessIbility ") @Overridepublic boolean ontouchevent (Motionevent event) {Float Touchx = event.getx (); Float touchy = event.gety ( int antion = Event.getaction (); if (antion = = Motionevent.action_down) {Testtouchcolorpanel (Touchx, TouchY);} Return Super.ontouchevent (event);}  Detects the number of blocks that the click event resides in public boolean testtouchcolorpanel (float x, float y) {if (x > Secondx && y > secondy&& X < FIRSTX + Firstsidesx + secondsidex * widthnum&& y < firsty + Sidey * heightnum) {int ty = (int) (y-f irsty)/sidey); int tx;if (X-firstx-firstsidesx > 0) {tx = (int) ((x-firstx-firstsidesx)/Secondsidex + 1);} E LSE {tx = 0;} int index = ty * widthnum + tx;if (nodata.equals (List.get (index, nodata)) {if (!nodata.equals (index, NoData) ) {Backlist.remove (index); myformlistener.shownum (index, 1);} else {backlist.put (index, "2"); Myformlistener.shownum (index, 2);} Invalidate ();} else {myformlistener.shownum (index, 0);} return true;} return false;}}

Main interface drawformactivity:

Package Com.sun.elderly.test;import Com.sun.elderly.r;import Com.sun.elderly.comm.ui.interfaces.IFormListener; Import Com.sun.elderly.comm.widget.formview;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.util.sparsearray;import Android.view.ViewTreeObserver; Import Android.widget.toast;public class Drawformactivity extends Fragmentactivity implements Iformlistener {private FormView myformview;private sparsearray<string> list = new sparsearray<string> ();p rivate string[] RowText = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};p rivate string[] Coltext = {"", "9:00", "10:00", "11:00", "12:00", "13:00", "15:00", "16:00", "17:00"}, @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Settitle ("Worksheet"); Setcontentview (r.layout.test_drawform); MyFormView = (FormView ) Findviewbyid (R.id.formid); list.put (0);//Key: Location, value:0--has booked a break time 2--pre-reserve reservation occupied List.put (1); List.put (19 , "1"); LisT.put ("0"), List.put ("0"), List.put ("1"), myformview.setlist (list); Myformview.setformlistener (this); Viewtreeobserver vto = Myformview.getviewtreeobserver (); Vto.addonpredrawlistener (new Viewtreeobserver.onpredrawlistener () {public Boolean onpredraw () {myformview.getviewtreeobserver (). Removeonpredrawlistener (this); int height = myformview.getmeasuredheight (); int width = Myformview.getmeasuredwidth (); Myformview.init (width, height); Myformview.setrowandcoltext (Rowtext, Coltext); return true;}); @Overridepublic void shownum (int num, int status) {switch (status) {case 0:showinfo (num + "Current time is already occupied") Break;case 1:showin Fo ("deselect:" + num); break;case 2:showinfo ("Select Working time:" + num); break;} private void Showinfo (String text) {Toast.maketext (Getapplicationcontext (), text, Toast.length_short). Show ();}}

Layout test_drawform:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >        <com.sun.elderly.comm.widget.formview        android:id= "@+id/formid"        Android:layout_width= "Match_parent"        android:layout_height= "wrap_content" android:layout_margin= "10dip                " /></linearlayout>

Display effect:








Android uses canvas to draw work schedules

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.