The graph of Android custom view

Source: Internet
Author: User

The recent project to do a simple graph to identify the data, began to think very simple, Android has so many open source chart library, what Achartenginee,hellochart,mpandroidchart and so on, download the demo to find, Are strong to a bit not suitable for my small display function, straight is no language. So I can only paint, inherit from the view to redraw.

Let's look at the following:


Idea: Divide the canvas evenly according to the number of points, and then draw all the horizontal and vertical axes of the chart before dividing. Then convert the data into point coordinates, draw to the screen, and finally connect the adjacent two points into a line.

The source code is as follows:

Import Android.content.context;import Android.content.res.resources;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.paint.style;import Android.graphics.path;import Android.graphics.point;import Android.util.attributeset;import Android.util.displaymetrics;import Android.view.view;import android.view.windowmanager;/********************************************************** * @ file Name: Linegraphicview.java * @ file Rzq * @ created: May 27, 2015 3:05:19 * @ File Description: Custom Simple graph * @ Modify History: May 27, 2015 create initial version ************ /class Linegraphicview extends view{/** * Public part */private static final int Circle_size = 10;private static enum Linestyle{line, Curve}private Context mcontext;private Paint mpaint;private Resource s res;private displaymetrics dm;/** * Data */private Linestyle mstyle = linestyle.curve;private int canvasheight;private i NT Canvaswidth;private int bheight = 0;private int blwidh;private Boolean ismeasure = true;/** * Y-Axis maximum */private int maxvalue;/** * y-axis spacing value */private int averagevalue;private int margintop = 20;private int marginbottom = 40;/** * Total points on curve */private point[] mpoints;/** * Ordinate value */private arraylist<double> yrawdata;/** * Horizontal value */private ArrayList< string> xrawdatas;private arraylist<integer> xlist = new arraylist<integer> ();//Record the value of each x private int Spacingheight;public Linegraphicview (Context context) {This (context, null);} Public Linegraphicview (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;initview ();} private void Initview () {this.res = Mcontext.getresources (); this.mpaint = new Paint (paint.anti_alias_flag);d m = new Displaymetrics (); WindowManager wm = (WindowManager) mcontext.getsystemservice (Context.window_service); Wm.getdefaultdisplay (). Getmetrics (DM);} @Overrideprotected void onsizechanged (int w, int h, int oldw, int oldh) {if (ismeasure) {this.canvasheight = GetHeight (); thi S.canvaswidth = GetWidth (); if (bheight = = 0) bheight = (int) (CanvasheiGht-marginbottom); Blwidh = dip2px (+); ismeasure = false;}} @Overrideprotected void OnDraw (canvas canvas) {Mpaint.setcolor (Res.getcolor (R.COLOR.COLOR_F2F2F2));d Rawallxline ( canvas);//Draw Straight (portrait) drawallyline (canvas);//point operation set mpoints = Getpoints (); Mpaint.setcolor (Res.getcolor (r.color.color_ ff4631)); Mpaint.setstrokewidth (dip2px (2.5f)); Mpaint.setstyle (Style.stroke); if (Mstyle = = Linestyle.curve) { Drawscrollline (canvas);} Else{drawline (canvas);} Mpaint.setstyle (Style.fill); for (int i = 0; i < mpoints.length; i++) {canvas.drawcircle (mpoints[i].x, MPOINTS[I].Y, CIRCLE_SIZE/2, Mpaint);}} /** * Draw all horizontal tables, including x-axis */private void drawallxline (canvas canvas) {for (int i = 0; i < spacingheight + 1; i++) {Canvas.drawl INE (Blwidh, Bheight-(bheight/spacingheight) * i + margintop, (Canvaswidth-blwidh), Bheight-(bheight/spacingheight ) * i + margintop, mpaint);//y-coordinate DrawText (string.valueof (Averagevalue * i), BLWIDH/2, Bheight-(bheight/spacingheight ) * i + Margintop,canvas);}} /** * Draw all vertical tables, including y-axis */private void Drawallyline (canvas canvas) {for (int i = 0; i < yrawdata.size (); i++) {Xlist.add (Blwidh + (Canvaswidth-blwid h)/yrawdata.size () * i), canvas.drawline (Blwidh + (Canvaswidth-blwidh)/yrawdata.size () * I, MarginTop, blwidh+ (Canva Swidth-blwidh)/yrawdata.size () * I, Bheight + margintop, mpaint);d Rawtext (Xrawdatas.get (i), Blwidh + (CANVASWIDTH-BL Widh)/yrawdata.size () * I, Bheight + dip2px (+), canvas);//x-coordinate}}private void drawscrollline (canvas canvas) {Point STARTP = new Point (); Point ENDP = new Point (), for (int i = 0; i < mpoints.length-1; i++) {STARTP = MPOINTS[I];ENDP = mpoints[i + 1];int WT = (startp.x + endp.x)/2; Point p3 = new Point (); Point P4 = new Point ();p 3.y = startp.y;p3.x = Wt;p4.y = endp.y;p4.x = WT; Path PATH = new Path ();p Ath.moveto (startp.x, startp.y);p ath.cubicto (p3.x, P3.y, p4.x, P4.y, endp.x, ENDP.Y); Canvas.drawpath (path, mpaint);}} private void DrawLine (canvas canvas) {Point STARTP = new Point (); Point ENDP = new Point (), for (int i = 0; i < MPOints.length-1; i++) {STARTP = MPOINTS[I];ENDP = Mpoints[i + 1];canvas.drawline (startp.x, Startp.y, endp.x, ENDP.Y, MPaint);}} private void DrawText (String text, int x, int y, canvas canvas) {Paint p = new Paint (paint.anti_alias_flag);p. Settextsize (d IP2PX ());p. SetColor (Res.getcolor (r.color.color_999999));p. SetTextAlign (Paint.Align.LEFT); Canvas.drawtext ( text, x, Y, p);} Private point[] Getpoints () {point[] points = new Point[yrawdata.size ()];for (int i = 0; i < yrawdata.size (); i++) {int P H = bheight-(int) (Bheight * (Yrawdata.get (i)/maxValue);p oints[i] = new Point (Xlist.get (i), Ph + margintop);} return points;} public void SetData (arraylist<double> yrawdata, arraylist<string> xrawdata, int maxValue, int averagevalue) {This.maxvalue = Maxvalue;this.averagevalue = averagevalue;this.mpoints = new Point[yrawdata.size ()];this.xRawDatas = Xrawdata;this.yrawdata = Yrawdata;this.spacingheight = Maxvalue/averagevalue;} public void Settotalvalue (int maxValue) {this.maxvalue = MAXVAlue;} public void Setpjvalue (int averagevalue) {this.averagevalue = Averagevalue;} public void Setmargint (int margintop) {this.margintop = margintop;} public void setmarginb (int marginbottom) {this.marginbottom = MarginBottom;} public void Setmstyle (Linestyle mstyle) {this.mstyle = Mstyle;} public void setbheight (int bheight) {this.bheight = Bheight;} /** * from the DP unit according to the resolution of the phone to be px (pixels) */private int dip2px (float dpvalue) {return (int) (Dpvalue * dm.density + 0.5f);}}

Use:

Package Com.example;import Java.util.arraylist;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity{linegraphicview tu; Arraylist<double> ylist; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); tu = (Linegraphicview) Findviewbyid (R.id.line_graphic) ; ylist = new arraylist<double> (); Ylist.add ((Double) 2.103); Ylist.add (4.05); Ylist.add (6.60); Ylist.add (3.08); Ylist.add (4.32); Ylist.add (2.0); Ylist.add (5.0); arraylist<string> Xrawdatas = new arraylist<string> () Xrawdatas.add ("05-19"); Xrawdatas.add ("05-20"); Xrawdatas.add ("05-21"), Xrawdatas.add ("05-22"), Xrawdatas.add ("05-23"), Xrawdatas.add ("05-24"); Xrawdatas.add (" 05-25 "); Xrawdatas.add (" 05-26 "); Tu.setdata (Ylist, Xrawdatas, 8, 2);}}

Demo Download

The graph of Android custom view

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.