最近做了一个类似天气预告的demo,其中有一个自定义折线图,在网上找了很多例子,没有一个合适的,于是只能参考别人的例子,根据自己的想法写了一套demo.
在做自定义控件之前,先熟悉一下paint和canvas的简单使用。
链接:http://blog.csdn.net/qq_33776306/article/details/52290729
大概的思路无非就是先画一个横纵坐标,然后根据点的X轴Y轴的坐标,进行画点画线画文字。
最后的效果图呢如下
然后接下来看定义的折线图控件了
<span style="color:#009900;">package com.yang.draw;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
class MyChartView extends View{
public static final int RECT_SIZE = 10;
//枚举实现坐标桌面的样式风格
public static enum Mstyle
{
Line,Curve
}
private Mstyle mstyle=Mstyle.Line;
private Point[] mPoints = new Point[100];
Context context;
int bheight=0;
HashMap<Double, Double> map;
ArrayList<Double> dlk;
int totalvalue=30;
int pjvalue=5;
String xstr,ystr="";//横纵坐标的属性
int margint=15;
int marginb=40;
int c=0;
int resid=0;
Boolean isylineshow;
/**
*
*/
public void SetTuView(HashMap<Double, Double> map,int totalvalue,int pjvalue,String xstr,String ystr,Boolean isylineshow)
{
this.map=map;
this.totalvalue=totalvalue;
this.pjvalue=pjvalue;
this.xstr=xstr;
this.ystr=ystr;
this.isylineshow=isylineshow;
}
public MyChartView(Context ct)
{
super(ct);
this.context=ct;
}
public MyChartView(Context ct, AttributeSet attrs)
{
super( ct, attrs );
this.context=ct;
}
public MyChartVi