在Android中绘制图形 [英] draw a graph in android

查看:183
本文介绍了在Android中绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是从这里取得的图表
http://writerbay.wordpress.com/2011/03/12/android-tutorial-displaying-chart-on-android/#comment-54



在Android中绘制图形。
用于绘制图形的数据取自DB。
在x轴上放数据,在y轴上放一些意味着速度的数字。



使用Async线程读取DB中的数据。



我要做的是以下操作:



*读取四个数据值和四个值将它们放在图表上



*读取以下四个值并更新我的图表....直到读取数据库中的数据



我面临的问题是:



LineView lv;我的图表有一个构造函数接受作为参数只有数组[]



所以我必须传递数组包含整个数据....这是反对我想要的

  public class InitTask extends AsyncTask< DBAdapter,String,Void> {

String TABLE_3;

protected Void doInBackground(DBAdapter ... db){
try {
db [0] .createDatabase();
db [0] .openDataBase();
Cursor c = db [0] .getCursor3(db [0] .TABLE_3,user_id);


viteza = new float [4]
time = new String [4];

if(c.moveToFirst()){

do {
int a =(int)Double.parseDouble(c.getString(3));

publishProgress(Integer.toString(a),c.getString(4));
Thread.sleep(500);


} while(c.moveToNext());

}
c.close();
db [0] .close();

} catch(Exception e){
Log.d(Eroare,doInBackground,e);
}

return null;
}

protected void onProgressUpdate(String ... values){

Aitem items [] = new Aitem [1];

viteza [contor] = Float.parseFloat(values [0]);

time [contor] = values [1];


items [0] = new Aitem(Color.RED,Evolution,viteza);


lv.setAxisValueX(time);

lv.setItems(items);


setContentView(lv);
}

}

time是一个数组,我放在x轴上的数据必须是一个字符串



viteza - 用于速度,也是一个包含浮点数的数组。



这是我如何取得图:



items [0] = new Aitem(Color.RED,Evolution,viteza) ;



问题是Aitem()构造函数需要数组[ ]字符串作为参数....我找不到一个方法来更新该数组[]与新的值(四个vnew值一步一步)



忘记



因此,在某一时刻,我的图形上只显示四个值....



<问题是,我不知道该怎么做...



有人有任何线索我该怎么做...谢谢u! / p>

EDIT:contor = 0的初始值

解决方案

选项,我可以看到。第一个将更容易实现,但更难的性能,这是妻子的显示和绘制图形,再次阅读尽可能多的你想要的。第二个选项是读取4个点并绘制这些,然后在下一个4中读取,并在新行中绘制这些。



- = EDIT = -

  public class InitTask extends AsyncTask< DBAdapter,Double,Void> 
{
String TABLE_3;

protected Void doInBackground(DBAdapter ... db)
{
try
{

//以前的

do
{
double x1 = Double.parseDouble(c.getString(1));
double y1 = Double.parseDouble(c.getString(2));
double x2 = Double.parseDouble(c.getString(3));
double y2 = Double.parseDouble(c.getString(4));
双x3 = Double.parseDouble(c.getString(5));
double y3 = Double.parseDouble(c.getString(6));
double x4 = Double.parseDouble(c.getString(7));
double y4 = Double.parseDouble(c.getString(8));

publishProgress(x1,y1,x2,y2,x3,y3,x4,y4);
Thread.sleep(500);
} while(c.moveToNext());

//像之前一样

return;
}

protected void onProgressUpdate(Double ... values)
{
double x1 = values [0];
double y1 = values [1];
double x2 = values [2];
double y2 = values [3];
double x3 = values [4];
double y3 = values [5];
double x4 = values [6];
double y4 = values [7];
}


I'm using a chart taken from here http://writerbay.wordpress.com/2011/03/12/android-tutorial-displaying-chart-on-android/#comment-54

to draw a graph in android. The data used for drawing the graph is taken from a DB. On x axis I put the data and on the y-axis I put some numbers meaning the speed.

The data from DB is readed using Async thread.

What I wanna do is the following:

*read four values for data and four values for speed and put them on the chart

*reading the following four values and update my chart....until the data from the DB is readed

The problem I'm facing is that:

LineView lv; my chart has a constructor that accepts as parameters only arrays[]

So I have to pass it the array containing the whole data....which is against what I want to do.

public class InitTask extends AsyncTask<DBAdapter,String, Void> {

    String TABLE_3;

    protected Void doInBackground(DBAdapter... db) {
        try {
            db[0].createDatabase();
            db[0].openDataBase();
            Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id);


            viteza = new float[4];
            time = new String[4];

            if (c.moveToFirst()) {

                do {
                    int a=(int)Double.parseDouble(c.getString(3));

                    publishProgress(Integer.toString(a),c.getString(4));
                    Thread.sleep(500);


                } while (c.moveToNext());

            }
            c.close();
            db[0].close();

        } catch (Exception e) {
            Log.d("Eroare", "doInBackground", e);
        }

        return null;
    }

    protected void onProgressUpdate(String...values) {

        Aitem items[] = new Aitem[1];

      viteza[contor]=Float.parseFloat(values[0]);

     time[contor]=values[1];


        items[0]=new Aitem(Color.RED, "Evolution",viteza);


        lv.setAxisValueX(time);

    lv.setItems(items);


        setContentView(lv);
        }

}

time is an array-containing my data which I put on the x-axis it must be a String

viteza-is for speed and is also an array which contains float numbers.

This is how I obtaing the graph:

items[0]=new Aitem(Color.RED, "Evolution",viteza);

lv.setItems(items);

The problem is that the Aitem() constructor needs arrays[] String as parameters....and I cannot find a way to update that array[] with the new values(four vnew values step by step)

forgetting about the old ones..

So at an moment in time only four values are displayed on my graph....

The problem is that I don't know how to do that...

Has anyone any clue of how could I do that...Thank u!

EDIT:initial value for contor=0

解决方案

You have two options that I can see. The first would be easier to implement but harder on performance and that is to wife the display and draw the graph all over again reading in as many as you want. The second option would be to read in 4 points and draw those and then read in the next 4 and draw those in a new line and so on.

-= EDIT =-

public class InitTask extends AsyncTask<DBAdapter, Double, Void> 
{
    String TABLE_3;

    protected Void doInBackground(DBAdapter... db)
    {
        try
        {

            // Like before

            do
            {
                double x1 = Double.parseDouble(c.getString(1));
                double y1 = Double.parseDouble(c.getString(2));
                double x2 = Double.parseDouble(c.getString(3));
                double y2 = Double.parseDouble(c.getString(4));
                double x3 = Double.parseDouble(c.getString(5));
                double y3 = Double.parseDouble(c.getString(6));
                double x4 = Double.parseDouble(c.getString(7));
                double y4 = Double.parseDouble(c.getString(8));

                publishProgress(x1, y1, x2, y2, x3, y3, x4, y4);
                Thread.sleep(500);
            } while (c.moveToNext());

            // Like before

            return;
    }

protected void onProgressUpdate(Double...values)
{
    double x1 = values[0];
    double y1 = values[1];
    double x2 = values[2];
    double y2 = values[3];
    double x3 = values[4];
    double y3 = values[5];
    double x4 = values[6];
    double y4 = values[7];
}

这篇关于在Android中绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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