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
  • 6,115
  • 16
  • 50
  • 57
Hossein Dolatabadi
  • 431
  • 3
  • 7
  • 13

1 Answers1

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
  • 26,189
  • 23
  • 116
  • 147
Nambi
  • 11,944
  • 3
  • 37
  • 49