0

我有一个自定义视图,我在其中绘制饼图。该图表在 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));

}

4

1 回答 1

0

当您实例化PieChart对象时,您将其传递给一个数组,value_degree. 使用此数组的大小来预先确定大小或您的RectF. 您可以在 2(即 )之间实现恒定关系rectfHeight = 5 * value_degree.lenght,或创建某种可变动态关系。

于 2012-10-21T09:25:25.223 回答