简体   繁体   中英

Unable to draw circle on bitmap?

I'm able to display the bitmap, but the circle in which I'm drawing doesn't display. I'm not sure what I'm missing.

private void loadImage() {
    File f = new File(imagesPath, currImageName);

    Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);
    canvas = new Canvas();
    canvas.drawCircle(60, 50, 25, paint);
    bitmapDrawable.draw(canvas);

    ImageView imageView = (ImageView)findViewById(R.id.imageview);
    imageView.setAdjustViewBounds(true);
    imageView.setImageDrawable(bitmapDrawable);
}

Your code is NOT drawing on the bitmap, rather is is drawing your bitmap into a canvas, then drawing a circle on that canvas's bitmap. The result is then thrown away. You then set your original bitmap (unaltered) into the ImageView.

You need to create the canvas with your bitmap. Then the draw method will draw on your bitmap.

    Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);

         // create canvas to draw on the bitmap
    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(60, 50, 25, paint);

    ImageView imageView = (ImageView)findViewById(R.id.imageview);
    imageView.setAdjustViewBounds(true);
    imageView.setImageBitmap(bitmap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM