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

如何在Android的layer-list中为平铺Bitmap裁剪四角?

给平铺Bitmap添加圆角裁剪的解决方案

要实现平铺Bitmap的四个圆角裁剪,你不需要复杂的操作,只需要把你的平铺Bitmap嵌套在一个带圆角的shape里就行——这样shape的圆角会自动裁剪内部的Bitmap内容。下面是完整的修改后的代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <!-- 这里设置四个角的圆角半径,根据你的需求调整数值,比如16dp -->
            <corners android:radius="16dp" />
            
            <!-- 将你的平铺Bitmap作为shape的背景 -->
            <android:drawable>
                <bitmap
                    android:src="@drawable/orange_tile"
                    android:tileMode="repeat"
                    android:dither="true"
                    android:gravity="center" />
            </android:drawable>
        </shape>
    </item>
</layer-list>

额外说明:

  • 如果之后需要给这个圆角区域添加边框,可以在layer-list里再添加一个item,设置合适的padding(和边框宽度一致),然后添加一个带圆角的stroke shape,比如:
    <item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
        <shape android:shape="rectangle">
            <corners android:radius="16dp" />
            <stroke android:width="1dp" android:color="#FF9800" /> <!-- 边框颜色 -->
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    
  • 注意corners标签里的radius是统一设置四个角的圆角,如果需要单独设置某个角的圆角,可以用android:topLeftRadius、android:topRightRadius等属性单独调整。

这样修改后,你的平铺Bitmap就会被裁剪成四个圆角的效果啦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:41:18