如何获得android渐变中心灯光效果?

如何获得android渐变中心灯光效果?,android,android-drawable,xml-drawable,Android,Android Drawable,Xml Drawable,我想要下面的图片 我试着用可拉伸的形状 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="360" android:centerX="50%" android:centerY="

我想要下面的图片

我试着用可拉伸的形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
    android:angle="360"
    android:centerX="50%"
    android:centerY="50%"

    android:gradientRadius="50%"
    android:endColor="#000000"
    android:centerColor="#FFFFFF"
    android:startColor="#000000" >
</gradient>
</shape> 

在您的drawable文件夹中创建一个新的Android xml文件(比如GreyRadial.xml)

在xml文件中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:centerColor="#c1c1c1"
        android:endColor="#4f4f4f"
        android:gradientRadius="400"
        android:startColor="#c1c1c1"
        android:type="radial" >
    </gradient>

</shape>

可以使用多个矩形形状在图层列表中近似表示效果。使用具有中心背景色的实心矩形。然后使用两个开始和结束颜色相同的渐变。使中心颜色也相同,只是将alpha设置为零

以下代码中的颜色如下所示:

@color/background_dark = #ffb8860b
@color/background_light = #ffd2b48c
@color/transparent = #00b8860b

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape >
            <solid android:color="@color/background_light"/>
        </shape>
    </item>

    <item>
        <shape>
            <gradient 
                android:startColor="@color/background_dark"
                android:centerColor="@color/transparent"
                android:endColor="@color/background_dark"
                android:angle="45"/>
        </shape>
    </item>

    <item>
        <shape>
            <gradient 
                android:startColor="@color/background_dark"
                android:centerColor="@color/transparent"
                android:endColor="@color/background_dark"
                android:angle="135"/>
        </shape>
    </item>

</layer-list>
@color/background_dark=#ffb8860b
@颜色/背景光=#ffd2b48c
@颜色/透明=#00b8860b

这是我解决这个问题的方法

<shape android:shape="rectangle">

 <gradient
     android:endColor="@color/your_dark_color"
     android:startColor="@color/your_light_color"
     android:type="radial"
     android:gradientRadius="700"/>

通过更改android:gradientRadius的值,我可以得到准确的结果。

还有一个解决方案:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <gradient
        android:centerX="50%"
        android:centerY="50%"
        android:endColor="@android:color/black"
        android:gradientRadius="100%"
        android:startColor="@android:color/transparent"
        android:type="radial"/>
</shape>

如果您需要的椭圆形的纵横比是固定的,那么您可以使用渐变绘制作为背景,并使用scaleX或scaleY将其拉伸成椭圆形

drawable myradial.xml:

   <?xml version="1.0" encoding="utf-8"?>
   <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
       <!--Radial gradient centered at middle of top with glow color-->
       <gradient
           android:startColor="FF336699"
           android:endColor="00000000"
           android:centerX="50%"
           android:centerY="50%"
           android:gradientRadius="50%"
           android:type="radial"/>
   </shape>

将其用作背景视图

   <View
       android:layout_width="200dp"
       android:layout_height="100dp"
       android:background="@drawable/myradial"
       android:scaleX="0.5"
       />

如果纵横比不是固定的,那么仍然可以在运行时在代码中设置scaleX


这并非在所有情况下都适用于所有人。这会导致复杂的布局。一件好事是它是一个单一的渲染过程,相比之下,非常优雅的解决方案在一个实体上张贴了2个线性渐变的3个过程。它也可用于拉伸任何渐变,例如以22.5度角创建线性渐变。

图片中的渐变为椭圆形,但这是圆形。这应该是公认的答案。。。工作完美警告即使这是一个很好的解决方案,它也会崩溃(甚至在Android 5.0上重新启动整个手机!)API@computerjulian谢谢在API 16中,19个模拟器在使用“100%”时显示正确,但在API 21中当
android:gradientRadius=“100%”
时崩溃,例外情况是“java.lang.IllegalArgumentException:radius必须大于0”。因此,我用
100%p
替换
android:centerX
centerY
保持
50%
   <View
       android:layout_width="200dp"
       android:layout_height="100dp"
       android:background="@drawable/myradial"
       android:scaleX="0.5"
       />