-2

In Android, it's possible to create a Drawable that has rounded corners, which can then be used as the background of other controls in order to give them the appearance of having rounded corners.

However, I want my corners to be angular, like this:

enter image description here

And most importantly, I want the clipped corner to remain the same size regardless of the size of the object it's applied to (just like the rounded corners would), which means that I can't just use a vector drawable, which would stretch the shape if the control is not the same size as the drawable.

How would I accomplish the effect I'm going for (preferably a solution that doesn't involve any Java)?

laptou
  • 6,389
  • 2
  • 28
  • 59

1 Answers1

0

I eventually achieved the effect I was going for using a layer list drawable:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="left" android:right="14.5dp" android:top="14.5dp">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <stroke
                android:width="1dp"
                android:color="#000000"
                android:dashWidth="2dp"
                android:dashGap="1.4dp" />
        </shape>
    </item>
    <item android:gravity="right" android:bottom="14.5dp">
        <rotate
            android:fromDegrees="-45"
            android:toDegrees="0"
            android:pivotX="100%"
            android:pivotY="0%">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <stroke
                android:width="1dp"
                android:color="#000000"
                android:dashWidth="2dp"
                android:dashGap="1.4dp" />
            <size android:width="22.3dp" android:height="22.3dp" />
        </shape>
        </rotate>
    </item>
</layer-list>

In order to display this drawable, I then used an ImageView:

<ImageView
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:scaleType="fitXY"
            android:layerType="software"
            android:src="@drawable/fg_dotted_line" />
laptou
  • 6,389
  • 2
  • 28
  • 59
  • Yes, it doesn't clip the view behind it, but it works for my purposes. And no, it does not work as a background - I edited my answer to show how I am using it @pskink – laptou Jun 09 '18 at 19:03
  • I am testing this on a phone running Android 8.1, so I am not actually sure whether it would work on 6.0; however, I have no reason yet to believe that it wouldn't. – laptou Jun 09 '18 at 19:04
  • @pskink It just seems like overkill to write an entire Java class to do something that should ideally already be able to do, and it would be a pain if I couldn't use this drawable as a resource -- which I would be unable to do if I defined it in Java. – laptou Jun 09 '18 at 19:15