1

I was wondering if it's possible to draw a circle on an existing imageView ?

I want to create a circle on my image, here's my code :

public void drawCircle(ImageView image, int x, int y)
    {
        BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        float radius = 20;
        canvas.drawCircle(x, y, radius, paint);
        image.setImageBitmap(bitmap);
    }

But unfortunately, i've got the following error :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference

Can u tell me what's wrong with my code ?

Thanks !

Maître Van Diest
  • 215
  • 1
  • 3
  • 11

1 Answers1

1

you can write a XML for this to set a oval shape, like this

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="YOUR_COLOR" android:endColor="YOUR_COLOR"
        android:angle="270"/>
</shape>

add this to the drawable folder and then on your imageView set this as source or background as you prefer. make sure to use this inside another layout top, after that set height and width a same amount

visit this one too to get more idea How to create an imageView with shape circle and have a border?

Community
  • 1
  • 1