How can I create a selectable circular ImageView
like in the current Google+ app used for the profile pictures?
This is what I refer to:
The image above is unselected and the below is selected.
I try to replicate the profile pictures 1 to 1.
My work so far:
loadedImage
is the Bitmap
which is displayed
mImageView.setBackground(createStateListDrawable());
mImageView.setImageBitmap(createRoundImage(loadedImage));
The used methods:
private Bitmap createRoundImage(Bitmap loadedImage) {
Bitmap circleBitmap = Bitmap.createBitmap(loadedImage.getWidth(), loadedImage.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(loadedImage, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(loadedImage.getWidth() / 2, loadedImage.getHeight() / 2, loadedImage.getWidth() / 2, paint);
return circleBitmap;
}
private StateListDrawable createStateListDrawable() {
StateListDrawable stateListDrawable = new StateListDrawable();
OvalShape ovalShape = new OvalShape();
ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, shapeDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, shapeDrawable);
return stateListDrawable;
}
The size of the ImageView
is imageSizePx
and the size of the image is imageSizePx - 3
. So, that means the background should overlap the image. Which doesn't work.
Really simple solution, thanks to @CommonsWare for the tips.
Size of Bitmap
: imageSizePx - 3DP
Size of ImageView
: imageSizePx
mImageView.setBackground(createStateListDrawable(imageSizePx));
mImageView.setImageBitmap(loadedImage);
private StateListDrawable createStateListDrawable(int size) {
StateListDrawable stateListDrawable = new StateListDrawable();
OvalShape ovalShape = new OvalShape();
ovalShape.resize(size, size);
ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
shapeDrawable.getPaint().setColor(getResources().getColor(R.color.somecolor));
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, shapeDrawable);
stateListDrawable.addState(new int[]{android.R.attr.state_focused}, shapeDrawable);
stateListDrawable.addState(new int[]{}, null);
return stateListDrawable;
}
Assuming that by "selectable" you mean "checkable, like a CheckBox
", then that could be some CompoundButton
that is using a StateListDrawable
with regular and checked states for the background behind the "SkillOverflow" foreground image.
You can use uiautomatorviewer
to see what the actual widget is that Google+ uses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With