Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing 2 circles on a canvas

I'm trying to draw two circles like this:

enter image description here

This is how I'm trying to do it:

Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

Canvas c = new Canvas(bmp);

RectF rect = new RectF(0,0,width,width);
Paint paint = new Paint();
drawCircles(paint, c, width, height, width);
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bmp);
img.setScaleType(ScaleType.FIT_CENTER);

And here is my drawCircles() method:

private void drawCircles(Paint paint, Canvas c, int width, int height, int radius) {
        paint.setARGB(255, 255 , 10, 21);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setStyle(Paint.Style.STROKE);
        if(width < height && radius == 0){
            radius = width/2;
            height = width;
        } else if (radius == 0){
            radius = height/2;
            width = height;
        }
        Paint paint2 = new Paint();
        paint2.setARGB(255, 255 , 10, 21);
        paint2.setStrokeWidth(10);
        paint2.setAntiAlias(true);
        paint2.setStrokeCap(Paint.Cap.BUTT);
        paint2.setStyle(Paint.Style.STROKE);
        c.drawCircle(width/2, height/2, radius-10, paint);
        c.drawCircle(width/2, height/2, 50, paint2);
}

I don't know why but I get only one circle, the small one (the one drawn with paint2). What can be the reason?

like image 697
dziwna Avatar asked Jan 21 '13 11:01

dziwna


People also ask

How do you make a half circle on canvas?

To create a semicircle with HTML5 Canvas, we can create an arc using the arc() method and define the ending angle has startAngle + PI.

How do you create multiple circles in JavaScript?

Anyway, If you want to have circles at random positions, you can utilize the Math. random() function. It returns a random number between 0 and 1. If you multiply that by the width / height of the canvas you'll have circles all around the canvas.


1 Answers

Try this code.Hope it may helps :)

public class SimpleCircleActivity extends Activity
{

       private CircleDemoView circledemoView ;

       public void onCreate(Bundle savedInstanceState)
       { 
        super.onCreate(savedInstanceState);

        circledemoView =new CircleDemoView(this);
        setContentView(circledemoView);

       }


       private class CircleDemoView extends View
       {
         public CircleDemoView(Context context)
         {
             super(context);
         }

         @Override
         protected void onDraw(Canvas canvas) 
         {

             super.onDraw(canvas);
             Paint p = new Paint();
             p.setColor(Color.RED);
             DashPathEffect dashPath = new DashPathEffect(new float[]{5,5}, (float)1.0);

             p.setPathEffect(dashPath);
             p.setStyle(Style.STROKE);


             for (int i = 0; i < 2; i ++) {
                 canvas.drawCircle(200, 200, 50+(i*40), p);
             }


             invalidate();

     }
     }

}
like image 158
AndroidLearner Avatar answered Oct 12 '22 23:10

AndroidLearner