Brokenline CONTROLS:
Import Java.text.simpledateformat;import Java.util.arraylist;import Java.util.calendar;import Java.util.collections;import Java.util.date;import Java.util.gregoriancalendar;import Java.util.List;import Android.content.context;import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.dashpatheffect;import Android.graphics.lineargradient;import Android.graphics.Paint;import Android.graphics.path;import Android.graphics.patheffect;import Android.graphics.shader;import Android.util.Log; Import Android.view.view;import Android.view.viewgroup.layoutparams;import Com.qianhua.healthy_brain. r;/** * wireframe * * @author Administrator * */public class Brokenline extends View {private list<integer> milliliter;p rivate float tb;private float interval_left_right;private float interval_left;private float margin_bottom;private Paint Paint_date, Paint_brokenline, Paint_dOttedline,paint_brokenline_big, Frampanint;private int time_index;private Bitmap bitmap_point;private path Path; Private float dotted_text;public float getdotted_text () {return dotted_text;} public void Setdotted_text (float dotted_text) {this.dotted_text = Dotted_text;} private int finelinecolor = 0X5FAAAAAA; Gray private int bluelinecolor = 0XFF00FFFF; Blue private int orangelinecolor = 0XFFD56F2B; Orange Public Brokenline (context context, list<integer> milliliter) {super (context); init (milliliter);} public void init (list<integer> milliliter) {if (null = = Milliliter | | milliliter.size () = = 0) return;this.milliliter = Delzero (milliliter); Resources res = getresources (), TB = Res.getdimension (R.DIMEN.HISTORYSCORE_TB), interval_left_right = TB * 5.0f;interval_ left = TB * 0.5F;MARGIN_BOTTOM=8*TB * 0.2f;paint_date = new paint ();p aint_date.setstrokewidth (TB * 0.1f);p Aint_date.sette Xtsize (TB * 1.2f);p aint_date.setcolor (finelinecolor);p aint_brokenline = new Paint ();p Aint_brokenline.setstrokeWidth (TB * 0.1f);p aint_brokenline.setcolor (bluelinecolor);p Aint_brokenline.setantialias (true);p Aint_dottedline = New Paint ();p Aint_dottedline.setstyle (Paint.Style.STROKE);p Aint_dottedline.setcolor (finelinecolor);p Aint_ Brokenline_big = new Paint ();p aint_brokenline_big.setstrokewidth (TB * 0.2f);p Aint_brokenline_big.setcolor ( Finelinecolor);p Aint_brokenline_big.setantialias (true), Frampanint = new paint (); Frampanint.setantialias (true); Frampanint.setstrokewidth (2f);p ath = new Path (), Bitmap_point = Bitmapfactory.decoderesource (Getresources (), R.drawable.icon_point_blue) Setlayoutparams (New Layoutparams ((int) (This.milliliter.size () * interval_left_right), layoutparams.match_parent));} /** * Remove left and right zero data * * @return */public list<integer> Delzero (list<integer> milliliter) {list<integer> lis t = new arraylist<integer> (), int sta = 0;int end = 0;for (int i = 0; i < milliliter.size (); i++) {if (milliliter. Get (i)! = 0) {sta = I;break;}} for (int i = Milliliter.size ()-1; i >= 0; i--) {if (Milliliter.get (i)! = 0) {end = I;break;}} for (int i = 0; i < milliliter.size (); i++) {if (I >= STA && i <= end) {List.add (Milliliter.get (i));}} Time_index = Sta;//dotted_text = ((Collections.max (milliliter)-Collections. Min (milliliter))/12.0f * 5.0f); return list ;} protected void OnDraw (Canvas c) {if (null = = Milliliter | | milliliter.size () = = 0) return;drawstraightline (c);d Rawbrokenli NE (c);d rawdate (c); /** * Draw Vertical Bar * * @param c */public void Drawstraightline (Canvas c) {int count_line = 0;for (int i = 0; i < milliliter.si Ze (); i++) {if (i==0) {//Draw y-axis c.drawline (interval_left_right * I, 0, interval_left_right * I, getheight ()-Margin_bottom, Paint_ Date), for (int j = 0; J <10; J + +) {C.drawtext (string.valueof (10* (j+1)), 0, (GetHeight ()-margin_bottom)/10* (10-(j+1)), Paint_date); if (j==5) {//Draw dashed paint_dottedline.setcolor (Orangelinecolor); Path PATH = new Path ();p Ath.moveto (0, (getheight ()-margin_bottom)/10* (10-(j+1)));p Ath.lineto (GetWidth (), (GetHeight ( )-MArgin_bottom)/10* (10-(j+1))); Patheffect effects = new Dashpatheffect (new float[] {TB * 0.3F,TB * 0.3f, TB * 0.3f, TB * 0.3f}, TB * 0.1f);p aint_dotted Line.setpatheffect (effects); C.drawpath (path, paint_dottedline);} }continue;} Draw a vertical bar four consecutive lines for a set of Paint_dottedline.setcolor (Finelinecolor); if (Count_line = = 0) {c.drawline (interval_left_right * I, 0, Interval_left_right * I, getheight ()-Margin_bottom, paint_date);} if (Count_line = = 2) {c.drawline (Interval_left_right * I, TB * 1.5f, Interval_left_right * I, getheight ()-Margin_bottom, Paint_date);} if (Count_line = = 1 | | count_line = = 3) {Path PATH = new Path ();p ath.moveto (interval_left_right * I, TB * 1.5f);p ath.linet O (interval_left_right * i, getheight ()-margin_bottom); Patheffect effects = new Dashpatheffect (new float[] {TB * 0.3f, TB * 0.3f, TB * 0.3f, TB * 0.3f}, TB * 0.1f);p Aint_dotte Dline.setpatheffect (effects); C.drawpath (path, paint_dottedline);} Count_line++;if (Count_line >= 4) {count_line = 0;}} Draw X-axis c.drawline (0, getheight ()-Margin_bottom, GetWidth (), GetHeight ()-Margin_bottom, paint_brokenline_big);} /** * Draw polyline * * @param c */public void Drawbrokenline (Canvas c) {int index = 0;float temp_x = 0;float temp_y = 0;//float b ASE = (getheight ()-TB * 3.0f)/(Collections.max (milliliter)-collections.min (milliliter)); float base = (getheight ()- Margin_bottom)/100; Shader Mshader = new LinearGradient (0, 0, 0, getheight (), new int[] {color.argb (0, 255, 255), Color.argb (45, 0, 255, 255), Color.argb (0, 255, 255)}, NULL, Shader.TileMode.CLAMP); Frampanint.setshader (Mshader); for (int i = 0; i < Mill Iliter.size ()-1; i++) {LOG.E ("I", "" "+milliliter.get (i)); float x1 = interval_left_right * I;float y1 = getheight ()-Margin_bottom-(base * Milliliter.get (i)); float Y1 = Milliliter.get (i); float x2 = interval_left_right * (i + 1); float y2 = getheight ()-Marg In_bottom-(Base * Milliliter.get (i + 1)), float Y2 = Milliliter.get (i+1), if ((int) (Base * Milliliter.get (i + 1)) = = 0 & amp;& index = = 0) {Index++;temp_x = x1;temp_y = y1;} if ((int) (Base * Milliliter.get (i + 1))! = 0 && Index! = 0) {index = 0;X1 = Temp_x;y1 = temp_y;} Paint_date.setcolor (Orangelinecolor); if (i==0) C.drawtext (string.valueof (Y1), x1, Y1, paint_date);//plot the size of the first value if ( index = = 0) {c.drawtext (string.valueof (Y2), X2, Y2, paint_date);//plot the size of the i+1 value C.drawline (x1, y1, x2, y2, paint_brokenline );p Ath.lineto (x1, y1), if (i! = 0) C.drawbitmap (bitmap_point,x1-bitmap_point.getwidth ()/2,y1-bitmap_point.getheight ( )/2, NULL), if (i = = Milliliter.size ()-2) {Path.lineto (x2, y2);p Ath.lineto (x2, GetHeight ());p ath.lineto (0, getheight ()) ;p ath.close (); C.drawpath (path, frampanint); C.drawbitmap (Bitmap_point,x2-bitmap_point.getwidth ()/2,Y2-BITMAP_ Point.getheight ()/2, NULL);}}} Paint_date.setcolor (Finelinecolor);} /** * Draw Time * * @param c */public void Drawdate (Canvas c) {date date=new date ();//Take time calendar calendar =new gregoriancal Endar (); Calendar.settime (date); SimpleDateFormat dateformat=new SimpleDateFormat ("mm-dD "); String[] dates=new string[milliliter.size ()];for (int i = 0; i < milliliter.size (); i++) {Calendar.add (calendar. date,1);//Increase the date by one day. Integer backwards, negative numbers move forward date=calendar.gettime (); This time is the result of the date pushing back the day Dates[i]=dateformat.format (date). toString (); for (int i = 0; i < milliliter.size (); i + = 1) {paint_date.setstrokewidth (TB * 2.8f); C.drawtext (Dates[i], interval_left _right * I, GetHeight (), paint_date);}}}
Activity:
Import Java.util.arraylist;import java.util.list;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.widget.relativelayout;import Com.qianhua.healthy_brain. R;import Com.qianhua.healthy_brain.widget.brokenline;public class Trainresultactivity extends Activity { Relativelayout linear; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.train_result); list<integer> lists = new arraylist<integer> ();//linear graph range 0-100for (int i = 0; i <; i++) {if (I < 8 | | i = = 28 | | i = = 12 | | i = = 18 | | i = = 20 | | i = = 30| | i = =) {lists.add (0);} else {//int item=getrandom (0),//LOG.W ("" +i, "" +item) Lists.add (getrandom (0, 100));}} Linear= (relativelayout) Findviewbyid (r.id.linear); Linear.addview (new Brokenline (this,lists));} public int getrandom (int min,int max) {return (int) Math.Round (Math.random () * (max-min) +min);}}
Xml:
<?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 " > <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" and Roid:background= "#f4f4f7" android:orientation= "vertical" >
Reference: Http://www.apkbus.com/forum.php?mod=viewthread&tid=158365&extra=page%3D1
Android Custom Line chart