【问题标题】:How to implement opposite gradient in android?如何在android中实现相反的渐变?
【发布时间】:2015-04-30 07:17:14
【问题描述】:

我正在使用这个

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <corners android:radius="7sp" />
        <gradient
            android:startColor="#333"
            android:centerColor="#ffffff"
            android:endColor="#333"
            android:angle="90" />

            <stroke android:width="1dp" android:color="#FFffffff" />
    </shape>
</item>

但这并没有达到预期的效果。

【问题讨论】:

  • 嘿,你在使用轮子库吗,看起来和那个很像
  • 是的,我只使用@apk
  • 是否必须成为GradientDrawable?用这个创建一个 9 补丁将是微不足道的。
  • @Payal 你想得到什么效果?你能用具体的模型来描绘它吗?
  • 我只使用 9 个补丁图像修复了它..谢谢大家

标签: android gradient xml-drawable


【解决方案1】:

你可以这样试试

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/gradient"
        android:orientation="vertical" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/gradient2"
        android:orientation="vertical" >
    </RelativeLayout>

</RelativeLayout>

渐变 1

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="7sp" />

            <gradient
                android:angle="90"
                android:endColor="#D1D1D1"
                android:startColor="#000" />

            <stroke
                android:width="1dp"
                android:color="#FFffffff" />
        </shape>
    </item>

</layer-list>

渐变 2

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="7sp" />

            <gradient
                android:angle="90"
                android:endColor="#000"
                android:startColor="#D1D1D1" />

            <stroke
                android:width="1dp"
                android:color="#FFffffff" />
        </shape>
    </item>

</layer-list>

最终结果

【讨论】:

    【解决方案2】:

    改用这个

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <gradient
                    android:startColor="#333"
                    android:centerColor="#DDD"
                    android:endColor="#333"
                    android:angle="90" />
    
                    <stroke android:width="1dp" android:color="#FF333333" />
            </shape>
        </item>
        <item android:left="4dp" android:right="4dp" android:top="1dp" android:bottom="1dp">
            <shape android:shape="rectangle">
                <gradient
                    android:startColor="#AAA"
                    android:centerColor="#FFF"
                    android:endColor="#AAA"
                    android:angle="90" />
            </shape>
        </item>
    </layer-list>
    

    但最好使用九块图像

    【讨论】:

      【解决方案3】:

      试试

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item>
          <shape>
              <gradient
                  android:startColor="#4C4C43"
                  android:centerColor="#B8B894"
                  android:endColor="#4C4C43"
                  android:angle="270" />
          </shape>
      </item>
      

      P.S - 根据需要改变颜色

      参考-http://www.learn-android-easily.com/2013/06/gradient-drawable-in-android.html

      【讨论】:

        【解决方案4】:

        使用:

        <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle" >
         <gradient 
           android:type="linear"
           android:centerX="50%" 
           android:startColor="#FFc0c0c0" 
           android:centerColor="#FFffffff" 
           android:endColor="#FF808080" 
           android:angle="270"/>
        </shape>
        
        <corners android:radius="7sp" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-19
          • 2011-08-20
          • 2022-11-24
          • 1970-01-01
          • 2017-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多