2

我是 android 新手,我需要知道如何使图形或图表(例如折线图)移动,即从右到左。

基本上我会按照手机的RAM Memory Usage来绘制图表。我需要在 Windows 的任务管理器中绘制一个类似的图形。

请帮助解决这个问题。

4

3 回答 3

3

看看这堂课。

它简单实用。

原作者是 Arno den Hond,谷歌一下。

我重新设计了这个类。

import android.content.Context; 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;

/**
 * GraphView creates a scaled line or bar graph with x and y axis labels. 
 * @author Arno den Hond
 * @redesign Reinhard & kimkmkm
 *
 */
public class GraphView extends View {

public static boolean BAR = true;
public static boolean LINE = false;

private Paint paint;
private float[] values;
private String[] horlabels;
private String[] verlabels;
private String title;
private boolean type;

/**
 *  Generate Graph
 *  Variable Array for GraphView
 *  verlabel : Background Height Values
 *  horlabel : Background Width Values
 *  values : Max Values of Foreground Active Graph
 *  
 *  basic draw rule
 *  if Array is not null
 *  Draw Background width, height & active Graph all time
 *  or Array is null
 *  Draw Background width, height
 *  active Graph fixed 0
 *    
 */
public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels, boolean type) {
    super(context);
    if (values == null)
        values = new float[0];
    else
        this.values = values;
    if (title == null)
        title = "";
    else
        this.title = title;
    if (horlabels == null)
        this.horlabels = new String[0];
    else
        this.horlabels = horlabels;
    if (verlabels == null)
        this.verlabels = new String[0];
    else
        this.verlabels = verlabels;
    this.type = type;
    paint = new Paint();
}
/**
 *  Graph for Background
 *  &
 *  Value for Background
 */
@Override
protected void onDraw(Canvas canvas) {
    float border = 20;
    float horstart = border * 2;
    float height = getHeight();
    float width = getWidth() - 1;
    float max = 700;
    float min = 0;
    float diff = max - min;
    float graphheight = height - (2 * border);
    float graphwidth = width - (2 * border);

    paint.setTextAlign(Align.LEFT); 
    /** vers : BackGround Height Values length*/
    int vers = verlabels.length - 1; 
    for (int i = 0; i < verlabels.length; i++) {

        paint.setColor(Color.LTGRAY);
        // Width Line of background

        float y = ((graphheight / vers) * i) + border;
        // float y : ((getHeight / Height values length) * Height values length ) + 20

        canvas.drawLine(horstart, y, width, y, paint);
        // drawLine ( 40, y, getWidth()-1, y, paint)

        paint.setColor(Color.WHITE);
        // Left Height of background

        canvas.drawText(verlabels[i], 0, y, paint);
    }

    /** hors : BackGround width Values length*/
    int hors = horlabels.length - 1;
    for (int i = 0; i < horlabels.length; i++) {
        // Height Line of background
        paint.setColor(Color.DKGRAY); 
        float x = ((graphwidth / hors) * i) + horstart;
        canvas.drawLine(x, height - border, x, border, paint);
        paint.setTextAlign(Align.CENTER);
        if (i==horlabels.length-1)
            paint.setTextAlign(Align.RIGHT);
        if (i==0)
            paint.setTextAlign(Align.LEFT);
         // Value of Width
        paint.setColor(Color.WHITE); 
        canvas.drawText(horlabels[i], x, height - 4, paint);
    }

    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

    /**
     * Yellow Line Graph
     * continue Repaint....
     * 
     */
    if (max != min) {
        paint.setColor(Color.YELLOW);
        if (type == BAR) {
            float datalength = values.length;
            float colwidth = (width - (10 * border)) / datalength;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;

                float rat = val / diff;
                //diff : max - min

                float h = graphheight * rat;
                canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);
            }
        } else {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            float halfcol = colwidth / 2;
            float lasth = 0;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                if (i > 0)
                    canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
                lasth = h;
            }
        }
    }
}
于 2012-06-12T08:47:17.043 回答
2

这是我的示例代码。

我还没有测试。

但我认为这段代码工作正常。

public class DrawGraph extends Activity{
    /**
     * Variable Array for GraphView
     * verlabel : Background Height Values
     * horlabel : Background Width Values
     * values : Max Values of Foreground Active Graph
     */
    private float[] values = new float[60];
    private String[] verlabels = new String[] { "600","500","400","300","200","100","80","60","40","20","0", };
    private String[] horlabels = new String[] { "0","10", "20", "30", "40", "50", "60"};
    private GraphView graphView;
    private LinearLayout graph;
    private boolean runnable = false;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        graph = (LinearLayout)findViewById(R.id.graph);
        graphView = new GraphView(DrawGraph.this, values, "TEST GRAPH", horlabels, verlabels, GraphView.LINE);
        graph.addView(graphView);
        runnable = true;
        startDraw.start();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        runnable = false;
    }

    public void setGraph(int data){
        for(int i=0; i<values.length-1; i++){
            values[i] = values[i+1];
        }

        values[values.length-1] = (float)data;
        graph.removeView(graphView);
        graph.addView(graphView);
    }        

    public Handler handler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg){
            switch(msg.what){

            case 0x01:
               int testValue = (int)(Math.random() * 600)+1;
               setGraph(testValue);
               break;
            }
        }
    }

    public Thread startDraw = new Thread(){
        @Override
        public void run(){
            while(runnable){
                handler.sendEmptyMessage(0x01);
                try{
                    Thread.sleep(1000);
                } catch (Exception e){
                     e.printstacktrace();
                }
            }
        }
    }
于 2012-06-13T09:35:28.223 回答
0

试试AndroidPlot,这是一个简单易用的库!

另请参阅chartengine

于 2012-06-12T08:57:14.570 回答