Circular image on android

0

Good, I am trying to design a user profile screen and I want the profile image to be circular. The original image in the drawable folder is square, so the idea is to modify it in the .java. My problem is that if I change the image and it becomes circular but it is superimposed on the original square image, so that at the end the circular remains on the square. My code is as follows: (I think it may be because I modified the image once the fragment was created, but I do not know how to do it before creating the fragment)

public ProfileFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_profile, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    ImageView imageView = (ImageView) view.findViewById(R.id.perfilId);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.perfil);
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),bitmap);
    roundedBitmapDrawable.setCircular(true);
    imageView.setImageDrawable(roundedBitmapDrawable);


     Button logout = view.findViewById(R.id.logout);

     TextView name = view.findViewById(R.id.name);
     name.setText(User.currentUser().getName());

     logout.setOnClickListener(this);
}
    
asked by Joaquín León Martínez 29.12.2017 в 22:22
source

1 answer

1

You can do it directly with XML in the following way:

// res/drawable/circle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="1.9"
    android:useLevel="false" >
    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="10dp"
        android:color="@android:color/white" />
</shape>

Then you make a LayerList and put the background image of the view.

// res/drawable/img.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/tu_imagen"/>
    <item android:drawable="@drawable/circle"/>

</layer-list>

And then you use it in in ImageView as background:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/img"/>

It would be something like this:

    
answered by 29.12.2017 в 23:50