2012-06-12 12 views
1

我是android新手,我需要知道如何制作图形或图表(例如折线图),即从右向左移动。如何在android中有一个移动的图形或图表?

基本上我会根据手机的RAM内存使用情况绘制图形。我需要绘制一个类似于Windows任务管理器中的图形。

请帮忙。

+0

我想你应该看看这个教程http://www.jjoe64.com/p/graphview-library.html 笔者givenn正确的事情来解释 –

回答

1

这是我的示例代码。

我还没有测试。

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

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(); 
       } 
      } 
     } 
    } 
+0

刚刚完成测试。它正在工作。 –

+0

@ reinhard.lee ...先生,我已经修好了,..感谢你的帮助..工作正常..非常谢谢你 – Sudarshan

+0

@ user1448299欢迎你。 –

1

看看这个类。

它很简单而且有用。

原作者是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; 
      } 
     } 
    } 
} 
+0

爵士u能解释一下与此发生代码... Atlest屏幕截图,所以我可以理解这个概念 – Sudarshan

+0

@ user1448299对不起,我忘了它。这是[link](http://cfile23.uf.tistory.com/image/2041E13A4F01569737FB1D)示例截图。此图正在移动和刷新。 –

+0

@ user1448299 and [Sample Graph Movie By Flash](http://tvpot.daum.net/clip/ClipViewByVid.do?vid=zJ5VrtWuiwo$) –

0

尝试AndroidPlot,这是一个简单易用的库!请参阅achartengine

+0

嗨,先生,嗨检查achartengine但在图表是静态的,直到我们滚动..但我需要选框样式中的图表 – Sudarshan

相关问题