如何从其他类更新图形?

时间:2011-11-09 06:17:05

标签: android graphics

您好我正在尝试绘制一个折线图,为此我正在使用此代码。

DrawGraph dg = new DrawGraph(this);
    pane.addView(dg);
    dg.setData(10, 10, 100, 100);   //cords. for Firs Line
    pane.invalidate();
    dg.setData(100, 100, 100, 300);  //cords. for second Line
    pane.invalidate();

这是我的DrawGraph课程。

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

public class DrawGraph extends View{
Paint p;
int x1;
int y1;
int x2;
int y2;
Canvas fc;
int i = 0;

public DrawGraph(Context context) {
    super(context);
    p = new Paint();
    fc = new Canvas();
}

public void setData(int x1, int y1, int x2, int y2)
{
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
}

public void onDraw(Canvas c){
            c.drawLine(x1, y1, x2, y2, p);
   }
}

但问题在于,我只能看到图中的一行(第二行),我需要两行 我一次表示图中的所有行。 任何解决方案请。谢谢。

1 个答案:

答案 0 :(得分:0)

您已经调用了两次setData,因此第二次调用将覆盖您设置的第一个值。

如果您正在尝试绘制图形,可以使用AndroidPlot等库来为您完成繁重的工作。 (http://androidplot.com/wiki/Home)还有其他图书馆,但我个人使用的是AndroidPlot,它对我来说效果很好 - 我也必须在图表上绘制2行。

相关问题