在图层列表中剪裁平铺位图的角落

5
我希望实现类似于这里底部的东西(对于所有四个角,但我只有图像的底部可以展示给你):

enter image description here

我目前所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/orange_tile"
            android:tileMode="repeat"
            android:dither="true"
            android:gravity="center" />
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <stroke android:color="@color/white" android:width="0.5dp" />
        </shape>
    </item>
</layer-list>

然而,根据预览,这会在每个角落给我一个微小的像素:

enter image description here

那么,我如何剪裁这些角落呢?可见的描边不是想要的也不应该存在...

尝试过的方法:

将实体添加到项目中会得到完美的相反效果。可能有一种遮罩技巧可行...

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/orange_tile"
            android:tileMode="repeat"
            android:dither="true"
            android:gravity="center" />
    </item>
    <item>
        <shape android:shape="rectangle">
            <color android:color="@color/white" />
            <corners android:radius="2dp"/>
            <stroke android:color="@color/white" android:width="0.5dp" />
        </shape>
    </item>
</layer-list>

enter image description here


标题:

A simple tile


你想要剪裁/移除橙色图像背景吗?对吧? - Jitesh Mohite
你能提供“orange_tile”绘制资源吗? - Jitesh Mohite
@jiteshmohite 我想要移除背景瓦片的角落。 - MPelletier
@jiteshmohite 添加了标题。 - MPelletier
1个回答

2
我可以想到几个选项。
  1. Just add one more background rectangle to hide the corners:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>
        <bitmap android:src="@drawable/orange_tile"
            android:tileMode="repeat"
            android:dither="true"
            android:gravity="center" />
    </item>
    
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <stroke android:color="@color/white" android:width="0.5dp"/>
        </shape>
    </item>
    
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="0dp"/>
            <stroke android:color="@color/white" android:width="0.5dp"/>
        </shape>
    </item>
    
    </layer-list>
    
  2. You can move your corners rectangle out of the view and make it bigger:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item>
        <bitmap android:src="@drawable/icon_bg"
            android:tileMode="repeat"
            android:dither="true"
            android:gravity="center" />
    </item>
    
    <item android:top="-1.5dp" android:bottom="-1.5dp"
        android:left="-1.5dp" android:right="-1.5dp"
        >
        <shape android:shape="rectangle">
            <corners android:radius="2dp"/>
            <stroke android:color="@color/white" android:width="1.5dp"/>
        </shape>
    </item>
    
    </layer-list>
    

对于这种情况,您需要微调偏移量。这有点麻烦,因为它会缩小平铺内容。在微调时,您可能需要使用 android:scaleX="float"android:scaleY="float" 缩放图像。

  1. If you are open to code solution for getting rounded corners you can also try the RoundedBitmapDrawable class.

    float cornerRadius = 2f * Resources.getSystem().getDisplayMetrics().density;    
    
    Bitmap tileBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.orange_tile);
    RoundedBitmapDrawable roundedTileDrawable = 
        RoundedBitmapDrawableFactory.create(getResources(), tileBitmap);
    
    roundedTileDrawable.setCornerRadius(cornerRadius);
    

选项3难道不会将每个瓷砖都四舍五入吗? - MPelletier
选项1和2有其他副作用,这些副作用会破坏我的(未发布的)要求。你不必为此承担责任,这完全是我的责任。 - MPelletier
我认为你可以在第三种方法中使用基于xml drawable的位图,并设置android:tileMode="repeat"。 - TpoM6oH
非常抱歉我处理这件事情的方式不当。我们正在朝着项目结束的方向奋力前行,我无法完全验证这个问题,我们也改变了布局来规避这个问题。我仍然觉得你应该因为你的时间和解决方案而受到奖励,但我还没有尝试最终的方法。谢谢。 - MPelletier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接