Android中如何在父RelativeLayout中裁剪ImageView?

5
我想知道如何裁剪一个具有缩放背景图像的ImageView,使用其父元素上固定的宽度和高度。
基本上,我希望使用ImageView android:background对图像进行缩放,然后裁剪超出父元素边界的部分图像。
到目前为止,我有以下代码:
<RelativeLayout android:id="@+id/time_foregrd"
    android:layout_width="57px"
    android:layout_height="100px"
    android:layout_marginLeft="100px"
    android:layout_marginTop="285px"
    android:clipChildren="true"
   >
    <ImageView  android:layout_width="57px"
                android:layout_height="338px"
                android:minWidth="57px"
                android:minHeight="338px"
                android:background="@drawable/time_foreground"
                />
</RelativeLayout>

但是它不起作用...我做错了什么?
3个回答

4

好的,我知道如何做了。我使用了FrameLayout而不是RelativeLayout,这样就完美解决了。

以下是代码:

<FrameLayout android:id="@+id/time_foregrd"
    android:layout_width="57px"
    android:layout_height="100px"
    android:layout_marginLeft="100px"
    android:layout_marginTop="285px"
   >
    <ImageView  android:layout_width="57px"
                android:layout_height="338px"
                android:layout_gravity="bottom"
                android:scaleType="fitXY"
                android:src="@drawable/time_foreground"
                />
</FrameLayout>

当使用RelativeLayout时,它不起作用吗?因为我需要在此容器中放置多个子元素。 - richardaum

0

1- 使用RelativeLayout将该元素包装起来。

2- 将以下代码添加到您的元素中:android:layout_gravity="center_vertical"

它将像ImageView的scaleType="centerCrop"一样运作。

   <LinearLayout
        android:id="@+id/ll_wrapper"
        android:layout_width="match_parent"
        android:layout_height="250.0dp" >
        <TextureView
            android:layout_width="match_parent"
            android:layout_height="451.0dp"
            android:id="@+id/tv_YourElement"
            android:layout_gravity="center_vertical" /> 

    </LinearLayout>

android:layout_gravity= "center_vertical", "center_horizontal", "center",...

(注:这是一段包含Android布局属性的代码片段,其中包括垂直居中、水平居中和居中对齐等属性)

0

我在我的RelativeLayout中能够做同样的事情

android:scaleType="centerCrop"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接