在android中动态划分一个圆圈

时间:2013-03-22 09:19:15

标签: android custom-controls

我在Android应用程序中工作,我有一个蜇的列表。如果字符串列表包含3个字符串,我必须将圆分成3个相等的部分,并将三个字符串绑定在圆的分割区域中。我怎样才能做到这一点。我应该使用哪个小部件来制作这个圈子。请帮助我enter image description here

4 个答案:

答案 0 :(得分: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)绘制文本。根据您的需要进行修改。在视图上添加动画。

enter image description here

答案 1 :(得分:0)

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

enter image description here

答案 2 :(得分: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);
                }
            }
        }

    }
}

答案 3 :(得分: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);
    }
}