直接上代码。。。。
public class CastView7 extends View{
private Paint linePaint;//坐标系的直线画笔
private Paint zhexianPaint;//折现画笔
private Paint dianPaint;//点的画笔
private Paint textPaint;//文字的画笔
private int x=0;
private int y=0;
//用来装每个点的(x,y)
private List<Map<String, Integer>> list=new ArrayList<Map<String,Integer>>();
public final static String X="x";
public final static String Y="y";
public final static int DELL=50;
public CastView7(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
//获取屏幕宽高
x = wm.getDefaultDisplay().getWidth();
y = wm.getDefaultDisplay().getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(DELL,DELL,DELL,y-250, linePaint);//画X的正坐标线
int county=(x-250)/DELL;
for(int i=0;i<county/2;i++){ //循环画出横向的所有线和数字
canvas.drawLine(DELL,(y-250)-(DELL*i),x-DELL, (y-250)-(DELL*i), linePaint);
canvas.drawText(i+"", 30,(y-250)-(DELL*i), textPaint);
}
int countx=x/DELL;
for(int i=0;i<countx/2;i++){//画Y轴的数字
canvas.drawText(i+"", DELL+(100*i),y-220, textPaint);
}
for(int i=0;i<list.size();i++){//折现图和拐点
if(i>0)
canvas.drawLine(list.get(i-1).get(X), list.get(i-1).get(Y), list.get(i).get(X), list.get(i).get(Y), zhexianPaint);
canvas.drawCircle(list.get(i).get(X), list.get(i).get(Y), 5, dianPaint);
}
}
private void init(){
linePaint=new Paint();
linePaint.setAntiAlias(true);
linePaint.setColor(Color.BLACK);
zhexianPaint=new Paint();
zhexianPaint.setAntiAlias(true);
zhexianPaint.setColor(Color.RED);
dianPaint=new Paint();
dianPaint.setAntiAlias(true);
dianPaint.setStyle(Style.FILL);
dianPaint.setColor(Color.BLUE);
textPaint=new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setAntiAlias(true);
}
//留给activity调用的方法,重绘。
public void setList(int x,int y){
Map<String, Integer> map=new HashMap<String, Integer>();
map.put(X, x);
map.put(Y, y);
list.add(map);
invalidate();
}
}
Activity的代码:
public class Activity7 extends Activity{
private CastView7 cv7;
private int x=0;
private int y=0;
private Handler h=new Handler(){
public void handleMessage(android.os.Message msg) {
cv7.setList(msg.arg1, msg.arg2);//调用setlist重绘view
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity7);
cv7=(CastView7) findViewById(R.id.cv7);
WindowManager wm = (WindowManager)this
.getSystemService(Context.WINDOW_SERVICE);
x = wm.getDefaultDisplay().getWidth();
y = wm.getDefaultDisplay().getHeight();
new Thread(){
public void run() {
int countx=x/50;Log.i("zhangteng",countx/2+"");
for(int i=0;i<countx/2;i++){
int xx=50+(100*i);
int yy=((y-250)-(50*((int)(Math.random()*16))));
Message msg=new Message();
msg.what=1;
msg.arg1=xx;
msg.arg2=yy;
h.sendMessage(msg);
try {
sleep(1000);//一秒画一条
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
}
布局文件:
<?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" >
<com.zhangteng.castomview7.CastView7
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cv7"
/>
</LinearLayout>
最后别忘了设置清单文件,把手机设置成为横屏模式。