How to get FAB with option without padding, but still you have elevation?

background

I have a small round view with a snapshot that I need to show to the user. If the photo is available, I have to show it, and the image is rounded to a circle and has a height.

If the photo is not available, I show an icon inside with a background, but still rounded and has a mark.

Problem

I managed to take a photo showing the work only on Android 5 and above:

enter image description here

But it has a bad color around the edges, as it tries to show the background FAB, and on Android 4.x and below it it shows as simple rectangular content that has nothing to do with FAB, probably because padding is what protects him from the shade.

I also need to add a stroke (a thin line of a certain color around a rounded image), but I'm not sure how to add it.

What i tried

This is in the layout file:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/image"
    android:layout_width="@dimen/test"
    android:layout_height="@dimen/test"/>

"test" is 80dp.

And this is in the code:

    FloatingActionButton floatingActionButton = (FloatingActionButton) view.findViewById(R.id.image);
    floatingActionButton.setPadding(0, 0, 0, 0);
    final Bitmap bitmap = ...
    RoundedCornersDrawable roundedCornersDrawable = new RoundedCornersDrawable(getResources(), bitmap, bitmap.getWidth() / 2);
    floatingActionButton.setImageDrawable(roundedCornersDrawable);

RoundedCornersDrawable code:

public class RoundedCornersDrawable extends BitmapDrawable {

    private final BitmapShader bitmapShader;
    private final Paint p;
    private final RectF rect;
    private final float borderRadius;

    public RoundedCornersDrawable(final Resources resources, final Bitmap bitmap, final float borderRadius) {
        super(resources, bitmap);
        bitmapShader = new BitmapShader(getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        final Bitmap b = getBitmap();
        p = getPaint();
        p.setAntiAlias(true);
        p.setShader(bitmapShader);
        final int w = b.getWidth(), h = b.getHeight();
        rect = new RectF(0, 0, w, h);
        this.borderRadius = borderRadius < 0 ? 0.15f * Math.min(w, h) : borderRadius;
    }

    @Override
    public void draw(final Canvas canvas) {
        canvas.drawRoundRect(rect, borderRadius, borderRadius, p);
    }
}

Question

How do I get FAB to work this way? How to disable debugging as desired for all versions of Android and still have a rounded image with shadow?

How to add a stroke to a rounded FAB?

+4
source share
1 answer

I think I wrote this post too quickly, although it seems that there is already about it here:

How to add shadow and border on Android circular view image?

I will look at him and see if he meets the requirements of what I need.

EDIT: , FAB ( ), , , FAB. .

, ....


EDIT: , : https://github.com/lopspower/CircularImageView

, , , .

FAB, , .

+1

Source: https://habr.com/ru/post/1620134/


All Articles