2

following up on this answer, how can i achieve the same but including a border that runs smoothly around the clipped ImageView.

I have tried using this as a background drawable for the ImageView with red borders and imageView.setClipToOutline(true).

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <corners
        android:radius="35" />
    <stroke
        android:width="2dp"
        android:color="#E42323" />
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp" />
</shape>

when not using an image as Android:src it works fine.

but when i use Android:src the borders are clipped as well.

note: I know one can use a FrameLayout with ImageView as a child and set the background drawable to the FrameLayout but that's just asking for issues.

EvilNeo
  • 313
  • 1
  • 3
  • 7
  • have you try use two image? first the image with the dish and second the image with round borders – Azhagthott Feb 26 '20 at 03:19
  • @FranciscoBarrios your suggesting is similar to the note i stated above, i even think it's cleaner to use `FrameLayout` rather than an `ImageView` to hold the round borders – EvilNeo Feb 26 '20 at 10:44

1 Answers1

0

Ok, I don't think this is a solution you expected but as there was no easy solution I could find for the question instead came up with a funny workaround if API level >= 23, but since it works thought I'll share it incase it helps someone.

Basically, set the same drawable used as background and set it as

android:foreground="@drawable/background"

you still need to set background in order for setClipToOutline to clip the drawable properly, and foreground will create an overlapping border on the outside.

Even though foreground makes use of FrameLayout but since its for the same ImageView didn't find any issues with animations, they seem to work fine with it.

ljk
  • 1,496
  • 1
  • 11
  • 14
  • Can confirm, this solution does work, haven't tested animations but it should be fine as well, still it has 2 caveats one is that it demands API >= 23 and the other is that if one is already using a foreground drawable then it has to be edited to have the same shape as the background drawable file – EvilNeo Feb 26 '20 at 11:11
  • @EvilNeo somehow I hadn't noticed your comment yet. Yes, as you mentioned API>=23 is a limitation but regarding the foreground it can be anything not necessary the same as background. But `setClipToOutline` clips based on the background frame, so setting a foreground with greater size will make it move outside the clipped imageview, but smaller ones are fine. And as borders for rounded corners was the requirement hence, the suggestion of having both same. – ljk Mar 05 '20 at 10:48