如何在Android上移动图形或图表? [英] How to have a Moving graph or chart in android?

查看:104
本文介绍了如何在Android上移动图形或图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和我需要知道如何使图形或图表(如折线图)移动即,由右至左。

I am new to android and I need to know how to make a graph or chart (eg. line chart) move i.e, from right to left.

基本上我要绘制按照手机的RAM内存使用情况的图表。我需要绘制图形类似present在Windows的任务管理器。

Basically I am going to draw the graph in accordance with the RAM Memory Usage of the phone. I need to draw a graph similarly present in Task Manager of Windows.

请在这方面的帮助。

推荐答案

这是我的样本code。

It's My Sample Code.

我还没有测试。

但我认为这code正常工作。

But I think This code works fine.

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

这篇关于如何在Android上移动图形或图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆