There are two ways to achieve your desired effect:
Simply just override the original dimensions of the Android support
library by adding:
<dimen name="design_fab_image_size" tools:override="true">48dp</dimen>
to your dimens.xml
, this will automatically override the design_fab_image_size
set by the library (may not work with third party libraries) as noted in How to remove auto padding of FAB button in android?
Note: 48dp in this case is the width of the floating action button.
- Use an
ImageView
with a Circle Transformation
using the Picasso
Library instead of a FloatingActionButton
. You'll lose the simple Fab functionality and animations but you'll have more flexibility with setting the image correctly. As you're animating the FAB on button press (i.e. displaying all four floating action buttons, replacing it with an ImageView
of the same dimensions and attributes, would be easy to change to in your XML)
An example of the class would be:
public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "Circle";
}
}
and an example of using such technique with Picasso
would be:
Picasso.with(context).load(imageUri).transform(new CircleTransform()).into(imageView);
Note: .load()
can take a String URL or Resource ID
Update:
If you want a circle image inside a circle FloatingActionButton
, use both techniques but use your FloatingActionButton
instead. Transform the image and then set it to your FloatingActionButton
using Picasso
.