1

I'm trying to add a hexagonal custom frame to an ImageView.

Approach I tried :

Created a hexagon shape in drawable folder.

drawable/ic_hexagon.xml file

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:thicknessRatio="1.9"
android:useLevel="false">
<solid android:color="@android:color/transparent"/>
<path android:fillColor="#00ffffff"
    android:pathData="m723,314c-60,103.9 -120,207.8 -180,311.8 -120,0 -240,0 -360,0C123,521.8 63,417.9 3,314 63,210.1 123,106.2 183,2.2c120,0 240,0 360,0C603,106.2 663,210.1 723,314Z"
    android:strokeColor="#000000" android:strokeWidth="10"/>
</shape>

Then created filter.xml file using layer-list and added it as the background of the ImageView.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img"/>
    <item android:drawable="@drawable/ic_hexagon"/>
</layer-list>

layout.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/filter"/>
</LinearLayout>

I followed this answer. But I wasn't able to apply this answer for custom hexagon. This is a sketch of the layout I'm trying to achieve.

Image

PS : I'm trying to use the hexagon as a frame. The hexagon shape is working. But it cannot be used as a frame.

Please help me with this. Thanks.

Choxmi
  • 1,584
  • 4
  • 29
  • 47
  • 3
    Possible duplicate of [How to create Hexagon shape in .xml format](https://stackoverflow.com/questions/37296863/how-to-create-hexagon-shape-in-xml-format) –  Oct 29 '17 at 03:29
  • @Ibrahim I followed this question to draw the shape. Shape is working fine as I mentioned. I have the issue with using that hexagon as a frame. – Choxmi Oct 29 '17 at 03:35
  • Yes I tried that as . But it doesn't work. :( – Choxmi Oct 29 '17 at 03:38
  • You have to create custom `imageView` class like [CircleImageView](https://github.com/hdodenhof/CircleImageView/blob/master/circleimageview/src/main/java/de/hdodenhof/circleimageview/CircleImageView.java#L45) –  Oct 29 '17 at 03:54
  • Thanks @Ibrahim . That'll be really helpful. Still searching whether there is any easier solution though. :) – Choxmi Oct 29 '17 at 04:00
  • Maybe after your last edit the question become not duplicate yet, but you want to make it more clear. –  Oct 29 '17 at 04:01
  • Yes I think so. – Choxmi Oct 29 '17 at 04:02
  • `` is not a valid tag in a ``, so it's ignored. If you want something like is shown in your image, then I don't think you're going to be able to do this through just XML resources, 'cause the source image needs to be clipped, not just drawn over with a solid color. – Mike M. Oct 29 '17 at 04:10
  • Thanks @MikeM. BTW `` is working fine. I used this shape as the `src` of an imageview. That worked fine. – Choxmi Oct 29 '17 at 04:17
  • 1
    Really? They must've changed something, then, since last I worked with them. You might wanna test thoroughly, though, as I'm pretty sure it's not going to work on older Android versions. – Mike M. Oct 29 '17 at 04:26
  • 1
    as Mike said: it is not possible to do that in xml only - see `android.support.v4.graphics.drawable.RoundedBitmapDrawable` sources on how to do that in java – pskink Oct 29 '17 at 06:52

0 Answers0