Look at the picture:
Relatively humble, mainly through the canvas painting up:
PackageCom.example.democurvegraph.view;Importjava.util.ArrayList;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.ViewGroup.LayoutParams;/*** Custom Local Line chart *@authorFeijian * @time June 29, 2015 15:37:41*/ Public classCurveviewextendsview{ArrayList<Float>ListData; intMheight,mwidth; Private intDefault_circle_radius = 4; Private intSelected_circle_radius = 8; Private intpadding = 8; Private intWidthems = 0;//distance between two adjacent strokes//Private float avg = 0;//The average value of the array inside the Listdata Private floatMaxValue = 0;//the maximum value Private intlinecolor=0;//the color of the line Private intcirclecolor=0;//the color of the circle Private floatpre_x = 0,pre_y = 0; PublicCurveview (Context context) {Super(context); } PublicCurveview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicCurveview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } /*** Custom initialization of drawing data *@paramListData *@paramLineColor *@paramCirclecolor*/ Public voidDrawData (arraylist<float> ListData,intLineColor,intCirclecolor) { This. ListData =ListData; Layoutparams Llparam= This. Getlayoutparams (); Mheight=Llparam.height; Mwidth=Llparam.width; if(listdata!=NULL) { for(intI=0;i<listdata.size (); i++) { if( This. MaxValue <Listdata.get (i)) { This. MaxValue =Listdata.get (i); } } } This. LineColor =LineColor; This. Circlecolor =Circlecolor; This. Widthems = (mwidth-padding*2)/listdata.size (); System.out.print ("Widthems=" +widthems+ ", mwidth=" +mwidth+ ";p addingleft=" +padding+ "; size=" +listdata.size () + "; mheight=" +mheight); Invalidate ();//Refresh Interface} @Overrideprotected voidOnDraw (canvas canvas) {SYSTEM.OUT.PRINTLN ("OnDraw Init"); if(listdata!=NULL) {System.out.println ("OnDraw"); Paint Circlepaint=NewPaint (); Circlepaint.setcolor (Circlecolor); Paint Linepaint=NewPaint (); Linepaint.setcolor (LineColor); for(intI=0;i<listdata.size (); i++) { if(Pre_x==0 && pre_y==0)//description Start drawing the first circle{pre_x=padding; Pre_y= (Mheight-listdata.get (i) *mheight/maxValue); } System.out.println ("pre_x=" +pre_x+ ";p re_y=" +pre_y); if(i > 0)//you need to draw a previous segment{canvas.drawline (pre_x, pre_y, pre_x+widthems, Mheight-listdata.get (i) *mheight/MaxValue, Linepaint); Pre_x= pre_x+Widthems; Pre_y= Mheight-listdata.get (i) *mheight/MaxValue; } canvas.drawcircle (pre_x, pre_y, Default_circle_radius, Circlepaint); } } Super. OnDraw (canvas); } @Override Public Booleanontouchevent (Motionevent event) {//gets the coordinates of the point when the screen is clicked floatx =Event.getx (); floaty =event.gety (); System.out.println ("X=" +x+ ", y=" +y+ ", Event.getaction ()" +event.getaction ()); return Super. Ontouchevent (event); }}
How to use:
Curveview = (Curveview) Findviewbyid (r.id.v_curve); = curveview.getlayoutparams (); =; =N; ArrayListNew arraylist<float>(); Listdata.add (1f); Listdata.add (2f); Listdata.add (3f); Listdata.add (2f); Listdata.add (5f); Listdata.add (1f); Listdata.add (4f); Curveview.drawdata (Listdata,color.parsecolor ("#ffffff"), Color.parsecolor ("#ffffff"));
Demo download
Android Custom Line chart