我需要绘制饼图(动态值).如何在不使用第三方API的情况下创建图表.
@R_502_6120@:
下面显示了简单的饼图,您必须扩展更多功能… values []和colors数组应该相等….
public class Demo extends Activity { /** Called when the activity is first created. */ float values[]={300,400,100,500}; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); linearLayout linear=(linearLayout) findVIEwByID(R.ID.linear); values=calculateData(values); linear.addVIEw(new MyGraphvIEw(this,values)); } private float[] calculateData(float[] data) { // Todo auto-generated method stub float total=0; for(int i=0;i<data.length;i++) { total+=data[i]; } for(int i=0;i<data.length;i++) { data[i]=360*(data[i]/total); } return data; } public class MyGraphvIEw extends VIEw { private Paint paint=new Paint(Paint.ANTI_AliAS_FLAG); private float[] value_degree; private int[] colorS={color.BLUE,color.GREEN,color.GRAY,color.CYAN,color.RED}; RectF rectf = new RectF (10, 10, 200, 200); 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++) {//values2.length; i++) { if (i == 0) { paint.setcolor(colorS[i]); canvas.drawArc(rectf, 0, value_degree[i], true, paint); } else { temp += (int) value_degree[i - 1]; paint.setcolor(colorS[i]); canvas.drawArc(rectf, temp, value_degree[i], true, paint); } } } }}
总结 以上是内存溢出为你收集整理的在Android中绘制饼图?全部内容,希望文章能够帮你解决在Android中绘制饼图?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)