使用 kotlin 的带圆角的 Imageview

Vig*_*shK 2 android rounded-corners android-layout

我已经使用 Imageview 创建了布局,并且设置了带角半径的自定义背景,父布局是有角的,但 Imageview 不是圆角的。我怎样才能实现这个目标?

提前致谢。

背景文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid
        android:color="@color/list_item_bg"/>

    <stroke
        android:width="2dp"
        android:color="@color/colorAccent" />
    <corners
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"/>

</shape>
Run Code Online (Sandbox Code Playgroud)

列表项.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:background="@drawable/list_item_bg"
    >

    <ImageView
        android:id="@+id/food_image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:scaleType="centerCrop"
        android:clipToOutline="true"
        android:background="@drawable/image_bg"
        android:src="@drawable/sample_image"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ane*_*ees 5

创建 res/drawable/round_outline.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

将可绘制对象设置为 ImageView 的背景

android:background="@drawable/round_outline"
Run Code Online (Sandbox Code Playgroud)

在 ImageView 上(布局)

android:clipToOutline="true"
Run Code Online (Sandbox Code Playgroud)