2

I'm trying to make my image circular(NOTE: not trying to make ImageView circular but the Image) and fill it with a background color. my fragment code:

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.activity_profile, container, false);
    ImageView mView = (ImageView) rootview.findViewById(R.id.dp);
    RoundedBitmapDrawable im = RoundedBitmapDrawableFactory.create(getActivity().getResources(), BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.my_image));
    im.setCircular(true);
    im.setColorFilter(ContextCompat.getColor(getContext(), R.color.colorAccent), PorterDuff.Mode.DST_OVER);
    mView.setImageDrawable(im);
    return rootview;
}

all I get is a non-circular image with no background color on running the app.

EDIT:I achieved the results using

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:src="@drawable/profile"
    app:civ_border_width="2dp"
    app:civ_border_color="#FF000000"/>

But I still want to know why the first code did not work.

EDIT :
I also tried im.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 2.0f); but to no avail

EDIT 2:
I was using Glide(and I really feel stupid for leaving out this information from the question) to display images from firebase storage and it turns out that images from glide are not bit-drawable but transition-drawable and thats why the code did not work

SynAck
  • 427
  • 5
  • 19

2 Answers2

1

You can use drawable to make your ImageView circular. Create new xml file in drawable folder.

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="oval">
    <solid
        android:color="@color/myColor">
    </solid>

</shape>

And implement your color in color.xml

<color name="myColor">#ffffff</color>

Finally set background to your ImageView Hope it helps.

Cagri Yalcin
  • 402
  • 4
  • 13
  • 1
    Then you can check [this](https://stackoverflow.com/a/11376610/7482319) answer. This may allow you to manage your drawable file. – Cagri Yalcin Mar 12 '18 at 14:51
  • 1
    for setting bacground color dynamically https://stackoverflow.com/questions/1466788/android-textview-setting-the-background-color-dynamically-doesnt-work might be useful. – Muahmmad Tayyib Mar 12 '18 at 15:01
0

I was using Glide(and I really feel stupid for leaving out this information from the question) to display images from firebase storage and it turns out that images from glide are not bit-drawable but transition-drawable and thats why the code did not work

SynAck
  • 427
  • 5
  • 19