You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter启动页居中图片设为圆形?替换android:gravity="center"无效

Flutter启动页居中图片设置为圆形的解决方案

android:gravity="center"仅用于控制图片在容器内的对齐位置,无法实现圆形裁剪效果,所以替换该属性的值(比如fill、center-fill)解决不了问题。你需要通过以下两种方式实现圆形图片:

方式一:通过Android Drawable实现圆形裁剪(无需额外依赖)

  1. 创建圆形形状的Drawable文件(命名为circle_mask.xml,放在res/drawable目录下):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/transparent" />
</shape>
  1. 修改启动页的背景Drawable(通常是res/drawable/launch_background.xml),使用<clip>标签包裹图片并应用圆形遮罩:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 启动页背景色 -->
    <item android:drawable="@color/your_launch_bg_color" />
    
    <!-- 居中显示的圆形图片 -->
    <item>
        <clip
            android:shape="oval"
            android:gravity="center"
            android:drawable="@drawable/your_launch_image" />
    </item>
</layer-list>

方式二:使用CircleImageView控件(适用于布局文件式启动页)

如果你的启动页是通过布局文件(如res/layout/launch_screen.xml)实现的,可直接使用第三方圆形图片控件:

  1. 在app模块的build.gradle中添加依赖:
implementation 'de.hdodenhof:circleimageview:3.1.0'
  1. 在布局文件中替换普通ImageView为CircleImageView,并设置居中:
<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="180dp"
    android:layout_height="180dp"
    android:layout_gravity="center"
    android:src="@drawable/your_launch_image"
    android:scaleType="centerCrop" />

内容的提问来源于stack exchange,提问作者Farid Ahmadov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 01:38:11