Android:自定义视图中的RectF参数
我在绘制饼图时有一个自定义视图。图表是用RectF绘制的。但是如果我想创建RectF,我需要设置xLeft,xRight,yTop,yBottom参数…但是我不知道这个参数!!我的自定义视图将包含在带有高度和宽度填充的线性布局中。那怎么做呢Android:自定义视图中的RectF参数,android,android-layout,android-custom-view,Android,Android Layout,Android Custom View,我在绘制饼图时有一个自定义视图。图表是用RectF绘制的。但是如果我想创建RectF,我需要设置xLeft,xRight,yTop,yBottom参数…但是我不知道这个参数!!我的自定义视图将包含在带有高度和宽度填充的线性布局中。那怎么做呢 public class PieChart extends View { private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); private float[] value_degree; privat
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);
}
}
}实例化
PieChart
对象时,向其传递一个数组,值\u度
。使用此数组的大小预先确定您的RectF
的大小。您可以在2之间实现一个常量关系(即,rectfHeight=5*value\u degree.lenght
),或者创建某种类型的变量动态关系
@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));