Andoid custom view line chart, andoid line chart
1. This mainly includes simple drawing operations.
Package com. che300.price. component; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. path; import android. graphics. pointF; import android. graphics. rectF; import android. util. attributeSet; import android. view. view; import com. che300.price. r; import com. che300.price. util. UIUtil; import java. util. arrayList; import java. util. list;/*** Created by Administrator on 2016/6/23. * // * LineChartView m = (LineChartView) findViewById (R. id. lineChartView); // The demo List <LineChartView is called here. itemInfo> infos = new ArrayList <> (); LineChartView. itemInfo info = new LineChartView. itemInfo (14.36f, "201612"); LineChartView. itemInfo info1 = new LineChartView. itemInfo (11.36f, "201613"); LineChartView. itemInfo info2 = new LineChartView. itemInfo (13.36f, "201615"); LineChartView. itemInfo info3 = new LineChartView. itemInfo (12.36f, "201612"); LineChartView. itemInfo info4 = new LineChartView. itemInfo (11.36f, "201612"); LineChartView. itemInfo info5 = new LineChartView. itemInfo (11.36f, "201612"); infos. add (info); infos. add (info1); infos. add (info2); infos. add (info3); infos. add (info4); infos. add (info5); m. setPrices (infos); <com. che300.price. component. lineChartView layout android: id = "@ + id/LineChartView" android: layout_width = "match_parent" android: layout_marginLeft = "15dp" android: layout_marginRight = "15dp" android: layout_height = "wrap_content"/> */public class LineChartView extends View {private Paint mpaint; private int width, heigth; private final int DEFAULTWIDTH = 200; // The height of the label 150dp private Context context _; public List <ItemInfo> prices = new ArrayList <> (); public List <PointF> pointFs = new ArrayList <> (); public int pricetipsize; public void setPrices (List <ItemInfo> prices) {this. prices. clear (); this. prices. addAll (prices); invalidate ();} public LineChartView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} public LineChartView (Context context) {super (context);} public LineChartView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} public void init (Context context) {this. context _ = context; mpaint = new Paint (); mpaint. setAntiAlias (true) ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {int widthmode = MeasureSpec. getMode (widthMeasureSpec); int hightmode = MeasureSpec. getMode (heightMeasureSpec); int widthsize = MeasureSpec. getSize (widthMeasureSpec); int higthsize = MeasureSpec. getSize (heightMeasureSpec); if (widthmode = MeasureSpec. EXACTLY) {width = widthsize;} else {width = UIUtil. getDisplayMetrics (context _). widthPixels;} if (hightmode = MeasureSpec. EXACTLY) {heigth = higthsize;} else {heigth = UIUtil. dp2pxInt (context _, 200);} setMeasuredDimension (width, heigth);} @ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); mpaint. setColor (getResources (). getColor (R. color. back); mpaint. setStrokeWidth (3); drawBackgrondLine (canvas); if (prices. size () = 0) {return;} initPricetipsize (); drawPoint (canvas); drawPointText (canvas); drawCanvasBg (canvas); drawPointOval (canvas ); drawCanvasButtom (canvas);} public void initPricetipsize () {pricetipsize = getDefaultTipSize ();} public void drawBackgrondLine (Canvas canvas) {mpaint. setColor (getResources (). getColor (R. color. line); int margin = heigth/4; for (int I = 0; I <4; I ++) {canvas. drawLine (0, I * margin, width, I * margin, mpaint) ;}} public void drawPointOval (Canvas canvas) {mpaint. setStrokeWidth (3); for (int I = 0; I <pointFs. size (); I ++) {PointF p = pointFs. get (I); mpaint. setColor (Color. WHITE); mpaint. setStyle (Paint. style. FILL); RectF mArc = new RectF (p. the x-UIUtil.dp2pxInt (context _, 4), p. the y-UIUtil.dp2pxInt (context _, 4), p. x + UIUtil. dp2pxInt (context _, 4), p. y + UIUtil. dp2pxInt (context _, 4); canvas. drawArc (mArc, 0f, 360f, true, mpaint); mpaint. setColor (Color. argb (0x44, 0xff, 0x66, 0x00); canvas. drawArc (mArc, 0f, 360f, true, mpaint); mpaint. setColor (getResources (). getColor (R. color. orange); // set the paint brush color mpaint. setStyle (Paint. style. STROKE); RectF mArc1 = new RectF (p. the x-UIUtil.dp2pxInt (context _, 4), p. the y-UIUtil.dp2pxInt (context _, 4), p. x + UIUtil. dp2pxInt (context _, 4), p. y + UIUtil. dp2pxInt (context _, 4); canvas. drawArc (mArc1, 0f, 360f, true, mpaint) ;}} public void drawPoint (Canvas canvas) {mpaint. setColor (getResources (). getColor (R. color. orange); for (int I = 0; I <prices. size (); I ++) {PointF p = getStartPostion (I, prices. get (I ). getPrice (); pointFs. add (p) ;}for (int I = 0; I <pointFs. size (); I ++) {if (I = pointFs. size ()-1) {return;} PointF p = pointFs. get (I); PointF p1 = pointFs. get (I + 1); canvas. drawLine (p. x, p. y, p1.x, p1.y, mpaint) ;}for (int I = 0; I <prices. size (); I ++) {PointF p = getStartPostion (I, prices. get (I ). getPrice (); pointFs. add (p); mpaint. setColor (Color. WHITE); // mpaint. setColor (Color. argb (0x66, 0xff, 0x66, 0x00); // set the paint brush color mpaint. setStyle (Paint. style. FILL); RectF mArc = new RectF (p. the x-UIUtil.dp2pxInt (context _, 4), p. the y-UIUtil.dp2pxInt (context _, 4), p. x + UIUtil. dp2pxInt (context _, 4), p. y + UIUtil. dp2pxInt (context _, 4); canvas. drawArc (mArc, 0f, 360f, true, mpaint); mpaint. setColor (Color. argb (0x44, 0xff, 0x66, 0x00); canvas. drawArc (mArc, 0f, 360f, true, mpaint); mpaint. setColor (getResources (). getColor (R. color. orange); // set the paint brush color mpaint. setStyle (Paint. style. STROKE); RectF mArc1 = new RectF (p. the x-UIUtil.dp2pxInt (context _, 4), p. the y-UIUtil.dp2pxInt (context _, 4), p. x + UIUtil. dp2pxInt (context _, 4), p. y + UIUtil. dp2pxInt (context _, 4); canvas. drawArc (mArc1, 0f, 360f, true, mpaint) ;}} public void drawPointText (Canvas canvas Canvas) {mpaint. setColor (Color. BLACK); mpaint. setTextSize (pricetipsize); mpaint. setStyle (Paint. style. FILL); mpaint. setStrokeWidth (2); for (int I = 0; I <pointFs. size (); I ++) {PointF p = pointFs. get (I); canvas. drawText (String. valueOf (prices. get (I ). getPrice (), p. x-(getDefaultTipSizeWidth ()/2), p. y-UIUtil.dp2pxInt (context _, 8), mpaint); // 8 is the distance from the center to the top 5 + 3} public void drawCanvasBg (Canvas canvas) {mpaint. setStyle (Paint. style. FILL); mpaint. setColor (Color. argb (0x44, 0xff, 0x66, 0x00); Path path = new Path (); for (int I = 0; I <pointFs. size (); I ++) {PointF p = pointFs. get (I); path. lineTo (p. x, p. y); if (I = pointFs. size ()-1) {path. lineTo (p. x, heigth * 3/4); path. lineTo (pointFs. get (0 ). x, heigth * 3/4); path. lineTo (pointFs. get (0 ). x, pointFs. get (0 ). y) ;}} canvas. drawPath (path, mpaint);} public void drawCanvasButtom (Canvas canvas) {mpaint. setStyle (Paint. style. FILL); mpaint. setColor (Color. BLACK); mpaint. setTextSize (pricetipsize); mpaint. setStrokeWidth (3); for (int I = 0; I <= pointFs. size (); I ++) {int buttomwidth = width/pointFs. size (); mpaint. setColor (getResources (). getColor (R. color. line); canvas. drawLine (buttomwidth * I, heigth * 3/4, buttomwidth * I, heigth * 3/4 + getdefascale Scale () * UIUtil. dp2pxFloat (context _, 10), mpaint); mpaint. setColor (getResources (). getColor (R. color. text2); if (I = pointFs. size () {return;} canvas. drawText (prices. get (I ). getYear (), buttomwidth * I + (buttomwidth-getdefascale Scale () * UIUtil. dp2pxInt (context _, 38)/2, heigth * 3/4 + pricetipsize + UIUtil. dp2pxInt (context _, 10) * getdefascale Scale (), mpaint);} public float getDeviationPrice () {// gets the direct difference between the maximum and minimum values. float max = 0; for (ItemInfo info: prices) {if (info. price> = max) {max = info. price ;}return max-getMinPrice ();} public float getMinPrice () {// get the direct difference between the maximum and minimum values float min = Integer. MAX_VALUE; for (ItemInfo info: prices) {if (info. price <= min) {min = info. price ;}} return min;} public PointF getStartPostion (int postion, float price) {PointF p = new PointF (); int contentheight = heigth * 2/4-(pricetipsize + UIUtil. dp2pxInt (context _, 10); p. y = contentheight-(price-getMinPrice () * contentheight/getDeviationPrice () + (pricetipsize + UIUtil. dp2pxInt (context _, 10); // This 10dp is left white by default p. x = width/prices. size () * postion + width/(2 * prices. size (); return p;} public int getdefatitipsize () {return (int) (getdefascale Scale () * UIUtil. dp2pxInt (context _, 15);} public int getdefadefatipsizewidth () {// 30 indicates the default value return (int) (getdefascale Scale () * UIUtil on the prd. dp2pxInt (context _, 30);} public int getDefaultButtomHeight () {// 30 indicates the default value of return (int) (getDefaultScale () * UIUtil on the prd. dp2pxInt (context _, 10); // 10dip} public float getdefascale Scale () {return (heigth * 3/4)/(float) UIUtil. dp2pxInt (context _, DEFAULTWIDTH);} public static class ItemInfo {private float price; private String year; public ItemInfo (float price, String year) {this. price = price; this. year = year;} public String getYear () {return year;} public void setYear (String year) {this. year = year;} public float getPrice () {return price;} public void setPrice (float price) {this. price = price ;}}}