[英]Layout Background partially transparent
I want to set a transparent background. 我想设置一个透明的背景。 Maybe 20% of gray but i also want to have a small part of the background being completely transparent (a rectangle or a circle in the layout): 也许20%的灰色,但我也想让背景的一小部分完全透明(布局中为矩形或圆形):
Is that possible? 那可能吗? later i want the rectangle (or the circle) to be moveable and scaleable. 后来我希望矩形(或圆形)是可移动和可缩放的。
This is easily achievable with a custom drawable like this: 使用这样的自定义可绘制对象很容易实现:
<?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>
When you overlay that drawable on top of an image the results would looks something like this: 当您将可绘制对象叠加在图像上方时,结果将如下所示:
By creating that drawable dynamically in code you can adjust it at runtime. 通过在代码中动态创建该可绘制对象,您可以在运行时对其进行调整。 For example: 例如:
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);
You can influence the shape of the hole in the middle by combining multiple layers in a LayerDrawable
. 您可以通过在LayerDrawable
中组合多个图层来影响中间孔的形状。
You can also influence the general shape of the hole by setting the android:shape
tag of the <shape />
element in the custom drawable. 您还可以通过在自定义可绘制对象中设置<shape />
元素的android:shape
标签来影响孔的一般形状。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.