1

I am working in an android application and I have a List of stings. If the List of string contains 3 string, I have to divide the circle into 3 equal parts and bind the three string in the divided area of the circle. How can I do this. Which widget should I use to make this circle. Please helenter image description herep me.

4

4 回答 4

1

这只是一个样本。您需要根据自己的需要进行修改。由于您要求提供示例,因此我已粘贴以下代码。

http://developer.android.com/training/custom-views/custom-drawing.html。关于绘图的文档。链接末尾有一个示例

使用 achartengine 很容易。http://www.achartengine.org/

对于使用 achartengine 的饼图。http://wptrafficanalyzer.in/blog/android-drawing-pie-chart-using-achartengine/

要绘制视图,您可以使用以下示例。

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyView mv= new MyView(this);
    setContentView(mv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
class MyView extends View
{
    Context c;
      private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private Paint mpaint,paint2;

    public MyView(Context context) {
        super(context);
        c= context;
        mpaint= new Paint();
        mpaint.setColor(Color.RED);
        mpaint.setStyle(Paint.Style.FILL);
        paint2 = new Paint();
        paint2.setColor(Color.GREEN);
        paint2.setStrokeWidth(10);
            mBitmapPaint = new Paint();
            mBitmapPaint.setColor(Color.RED);
        // TODO Auto-generated constructor stub
    }
      @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {
             Display display = ( (Activity) c).getWindowManager().getDefaultDisplay();  
            float w = display.getWidth(); 
            float h = display.getHeight();
           canvas.drawCircle(w/2, h/2, 350, mpaint);
           canvas.drawLine(w/2, h/2, 20, h/2, paint2);

        }
}
}

您可以使用 canvas.drawText(text, x, y, paint) 绘制文本。根据您的需要进行相同的修改。在视图上添加动画。

在此处输入图像描述

于 2013-03-22T12:02:45.297 回答
0

您可以使用AChartEngine来完成此操作。它有非常强大的方法来绘制饼图。

在此处输入图像描述

于 2013-03-22T09:25:35.513 回答
0
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);
                }
            }
        }

    }
}
于 2013-03-27T07:18:25.973 回答
0

您可以创建一个自定义视图,并在其中绘制一个圆圈并使用绘制线划分任何部分。

在您的 onDraw() 方法中使用此代码。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    this.canvas = canvas;

    int width = getWidth(); // get center point of view.
    // Draw circle
    Paint mPaintCircle = new Paint();
    mPaintCircle.setColor(Color.WHITE);
    mPaintCircle.setAntiAlias(true);
    mPaintCircle.setStyle(Paint.Style.STROKE);
    mPaintCircle.setStrokeWidth(5);

    canvas.drawCircle(width / 2, width / 2, width / 2, mPaintCircle);
    // Draw line
    Paint mPaintLine = new Paint();
    mPaintLine.setColor(Color.GREEN);
    mPaintLine.setStrokeWidth(5);

    //number of section you want to divide.
    int pointsTODraw = 8; 
    float pointAngle = 360 / pointsTODraw; //angle between points
    for (float angle = 0; angle < 360; angle = angle + pointAngle) { //move round the circle to each point
            float x = (float) (Math.cos(Math.toRadians(angle)) * radiusPart); //convert angle to radians for x and y coordinates
            float y = (float) (Math.sin(Math.toRadians(angle)) * radiusPart);
            canvas.drawLine(radiusPart, radiusPart, x + radiusPart, y + radiusPart, mPaintLine);
    }
}
于 2018-01-23T13:46:07.380 回答