我有一个自定义视图,我在其中绘制饼图。该图表在 RectF 中绘制。但如果我想创建 RectF,我需要设置 xLeft、xRight、yTop、yBottom 参数……但我不知道这个参数!我的自定义视图将包含在具有高度和重量填充父级的线性布局中。那么该怎么做呢?
public class PieChart extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private int[] COLORS;
RectF rectf;
public PieChart(Context context, float[] values, int colors[]) {
super(context);
value_degree = new float[values.length];
COLORS = colors;
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);
rectf = new RectF(// I dont want to hardcode coordinates here);
for (int i = 0; i < value_degree.length; i++) {
paint.setColor(COLORS[i]);
canvas.drawArc(rectf, 270 + 90 * i, value_degree[i], true, paint);
}
}
}
在我的活动中
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.pie_chart);
linearLayout .addView(new PieChart(this, someValues, someColors));
}