安卓:自定义视图RectF PARAMS [英] Android: RectF params in custom View
问题描述
我在那里我画饼图自定义视图。该图表中RectF绘图。但是,如果我想创建RectF我需要设置xLeft,xRight,yTop,yBottom参数...但是,我knwo这PARAMS!我的自定义视图将被纳入的LinearLayout与高度和怀特填充母。那么如何呢?
I have a custom view where I'm drawing pie chart. The chart is drawing in RectF. But if I want to create RectF I need to set xLeft,xRight,yTop,yBottom params...But I dont knwo this params !! My custom view will be included in linearLayout with height and wight fill parent. So how to be ?
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);
}
}
}
在我Actitivy
And in my Actitivy
@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));
}
推荐答案
在实例化饼图
对象传递给它一个数组中, value_degree
。使用该数组的大小,以predetermine大小或你的 RectF
。你可以实现的2(即 rectfHeight = 5 * value_degree.lenght
)的恒定关系,或者建立某种形式的一个变量的动态关系。
When you instantiate the PieChart
object you pass it an array, the value_degree
. Use the size of this array to predetermine the size or your RectF
. You could implement a constant relation in between the 2 (ie. rectfHeight = 5 * value_degree.lenght
), or create some sort of a variable dynamic relation.
这篇关于安卓:自定义视图RectF PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!