2

I have an imageview an i need to clip image half in that imageview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="true"
                android:clipChildren="true">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="200dp"
        android:background="@drawable/hexagon_1"/>


</RelativeLayout>

This is the image without any margin

However if i add margin instead of clipping image got streched like below

enter image description here

is there anyway to clip my hexagon into half using xml and without using custom layout?

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
Nikhil
  • 650
  • 1
  • 11
  • 28

2 Answers2

3

You should add

    android:scaleType="fitXY"
    android:adjustViewBounds="true" 
    android:src="@drawable/hexagon_1"

Use android:src instead of android:background . For more information read ClipDrawable.

For clip image, You should create custom clip drawable.

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/your_drawable"
    android:gravity="top" />

Courtesy goes to ClipDrawable.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

[Not tested, but a guiding light]

PercentRelativeLayout might solve your problem. Read more here

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>
vishwarajanand
  • 1,021
  • 13
  • 23