Android uses Canvas to draw work schedules. Android canvas

Source: Internet
Author: User

Android uses Canvas to draw work schedules. Android canvas

There is a project to use a worksheet, select to use canvas to draw, to achieve the choice of displaying the work schedule, click to add and cancel,: http://jwzhangjie.com/workplan.gif

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;/*** plot the table ** @ author development Z Hangjie */public class FormView extends View {private int firstX = 0; // start point xprivate int firstY = 0; // start point yprivate int secondX = 80; // The second vertex xprivate int secondY = 50; // The second vertex yprivate int widthNum = 8; // The column private int heightNum = 10; // The row private int secondSideX = 150; // The width of the second column private int sideY = 50; // The Row Height private int firstSidesX = 80; // The width of the first column private int workColor = 0xffADFF2F; private int restColor = 0xffAD0F2F; private int selectColor = 0xffAD00FF; private String [] rowText = null; private String [] colText = null; private String noData = "-1 "; public void setRowAndColText (String [] rowText, String [] colText) {this. rowText = rowText; this. colText = colText; invalidate ();} public SparseArray <String> list = new SparseArray <String> (); private SparseArray <String> backList = new SparseArray <String> (); IFormLi Stener 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 (); paint. setAntiAlias (true); paint. setColor (Color. BLACK); paint. setStyle (Paint. style. STROKE); paint. setStrokeWidth (2); paint. setStyle (Paint. style. FILL); paint. setColor (Color. BLACK); paint. 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 it is the first column, the width of the first column is drawn. 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, cellX, cellY, cellBX, cellBY, rowText [I]);} if (cellsNum % widthNum! = 0) {if (! NoData. equals (list. get (cellsNum, noData) {if (list. get (cellsNum ). equals ("0") {drawCellColor (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, 0 xFFF) ;}} else {// draw the first drawCellText (canvas, cellX, cellY, cellBX, cellBY, colText [cellsNum/widthNum]); }}} // draw the private void drawCellText (Canvas canvas, int cellX, int cellY, int cellBX, int cellBY, String text) {Paint pain T = new Paint (); paint. setFlags (Paint. ANTI_ALIAS_FLAG); paint. setColor (Color. BLUE); int textSize = (cellBY-cellY)/5*2; paint. setTextSize (textSize); int textX = cellX + (cellBX-cellX)/10; int textY = cellBY-(cellBY-cellY)/3; canvas. drawText (text, textX, textY, paint);} // draws the private void drawCellColor (Canvas canvas, int cellX, int cellY, int cellBX, int cellBY, int color) in the cell) {Paint paint = New Paint (); // draw an alternative color border and the color paint. setColor (color); paint. setStyle (Paint. style. FILL); canvas. drawRect (cellX + 1, cellY + 1, cellBX-1, cellBY-1, paint);} @ SuppressLint ("ClickableViewAccessibility") @ Overridepublic boolean onTouchEvent (MotionEvent) {float touchX = event. getX (); float touchY = event. getY (); int antion = event. getAction (); if (antion = MotionEvent. ACTION_DOWN) {testTouchColorPan El (touchX, touchY);} return super. onTouchEvent (event);} // check the number of cells of the Click event 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-firstY)/sideY); int tx; if (x-firstX-firstSidesX> 0) {tx = (int) (x-firstX-firstSidesX) /secondSideX + 1);} else {tx = 0;} int in Dex = ty * widthNum + tx; if (noData. equals (list. get (index, noData) {if (! NoData. equals (backList. get (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> (); private String [] rowText = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Saturday", "Sunday"}; private String [] colText = {"", "}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setTitle ("worksheet"); setContentView (R. layout. test_drawform); myFormView = (FormView) findViewById (R. id. formId); list. put (12, "0"); // key: location, value: 0 -- Reserved 1 -- rest time 2 -- list of reserved reservations. put (18, "1"); list. put (19, "1"); list. put (21, "0"); list. put (33, "0"); list. put (46, "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 + "occupied by current time"); break; case 1: showInfo ("deselected:" + 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:orientation="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:







Http://jwzhangjie.com/workplan.gif


How does Android view use canvas drawing outside ondraw?

You should set the identifier in ontouchevent and refresh the control. Draw the chart according to the identifier in the onDraw method.

How does Android view use canvas in ontouchevent?

You should set the identifier in ontouchevent and refresh the control. Draw the chart according to the identifier in the onDraw method.

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.