Android Canvas exercise (4) Self-drawing line chart

Source: Internet
Author: User

After finishing the column chart, I suddenly found that it was easy to create a line chart. A line chart, line chart, and column chart were created immediately.

:

It mainly uses the Path of Android, which is very useful in this case.

Code attached:

Package com. xcl. canvas03;/*** Canvas exercise ** it is practical to draw a pie chart and column chart by yourself. * line chart and a mix of line charts and bars * author: xiongchuanliang * date: 2014-4-6 */import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. content. context; import android. content. res. resources; import android. graphics. blurMaskFilter; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. path; import android. graphics. typeface; import android. graphics. path. direction; import android. graphics. rectF; import android. util. displayMetrics; import android. view. menu; import android. view. view; @ SuppressLint ("NewApi") public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. activity_main); // setContentView (new PanelRpt (this); // pie chart // setContentView (new PanelBar (this )); // bar chart setContentView (new PanelLnChart (this); // line chart} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} class PanelLnChart extends View {private int ScrHeight; private int ScrWidth; private Paint [] arrPaintArc; private Paint PaintText = null; final int [] colors = new int [] {R. color. red, R. color. white, R. color. green, R. color. yellow, R. color. blue ,}; // percentage used for the pie chart demonstration. In actual use, the final float arrPer [] = new float [] {20f, 30f, 10f, 40f}; // The proportion used for the column chart demonstration. In actual use, it is the proportion parameter private final int [] arrNum = {,} That is passed in externally }; public PanelLnChart (Context context) {super (context); // solves the canvas in versions earlier than 4.1. drawTextOnPath () does not display the problem this. setLayerType (View. LAYER_TYPE_SOFTWARE, null); // display information DisplayMetrics dm = getResources (). getDisplayMetrics (); ScrHeight = dm. heightPixels; ScrWidth = dm. widthPixels; // set the special edge effect BlurMaskFilter PaintBGBlur = new BlurMaskFilter (1, BlurMaskFilter. blur. INNER); arrPaintArc = new Paint [5]; Resources res = this. getResources (); for (int I = 0; I <5; I ++) {arrPaintArc [I] = new Paint (); arrPaintArc [I]. setColor (res. getColor (colors [I]); arrPaintArc [I]. setStyle (Paint. style. FILL); arrPaintArc [I]. setStrokeWidth (4); arrPaintArc [I]. setMaskFilter (PaintBGBlur);} PaintText = new Paint (); PaintText. setColor (Color. BLUE); PaintText. setTextSize (30); PaintText. setTypeface (Typeface. DEFAULT_BOLD);} public void onDraw (Canvas canvas) {// canvas background Canvas. drawColor (Color. WHITE); // the title of the pie chart canvas. drawText ("self-painted line chart", 50, PaintText); arrPaintArc [0]. setTextSize (25); arrPaintArc [3]. setTextSize (25); arrPaintArc [0]. setStyle (Paint. style. STROKE); int I = 0; int lnWidth = 10; // identify line width int lnSpace = 40; // identify spacing int startx = 120; int endx = startx + 20; int starty = ScrHeight/3; int endy = ScrHeight/3; int initX = startx; int initY = starty; int rectHeight = 10; // Bar Height // line chart /////// ////////////////// Y axis identification line and value int yCount = 5; for (I = 0; I
 
  

Link to other charts:

Android Canvas exercise (1) Draw a report

Android Canvas exercise (2) Self-painted pie chart

Android Canvas exercise (3) Self-painted column chart



MAIL: xcl_168@aliyun.com

BLOG: http://blog.csdn.net/xcl168


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.