Search code examples
androidfacebookbitmapimageviewprofile-picture

How to create circular facebook profile picture


I'm having trouble using this library to make one of my images into a circle dynamically. Here is my attempt:

private void drawerSetup() {
    Profile profile = Profile.getCurrentProfile();
    ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profile_image);
    CircularImageView circularProfilePicture = (CircularImageView)    findViewById(R.id.profile_image_circle);
    if(profilePictureView != null) {
        profilePictureView.setProfileId(profile.getId());
        ImageView imageView = ((ImageView)profilePictureView.getChildAt(0));
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        circularProfilePicture.setImageBitmap(bitmap);
    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/tools"
    android:background="@drawable/side_nav_bar"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom"
    app:showIn="@layout/activity_news_feed">

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/profile_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    facebook:com_facebook_preset_size="normal"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingBottom="8dp"
    android:layout_centerInParent="true" />

<com.mikhaellopez.circularimageview.CircularImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:border_color="#EEEEEE"
    app:border_width="2dp"
    app:shadow="true"
    app:shadow_radius="10"
    android:id="@+id/profile_image_circle"
    app:shadow_color="#000000"
    android:layout_alignBottom="@+id/profile_image"
    android:layout_toLeftOf="@+id/profile_image"
    android:layout_toStartOf="@+id/profile_image" />

I know that my profilePictureView is displaying correctly when i call profilePictureView.setProfileId(profile.getId()); On my layout, it displays correctly. However, when I try to call the circularProfilePicture, I simply get an "empty" photo. It doesn't seem like the bitmap for the image isn't being recognized/set correctly. The image doesn't display as the profilePictureView does. Any ideas why this might happen?


Solution

  • After using different image library (including Picasso), I finished to use Facebook one named Fresco. It's much faster with less code, everyting works as it should.

    Fresco support:

    • streaming of progressive JPEGs
    • display of animated GIFs and WebPs
    • extensive customization of image loading and display

    The doc says too that

    In Android 4.x and lower, Fresco puts images in a special region of Android memory. This lets your application run faster - and suffer the dreaded OutOfMemoryError much less often.

    It also support rounded corner as you are searching, see here.

    Layout example:

            <com.facebook.drawee.view.SimpleDraweeView
                android:id="@+id/avatarImageView"
                android:layout_width="50dp"
                android:layout_height="50dp"
                fresco:placeholderImageScaleType="centerCrop"
                fresco:placeholderImage="@drawable/photo_placeholder"
                fresco:roundAsCircle="true"/>
    

    Note: don't forget to call Fresco.initialize(this); somewhere (normally in your Application class).

    I should also notice that Fresco currently add 2.6Mb to your application using ProGuard. You may choose to use another library such as Glide if you want less functionality.