【问题标题】:How to center the gradient color in android如何在android中使渐变颜色居中
【发布时间】:2017-12-18 14:38:59
【问题描述】:

您好,我正在尝试在 android 中进行渐变。 我正在尝试以下代码

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#EF5350"
        android:centerColor="#FFEBEE"
        android:endColor="#EF5350"
        android:angle="-270" />
</shape>

结果:

我想增加中心白色的大小。请帮我解决我的问题。

【问题讨论】:

标签: android gradient


【解决方案1】:

试试这个:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="50dp">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#EF5350"
                android:endColor="#EF5350"
                android:centerColor="#FFFFFF"
                android:angle="270"/>
            <size
                android:height="100dp"
                android:width="80dp"/>
        </shape>
    </item>
    <item
        android:bottom="50dp">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#EF5350"
                android:endColor="#FFFFFF"
                android:centerColor="#FFFFFF"
                android:angle="270"
                />
            <size
                android:height="100dp"
                android:width="80dp"/>
        </shape>
    </item>
</layer-list>

【讨论】:

  • 是的..这就是我想要的..!!谢谢@Vishva ..!!
  • 别忘了点赞并标记为正确。 :) 乐于助人 :)
【解决方案2】:

你需要在渐变标签内指定type

例如在我的例子中,我为中心渐变效果定义了 radial

android:type="radial"

我已经创建了具有中心渐变效果的渐变形状(centergradietbg.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>

在你的布局背景中使用这个xml

android:background="@drawable/centergradietbg"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2014-10-29
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多