Friday, 10 June 2016

Rounded Image with Glide

Glide is a fast and efficient open source image loading framework for Android that wraps memory and disk caching, and resource pooling into a simple and easy to use interface.
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs.
Glide includes a flexible API that allows developers to plug in to almost any network stack. By default Glide uses a custom HttpUrlConnection based stack, but also includes utility libraries plug in to Google's Volley project or Square's OkHttp library instead.
Glide's primary focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.

Download
You can download a jar from GitHub's releases page.
Or use Gradle:

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:19.1.0'
}

Basic
The basic way to load a remote image into an ImageView with Glide is quite simple:

Glide.with(context)  //you can pass either context or activity or fragment
    .load("YOUR_IMAGE_URL") //or fragmentactivity,Glide accepts everything
    .into(YOUR_IMAGEVIEW);

For Circular ImageView, you can use RoundedBitmapDrawable with Glide as follows:
(No Custom ImageView is required for this)

 Glide.with(context).load(YOUR_IMAGE_URL).asBitmap().centerCrop().into(new BitmapImageViewTarget(YOUR_IMAGEVIEW) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            YOUR_IMAGEVIEW.setImageDrawable(circularBitmapDrawable);
        }
    });
This will make your imageview look circular.

That's all.. and always code to make it better.. :)


No comments:

Post a Comment