Reputation: 121
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;
}
}
Upvotes: 0
Views: 789
Reputation: 16397
The title of your question is pretty misleading... does not sound like there is really any endless loop there.
You should initialize top and bottom in your onDraw function. So you start from the "top" of the screen every time you draw.
Also, you should try to not allocate a new Rect for every pie chart.
Try to allocate one, use offsetTo(50,100)
and then use offset(0, 450)
to move it down.
Calling invalidate() every time will just make your pie charts being drawn again and again...probably will not do much good.
Upvotes: 1
Reputation: 15434
top
and bottom
are instance variables. Try to make them local, move them to onDraw
method. Now you change them every time onDraw
called and they don't reset to top=100,bottom=500
after onDraw
finished.
Upvotes: 2