gpt4 book ai didi

android - 为什么在android中绘图有时会显示不同

转载 作者:行者123 更新时间:2023-11-30 02:56:08 25 4
gpt4 key购买 nike

很抱歉,我不知道如何提出问题,但您会从图片中理解。随意编辑标题。

当我运行程序时通常会显示:

但有时我会看到这个:

为什么会这样?我没有发布代码,因为我没有做任何动态的事情,我总是使用相同的方法和相同的值,所以这并不重要,但如果你需要,请告诉我。

有什么想法吗?

编辑:

代码:

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);
}

}
}
}

Activity :

    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;
}

}

最佳答案

将这部分代码移动到 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));

关于android - 为什么在android中绘图有时会显示不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23229880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com