繁体   English   中英

带有圆角的ImageView通过另一个图像作为背景

[英]ImageView with rounded corners via another Image as a background

我有两个图像 - 1)矩形图像,显示实际内容和2)带圆角透明角的白色图像

是否可以将图像1放在图像2中,保持其大小,但使其形状与2相同?

基本上我希望Image 2成为图像1的容器。

我已经尝试了图层和插图drawables但是图像1一直重叠图像2。

提前致谢!

更新1:

这是我的ImageView xml部分:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:orientation="horizontal">

    <ImageView
        android:id="@+id/avatar"
        android:src="@drawable/mainImg"
        android:background="@drawable/backgroundImg"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_gravity="center"
        android:contentDescription="@string/desc" />

</LinearLayout>

更新2:

下面是三个图像的链接1)背景2)主图像3)预期结果(带圆角)

ImageShack上传

一个简单的解决方案是使用一个ImageViewandroid:background为你的image2,你说它是容器,而android:src用于image1,这是实际的图像:

<ImageView
    ...
    android:background="@drawable/image2"
    android:src="@drawable/image1"
    android:padding="2dp" />

只需添加一个填充,以指定要在“框架”和实际“图片”之间留出多少空白空间。

我不得不用我最近制作应用来处理这个问题。 请注意,在第一个和第二个屏幕截图中,缩略图是如何构图的。

为了实现这一点,我在FrameLayout中将图像和框架堆叠在一起。 首先,我布置实际图像(@ id / thumbnail),然后是框架(@ id / frame)。

需要注意的重要事项是缩略图使用的是scaleType“fitXY”,并且略有边距,因此角落不会在框架的圆角后面伸出。

这仅适用于框架边框不透明的情况,因此您可能必须使框架边缘与背景颜色相同。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/thumbnail_size"
    android:layout_height="@dimen/thumbnail_size"
    android:layout_margin="5dp"
    android:gravity="center"
    android:orientation="vertical"
     >

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="4dp"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/pixel_frame"
        android:scaleType="fitXY" />

</FrameLayout>

使用ImageButton ..将Background设置为Image1,将Image src设置为Image2

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/image2"
            android:src="@drawable/image1" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM