我正在try 将Make an ImageView的角用纯XML舍入到一个Home Screen窗口小部件中.我在v31和更高版本中使用android:clipToOutline="true",但由于我的应用程序支持更低的版本,我不能一直使用它.这是我的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:layout_margin="9dp"
    android:background="@drawable/widget_background"
    android:padding="9dp"
    android:id="@+id/widget_container"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/default_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="25sp"
        android:textStyle="bold"
        android:gravity="center"
        android:textColor="#E9E9E9"
        tools:text="Nothing yet!" />

    <ImageView
        android:id="@+id/scribb_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:background="@drawable/image_background"
        android:contentDescription="@string/image" />
</LinearLayout>

image_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="9dp"/>
</shape>

这是一个Flutter 项目,所以我尽量不使用太多的依赖项.

推荐答案

注意:如果你找不到更好的解决方案,那么就使用这种方法.

创建一个具有所需的拐角半径和透明背景的可绘制框架.框架的 colored颜色 应该与根父级的背景相匹配.然后将此帧作为覆盖应用到您的图像视图.

由于框架背景是透明的,您将能够看到底部图像,但由于框架边框 colored颜色 与您的屏幕 colored颜色 匹配,因此它隐藏了使图像显示为弯曲边缘的部分的图像.

可绘制代码:我在图像编辑器中创建了所需的图像,然后将其转换为 使用这个site的矢量.

<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:aapt="http://schemas.android.com/aapt"
    android:viewportWidth="474"
    android:viewportHeight="469"
    android:width="200dp"
    android:height="200dp">
    <path
        android:pathData="M0 0L474 0L474 469L0 469L0 0ZM66 3Q64 6 58 4L45 8Q33 13 25 22Q9 36 5 61L5 410L8 424L19 443Q33 462 61 467L414 467Q432 464 444 454Q455 444 463 431L467 421L469 410L469 61L464 43Q459 32 451 24Q442 14 430 8L417 4Q410 6 409 3L66 3Z"
        android:fillColor="#000000"
        android:strokeColor="#000000"
        android:strokeWidth="1" />
</vector>

XML代码:您可以使用backgroundTint属性来更改可绘制的 colored颜色 .您可能还希望使用RelativeLayout或ConstraintLayout来正确对齐图像视图顶部的叠加视图.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_color">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:src="@drawable/ic_launcher_background" />

    <View
        android:id="@+id/circular_overlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/image_view"
        android:layout_alignTop="@id/image_view"
        android:layout_alignEnd="@id/image_view"
        android:layout_alignBottom="@id/image_view"
        android:background="@drawable/bg"
        android:backgroundTint="@color/background_color" />

    <ImageView
        android:id="@+id/image_view_without_overlay"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/image_view"
        android:src="@drawable/ic_launcher_background" />
</RelativeLayout>

可绘制的:

Drawable Output

示例输出:

Sample Output

Drawbacks:上面的方法有一些缺点,比如不能轻松地自定义曲线的半径.而且,如果您经常想要这样做,每个图像视图的查看次数将增加一倍,从而使布局变得复杂.

Better Approach:更好的方法是创建一个自定义视图,与上面的 idea 相同,扩展基本ImageView类,并使用油漆和画布在其外部绘制一个边框.使用自定义视图,您可以在以后轻松地自定义它,包括半径和 colored颜色 .此外,您的布局将变得更简单,因为您可以使用您的自定义视图而不是ImageView.您可以阅读有关自定义视图here的内容.

Android相关问答推荐

AdMob:MobileAds. initialize()—java. lang. xml对于某些设备不能强制转换为java. lang. String""

如何解决Android Studio中的in fragment问题

组成底部导航栏,自定义形状,周围透明

无法在Jetpack Compose中显示Admob原生广告

Android和Rust,OpenSSL交叉编译在ARM V7上链接失败

BroadCastReceiver的onReceive方法中的Intent上的Extras为空

Android 12+BLE字节不同

activity在 Runnable 中如何工作?我的 Android 表格未显示

retrofit2.HttpException: HTTP 401

SQLite Kotlin 问题 - 没有数据库

修复报错 RecyclerView: No adapter attached;跳过布局

如何使用滑行加载媒体的专辑封面?

在 Jetpack Compose 中对齐行项目

如何在 Compose 中使用合并的单元格创建网格视图?

Material Design 网站的新设计已启动,但我找不到实现选项卡,该选项卡在哪里?

协程中的 Job.Cancel 与 Scope.Cancel 有什么区别?

如何授予对 Uri 图像的永久权限(androidx)

Android Studio:如何添加应用程序质量洞察窗口以查看 Android Studio 中的 Crashlytics 数据?

如何在 Android Studio 中使用 Github Copilot?

Сan 无法从 Firestore 获取数据,没有错误