对不起,我不知道如何制定问题,但你会从图片中理解。随意编辑标题。
当我运行程序时,通常会显示:
http://i57.tinypic.com/2csf94w.png
但有时候我会看到这个:
http://i59.tinypic.com/mlgprk.png
为什么会这样?我不发布代码,因为我不做任何动态我总是使用相同的方法使用相同的值,所以它并不重要,但如果你需要告诉我。
有什么想法吗?
修改
CODE:
public class MyGraphView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private int[] COLORS = { Color.GREEN, Color.RED };
// size of bigger half circle
RectF rectf = new RectF(10, 10, 310, 310);
// size of smaller half circle
RectF rectf2 = new RectF(45, 45, 275, 275);
// size of the smallest half circle
RectF rectf3 = new RectF(80, 80, 240, 240);
int temp = 0;
public MyGraphView(Context context, float[] values) {
super(context);
value_degree = new float[values.length];
for (int i = 0; i < values.length; i++) {
value_degree[i] = values[i];
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
for (int i = 0; i < value_degree.length; i++) {
// set type of "brush"
paint.setStrokeWidth(20);
paint.setStyle(Paint.Style.STROKE);
// agree
if (i == 0) {
final Path path = new Path();
paint.setColor(COLORS[i]);
// draw 3 paths to show 3 curves
path.addArc(rectf, 180, value_degree[i] - 4);
path.addArc(rectf2, 180, value_degree[i] - 5);
path.addArc(rectf3, 180, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);
// disagree
} else {
temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
final Path path = new Path();
path.addArc(rectf, temp + 180 + 4, value_degree[i] - 4);
path.addArc(rectf2, temp + 180 + 5, value_degree[i] - 5);
path.addArc(rectf3, temp + 180 + 6, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);
}
}
}
}
活动:
public class PieChart extends Activity {
float values[] = { 10, 20 };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pie_chart);
// pie chart assigned to linear layout
LinearLayout linear = (LinearLayout) findViewById(R.id.pieChartLL);
values = calculateData(values);
// draw the graf
linear.addView(new MyGraphView(this, values));
}
// for pie chart
private float[] calculateData(float[] data) {
// TODO Auto-generated method stub
float total = 0;
for (int i = 0; i < data.length; i++) {
total += data[i];
}
// 180 is the amount of degrees of the circle - setting it up to half
// circle 360 means full circle
for (int i = 0; i < data.length; i++) {
data[i] = 180 * (data[i] / total);
}
return data;
}
}
答案 0 :(得分:1)
将这部分代码移到onWindowFocusChanged()
方法帮助:
// pie chart assigned to linear layout
LinearLayout linear = (LinearLayout) findViewById(R.id.pieChartLL);
values = calculateData(values);
// draw the graf
linear.addView(new MyGraphView(this, values));