0

I want to draw several pie chart by using a for loop, and the data is stored in an array (data[][][]). When each chart draw is completed, I make the top and bottom plus 450 (top+=450; bottom+=450;) to avoid the charts to overlap. But the result is that a pie chart moves to the bottom constantly. I try to add invalidate() at the end of line but it doesn't work.

private String data[][][]=... //store some data
private float left=50,top=100,right=450, bottom=500;

public void onDraw(Canvas c) {

    super.onDraw(c);
    Paint paint = new Paint();
    RectF rec;
    for(int j=0;j<data.length;j++){
        rec=new RectF(left,top,right,bottom);
        //draw a pie chart
        for (int i = 0; i < data[j].length; i++) {
            float drawDegree = (float)Integer.parseInt(data[j][i][1])/100* 360f;
            c.drawArc(rec, -90, drawDegree, true, paint);   
        }

        top+=450;
        bottom+=450;
    }           
}
4

2 回答 2

2

top并且bottom是实例变量。尝试使它们本地化,将它们移动到onDraw方法中。现在您每次onDraw调用时都会更改它们,并且它们不会在完成top=100,bottom=500后重置onDraw

于 2012-11-19T14:03:22.613 回答
1

您的问题的标题非常具有误导性……听起来那里确实没有无限循环。

您应该在 onDraw 函数中初始化顶部和底部。因此,您每次绘制时都从屏幕的“顶部”开始。

此外,您应该尽量不要为每个饼图分配一个新的 Rect。尝试分配一个,使用offsetTo(50,100),然后使用offset(0, 450)将其向下移动。

每次调用 invalidate() 只会让你的饼图一次又一次地被绘制......可能不会有多大好处。

于 2012-11-19T14:07:27.000 回答