与画布油漆的错误对齐饼图

时间:2014-02-10 18:27:42

标签: android charts android-canvas

我使用以下代码生成饼图,但图表不在屏幕视图中。 任何人都可以告诉我需要做哪些更改才能在屏幕内完成所有操作吗?

package com.example.chartexample;

import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

float values[] = { 700, 400, 100, 500, 600 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout lv1 = (LinearLayout) findViewById(R.id.linear);

    values = calculateData(values);
    MyGraphview graphview = new MyGraphview(this, values);
    lv1.addView(graphview);
}

private float[] calculateData(float[] data) {
    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;
    RectF rectf = new RectF(120, 120, 380, 380);
    float 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) {
        super.onDraw(canvas);
        Random r;
        for (int i = 0; i < value_degree.length; i++) {
            if (i == 0) {
                r = new Random();
                int color = Color.argb(100, r.nextInt(256), r.nextInt(256),
                        r.nextInt(256));
                paint.setColor(color);
                canvas.drawArc(rectf, 0, value_degree[i], true, paint);
            } else {
                temp += value_degree[i - 1];
                r = new Random();
                int color = Color.argb(255, r.nextInt(256), r.nextInt(256),
                        r.nextInt(256));
                paint.setColor(color);
                canvas.drawArc(rectf, temp, value_degree[i], true, paint);
            }
        }
    }
}
}

以下是我的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linear"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
</LinearLayout>



</LinearLayout>

我现在得到的结果是The present output

我需要它完全在屏幕内

0 个答案:

没有答案