gitmyhub

CircleImageView

Java ★ 15k updated 2y ago

A circular ImageView for Android

A lightweight Android library that displays images as perfect circles, ideal for profile pictures and avatars, using an efficient technique that avoids memory duplication and rendering artifacts.

JavaAndroidsetup: easycomplexity 2/5

CircleImageView is a small Java library for Android developers who need to display images in a perfect circle. The most common use case is a profile picture, where a square photo needs to be clipped into a round shape before being shown on screen.

The library is built around a technique that avoids the most common pitfalls of cropping images on Android. It does not make a duplicate copy of the photo in memory, does not use a clipping path (which tends to look jagged and is slow to render), and does not draw the canvas twice. Instead, it uses something called a BitmapShader, a built-in Android mechanism that paints an image through a shape mask efficiently.

Adding it to an Android project is straightforward: you declare it as a dependency in your build file, then drop a custom view tag into your layout XML wherever you want a circular image. You can set a border around the circle by specifying a width in density-independent pixels and a color. The image is always cropped from the center and fills the circle, which works well for portraits and avatars.

There are a few known constraints. The cropping mode is fixed to center-crop and cannot be changed. The adjustViewBounds option is not compatible. If you use a popular image-loading library such as Picasso or Glide to download photos from the internet, you need to turn off their built-in fade-in animation, otherwise the circular crop can appear garbled during the transition. Vector graphics are also a poor fit for this component and the README explicitly recommends against using them here.

The project has been actively maintained across more than a dozen releases, adding things like padding support, elevation, a circle background color, and migration to AndroidX (the modern Android support library set). It is released under the Apache 2.0 license, which permits free use in both personal and commercial apps.

Where it fits