友達の助けが必要です。
これが私の活動です
public class MainActivity extends Activity {
float values[] = { 700, 400, 100, 500, 600 };
float values1[] = { 70, 40, 10, 50 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout lv1 = (LinearLayout) findViewById(R.id.linear);
values = calculateData(values);
values1 = calculateData(values1);
MyGraphview graphview = new MyGraphview(this, values);
graphview.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv1.addView(graphview, 0);
MyGraphview1 graphview1 = new MyGraphview1(this, values1);
graphview1.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv1.addView(graphview1, 0);
}
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(150, 150, 350, 350);
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);
}
}
}
}
public class MyGraphview1 extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
RectF rectf = new RectF(170, 370, 330, 530);
float temp = 0;
public MyGraphview1(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です
<RelativeLayout 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" >
<LinearLayout
android:id="@+id/linear"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</RelativeLayout>
そしてこれが私の結果です
しかし、期待される結果は
私のコードフレンドの何が問題になっていますか。私を助けてください。前もって感謝します。
パラメータを削除すると、次のようになります
addView(view); インデックスを削除します
MyGraphview graphview = new MyGraphview(this, values);
// graphview.setLayoutParams(new LinearLayout.LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv1.addView(graphview);
MyGraphview1 graphview1 = new MyGraphview1(this, values1);
// graphview1.setLayoutParams(new LinearLayout.LayoutParams(
// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lv1.addView(graphview1);
次に、私の出力は次のとおりです。