在图像视图中裁剪图像

Cliping an image in 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>

但是,如果我添加边距而不是剪裁,图像会像下面那样被拉伸

是否可以使用 xml 且不使用自定义布局将我的六边形裁剪成两半?

[未测试,但指路明灯]

PercentRelativeLayout 可能会解决您的问题。 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>

你应该添加

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

使用 android:src 而不是 android:background 。 有关详细信息,请阅读 ClipDrawable

对于剪辑图像,您应该创建自定义剪辑可绘制对象。

<?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" />

致谢 ClipDrawable