绘制折线图

  1 package com.example.canvas.view;

  2 

  3 import java.util.ArrayList;

  4 import java.util.List;

  5 import java.util.Random;

  6 

  7 import android.annotation.SuppressLint;

  8 import android.content.Context;

  9 import android.graphics.Canvas;

 10 import android.graphics.Color;

 11 import android.graphics.Paint;

 12 import android.os.Handler;

 13 import android.os.Message;

 14 import android.util.AttributeSet;

 15 import android.view.View;

 16 

 17 @SuppressLint({ "DrawAllocation", "HandlerLeak" })

 18 public class MyView extends View {

 19     private int XPoint = 60;

 20     private int YPoint = 260;

 21     private int XScale = 8; // 刻度长度

 22     private int YScale = 40;

 23     private int XLength = 380;

 24     private int YLength = 240;

 25 

 26     private int MaxDataSize = XLength / XScale;

 27 

 28     private List<Integer> data = new ArrayList<Integer>();

 29 

 30     private String[] YLabel = new String[YLength / YScale];

 31 

 32     private Handler handler = new Handler() {

 33         public void handleMessage(Message msg) {

 34             if (msg.what == 0x1234) {

 35                 MyView.this.invalidate();

 36             }

 37         };

 38     };

 39 

 40     public MyView(Context context, AttributeSet attrs) {

 41         super(context, attrs);

 42         // TODO Auto-generated constructor stub

 43         for (int i = 0; i < YLabel.length; i++) {

 44             YLabel[i] = (i + 1) + "M/s";

 45         }

 46 

 47         new Thread(new Runnable() {

 48             @Override

 49             public void run() {

 50                 while (true) {

 51                     try {

 52                         Thread.sleep(1000);

 53                     } catch (InterruptedException e) {

 54                         e.printStackTrace();

 55                     }

 56                     if (data.size() >= MaxDataSize) {

 57                         data.remove(0);

 58                     }

 59                     data.add(new Random().nextInt(4) + 1);

 60                     handler.sendEmptyMessage(0x1234);

 61                 }

 62             }

 63         }).start();

 64 

 65     }

 66 

 67     /*

 68      * (non-Javadoc)

 69      * 

 70      * @see android.view.View#onDraw(android.graphics.Canvas)

 71      * 

 72      * if(data.size() > 1){ Path path = new Path(); path.moveTo(XPoint, YPoint -

 73      * data.get(0) * YScale); for(int i=1; i<data.size(); i++){

 74      * path.lineTo(XPoint + i * XScale, YPoint - data.get(i) * YScale); }

 75      * canvas.drawPath(path, paint); }

 76      */

 77     @Override

 78     protected void onDraw(Canvas canvas) {

 79         // TODO Auto-generated method stub

 80         super.onDraw(canvas);

 81 

 82         Paint paint = new Paint();

 83         paint.setStyle(Paint.Style.STROKE);

 84         paint.setAntiAlias(true); // 去锯齿

 85         paint.setColor(Color.BLUE);

 86 

 87         // 画Y轴

 88         canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint);

 89 

 90         // Y轴箭头

 91         canvas.drawLine(XPoint, YPoint - YLength, XPoint - 3, YPoint - YLength

 92                 + 6, paint); // 箭头

 93         canvas.drawLine(XPoint, YPoint - YLength, XPoint + 3, YPoint - YLength

 94                 + 6, paint);

 95 

 96         // 添加刻度和文字

 97         for (int i = 0; i * YScale < YLength; i++) {

 98             canvas.drawLine(XPoint, YPoint - i * YScale, XPoint + 5, YPoint - i

 99                     * YScale, paint); // 刻度

100 

101             canvas.drawText(YLabel[i], XPoint - 50, YPoint - i * YScale, paint);// 文字

102         }

103 

104         // 画X轴

105         canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint);

106         System.out.println("Data.size = " + data.size());

107         if (data.size() > 1) {

108             for (int i = 1; i < data.size(); i++) {

109                 canvas.drawLine(XPoint + (i - 1) * XScale,

110                         YPoint - data.get(i - 1) * YScale, XPoint + i * XScale,

111                         YPoint - data.get(i) * YScale, paint);

112             }

113         }

114 

115     }

116 

117 }

 

你可能感兴趣的:(图)