5

I want to draw a circle on a canvas in my android app. I searched a lot and realized if I need a dynamic form of painting which can be updated time by time I need to use canvas instead of imageView.

any help is appreciated

this is the code that I wrote so far but it will not draw anything on the android device screen:

    private void createBitMap() {
    Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  //creates bmp
    bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
    Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

    Paint paint = new Paint();                          //define paint and paint color
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);
    //paint.setAntiAlias(true);

    canvas.drawCircle(50, 50, 10, paint);
}
Kara
  • 5,996
  • 16
  • 49
  • 56
Hossein Dolatabadi
  • 423
  • 3
  • 7
  • 13

2 Answers2

10

Update your createBitMap method like this

private void createBitMap() {
        // Create a mutable bitmap
        Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

        bitMap = bitMap.copy(bitMap.getConfig(), true);
        // Construct a canvas with the specified bitmap to draw into
        Canvas canvas = new Canvas(bitMap);
        // Create a new paint with default settings.
        Paint paint = new Paint();
        // smooths out the edges of what is being drawn
        paint.setAntiAlias(true);
        // set color
        paint.setColor(Color.BLACK);
        // set style
        paint.setStyle(Paint.Style.STROKE);
        // set stroke
        paint.setStrokeWidth(4.5f);
        // draw circle with radius 30
        canvas.drawCircle(50, 50, 30, paint);
        // set on ImageView or any other view 
        imageView.setImageBitmap(bitMap);

    }
Jitender Dev
  • 7,381
  • 2
  • 23
  • 37
2

try this

create ImageView and use image.setImageBitmap(bitMap); to make a bitmap to visible.

public class MainActivity extends Activity { ImageView image;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image=(ImageView)findViewById(R.id.imageView1);
        createBitMap();
    }

    private void createBitMap() {
        Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  //creates bmp
        bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
        Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

        Paint paint = new Paint();
        // smooths
       paint.setAntiAlias(true);
        paint.setColor(Color.RED);
       paint.setStyle(Paint.Style.STROKE); 
        paint.setStrokeWidth(4.5f);
        // opacity
        //p.setAlpha(0x80); //
        canvas.drawCircle(50, 50, 30, paint);
        image.setImageBitmap(bitMap);
    }
IgorGanapolsky
  • 24,768
  • 21
  • 112
  • 140
Nambi
  • 11,766
  • 3
  • 35
  • 49