gpt4 book ai didi

android - 布局背景部分透明

转载 作者:行者123 更新时间:2023-11-30 02:19:45 25 4
gpt4 key购买 nike

我想设置透明背景。可能有 20% 的灰色但我也希望背景的一小部分完全透明(布局中的矩形或圆形):

enter image description here

这可能吗?稍后我希望矩形(或圆形)可以移动和缩放。

最佳答案

这很容易通过这样的自定义可绘制对象实现:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="rectangle"
android:thicknessRatio="1.9"
android:useLevel="false">

<solid android:color="@android:color/transparent"/>

<stroke
android:width="125dp"
android:color="#CC000000"/>
</shape>

当您将该可绘制对象叠加在图像之上时,结果将如下所示:

enter image description here

通过在代码中动态创建可绘制对象,您可以在运行时对其进行调整。例如:

ShapeDrawable drawable = new ShapeDrawable(new RectShape());
drawable.getPaint().setColor(0xCC000000);
drawable.getPaint().setStyle(Paint.Style.STROKE);
drawable.getPaint().setStrokeWidth(dpToPixel(125.0f));
someView.setBackground(drawable);

您可以通过在 LayerDrawable 中组合多层来影响中间孔的形状.

您还可以通过设置 android:shape 来影响孔的总体形状<shape /> 的标签自定义绘图中的元素。

关于android - 布局背景部分透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com