我正在尝试绘制一个饼图,每个弧作为单独的自定义对象。但是只显示了弧的第一个切片。 LinearLayout确实具有垂直方向,因为它已在其他帖子中建议。 任何帮助将受到高度赞赏。
清单1自定义视图类(MySliceView.java):
package com.example.piechartsample;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.provider.SyncStateContract;
import android.view.View;
import android.widget.Toast;
public class MySliceView extends View implements View.OnClickListener {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private int[] COLORS = {Color.BLUE, Color.GREEN, Color.GRAY, Color.RED, Color.CYAN};
RectF rectf = new RectF(10, 10, 500, 500);
float temp = 0;
float sliceStartAngle, sliceSweepAngle;
int sliceColor;
RectF sliceRectf;
public MySliceView(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];
}
}
public MySliceView(Context context, RectF rectF, float startAngle, float sweepAngle, int color) {
super(context);
sliceRectf = rectF;
sliceStartAngle = startAngle;
sliceSweepAngle = sweepAngle;
sliceColor = color;
setOnClickListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(sliceColor);
canvas.drawArc(rectf, sliceStartAngle, sliceSweepAngle, true, paint);
}
@Override
public void onClick(View view) {
Toast.makeText(this.getContext(), this.getTag().toString(),
Toast.LENGTH_SHORT).show();
}
}
清单2布局(activity_slice_view.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliceViewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/slicePieText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slice Pie"
/>
</LinearLayout>
清单3:活动类(SliceViewActivity.java):
package com.example.piechartsample;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.RectF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
public class SliceViewActivity extends AppCompatActivity {
float dataValues[] = {300, 400, 100, 500, 200};
float sweepAngle[] = new float[dataValues.length];
float startAngle[] = new float[dataValues.length];
private int[] colors = {Color.BLUE, Color.GREEN, Color.GRAY, Color.RED, Color.CYAN};
RectF rectf = new RectF(10, 10, 500, 500);
private String TAG = "SliceViewActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slice_view);
LinearLayout linear = (LinearLayout) findViewById(R.id.sliceViewLayout);
linear.setOrientation(LinearLayout.VERTICAL);
calculateSweepAngle(dataValues);
calculateStartAngle(sweepAngle);
MySliceView slice1 = new MySliceView(this, rectf, startAngle[0], sweepAngle[0], colors[0]);
slice1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
slice1.setTag("Slice1");
linear.addView(slice1);
MySliceView slice2 = new MySliceView(this, rectf, startAngle[1], sweepAngle[1], colors[1]);
slice2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
slice2.setTag("Slice2");
linear.addView(slice2);
}
private void calculateSweepAngle(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++) {
sweepAngle[i] = 360 * (data[i] / total);
//Log.d(TAG, "calculateSweepAngle Post Calc Data " + i + ":" + sweepAngle[i]);
}
}
private void calculateStartAngle(float[] data) {
// TODO Auto-generated method stub
float startDegree = 0;
for (int i = 0; i < data.length; i++) {
if (i != 0) {
startDegree += data[i - 1];
}
startAngle [i] = startDegree;
//Log.d(TAG, "calculateStartAngle Post Calc Data " + i + ":" + startAngle[i]);
}
}
}