android中的圆形渐变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2470072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Circular gradient in android
提问by pgsandstrom
I'm trying to make a gradient that emits from the middle of the screen in white, and turns to black as it moves toward the edges of the screen.
我正在尝试制作从屏幕中间发出的白色渐变,并在向屏幕边缘移动时变成黑色。
As I make a "normal" gradient like this, I have been experimenting with different shapes:
当我制作这样的“正常”渐变时,我一直在尝试不同的形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#E9E9E9" android:endColor="#D4D4D4"
android:angle="270"/>
</shape>
When using the "oval"-shape I at least got a round shape, but there were no gradient effect. How can I achieve this?'
当使用“椭圆”形状时,我至少得到了一个圆形,但没有渐变效果。我怎样才能做到这一点?
回答by Dan Lew
You can get a circular gradient using android:type="radial"
:
您可以使用android:type="radial"
以下方法获得圆形渐变:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial" android:gradientRadius="250dp"
android:startColor="#E9E9E9" android:endColor="#D4D4D4" />
</shape>
回答by Suragch
I always find images helpful when learning a new concept, so this is a supplemental answer.
在学习新概念时,我总是发现图像很有帮助,所以这是一个补充答案。
The %p
means a percentage of the parent, that is, a percentage of the narrowest dimension of whatever view we set our drawable on. The images above were generated by changing the gradientRadius
in this code
这%p
意味着父级的百分比,即我们设置 drawable 的任何视图的最窄维度的百分比。上面的图像是通过更改gradientRadius
此代码中的
my_gradient_drawable
my_gradient_drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="radial"
android:gradientRadius="10%p"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
Which can be set on a view's background
attribute like this
可以background
像这样在视图的属性上设置
<View
android:layout_width="200dp"
android:layout_height="100dp"
android:background="@drawable/my_gradient_drawable"/>
Center
中心
You can change the center of the radius with
您可以更改半径的中心
android:centerX="0.2"
android:centerY="0.7"
where the decimals are fractions of the width and height for x
and y
respectively.
其中小数是宽度和高度的级分x
和y
分别。
Documentation
文档
Here are some notes from the documentationexplaining things a little more.
以下是文档中的一些注释,解释了更多内容。
android:gradientRadius
Radius of the gradient, used only with radial gradient. May be an explicit dimension or a fractional value relative to the shape's minimum dimension.
May be a floating point value, such as "1.2".
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).
May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.
android:gradientRadius
渐变的半径,仅用于径向渐变。可以是显式尺寸或相对于形状最小尺寸的分数值。
可能是一个浮点值,例如“1.2”。
可能是一个维度值,它是一个浮点数,附加一个单位,例如“14.5sp”。可用单位有:px(像素)、dp(与密度无关的像素)、sp(基于首选字体大小的缩放像素)、in(英寸)和 mm(毫米)。
可能是一个小数值,它是一个附加有 % 或 %p 的浮点数,例如“14.5%”。% 后缀始终表示基本大小的百分比;可选的 %p 后缀提供了相对于某个父容器的大小。
回答by Gasper Kolenc
You can also do it in code if you need more control, for example multiple colors and positioning. Here is my Kotlin snippet to create a drawable radial gradient:
如果您需要更多控制,例如多种颜色和定位,您也可以在代码中进行。这是我创建可绘制径向渐变的 Kotlin 片段:
object ShaderUtils {
private class RadialShaderFactory(private val colors: IntArray, val positionX: Float,
val positionY: Float, val size: Float): ShapeDrawable.ShaderFactory() {
override fun resize(width: Int, height: Int): Shader {
return RadialGradient(
width * positionX,
height * positionY,
minOf(width, height) * size,
colors,
null,
Shader.TileMode.CLAMP)
}
}
fun radialGradientBackground(vararg colors: Int, positionX: Float = 0.5f, positionY: Float = 0.5f,
size: Float = 1.0f): PaintDrawable {
val radialGradientBackground = PaintDrawable()
radialGradientBackground.shape = RectShape()
radialGradientBackground.shaderFactory = RadialShaderFactory(colors, positionX, positionY, size)
return radialGradientBackground
}
}
Basic usage (but feel free to adjust with additional params):
基本用法(但可以随意调整其他参数):
view.background = ShaderUtils.radialGradientBackground(Color.TRANSPARENT, BLACK)
回答by Shihab Uddin
Here is the complete xml with gradient, stoke & circular shape.
这是带有渐变、斯托克和圆形的完整 xml。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<!-- You can use gradient with below attributes-->
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<!-- You can omit below tag if you don't need stroke -->
<stroke android:color="#3b91d7" android:width="5dp"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="200dp" android:height="200dp"/>
<!--if you need only a single color filled shape-->
<solid android:color="#e42828"/>
</shape>
回答by dhesse
I guess you should add android:centerColor
我想你应该添加 android:centerColor
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:centerColor="#000000"
android:endColor="#FFFFFF"
android:angle="0" />
</shape>
This example displays a horizontal gradient from white to black to white.
此示例显示从白色到黑色再到白色的水平渐变。
回答by Nazmul
<gradient
android:centerColor="#c1c1c1"
android:endColor="#4f4f4f"
android:gradientRadius="400"
android:startColor="#c1c1c1"
android:type="radial" >
</gradient>
回答by CHirag RAmi
<!-- Drop Shadow Stack -->
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#00CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#10CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#20CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#30CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#50CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape android:shape="oval">
<gradient
android:startColor="@color/colorAccent_1"
android:centerColor="@color/colorAccent_2"
android:endColor="@color/colorAccent_3"
android:angle="45"
/>
<corners android:radius="3dp" />
</shape>
</item>
<color name="colorAccent_1">#6f64d6</color>
<color name="colorAccent_2">#7668F8</color>
<color name="colorAccent_3">#6F63FF</color>