【问题标题】:Set gradient on stroke android在描边android上设置渐变
【发布时间】:2017-08-07 09:45:04
【问题描述】:

我有一个圆形可绘制对象,我在其上设置了一个 8dp 白色描边,如下所示:

    circleImage = (ImageView) rootView.findViewById(R.id.image);
    circleImage.setOnClickListener(clickListener);
    drawable = (GradientDrawable) circleImage.getBackground();
    drawable.setStroke(8, getResources().getColor(R.color.colorWhite));

circleImage 的 XML 如下所示:

 <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:id="@+id/image"
            android:background="@drawable/roundcorner"
            android:clickable="true"/>

我现在要做的是更改drawable.setStroke 以包含这样的渐变色

我想实现这一点最简单的方法是在可绘制的 XML 中编写一些东西,但我不太知道如何实现这一点。

圆角.xml

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle" >
        <corners
            android:topLeftRadius="100dp"
            android:topRightRadius="100dp"
            android:bottomLeftRadius="100dp"
            android:bottomRightRadius="100dp"
            />
        <padding
            android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp"
            />
        <size
            android:width="100dp"
            android:height="100dp"
            />
    </shape>

【问题讨论】:

    标签: java android gradient shape


    【解决方案1】:

    你应该这样做。使用layer-list 2 个形状。第一个用于渐变描边,第二个用于实心。

    <?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
            <shape android:shape="oval" >
                <gradient
                    android:angle="360"
                    android:startColor="#543456"
                    android:endColor="#ff00b5"
                    android:type="linear" />
                <size android:width="24dp"
                      android:height="24dp"/>
            </shape>
        </item>
    
        <item
            android:bottom="2dp"
            android:left="2dp"
            android:right="2dp"
            android:top="2dp">
            <shape android:shape="oval" >
                <solid android:color="#fff" />
            </shape>
        </item>
    
    </layer-list>
    

    这段代码看起来像这样

    【讨论】:

      【解决方案2】:

      如果你需要内部颜色是透明的,那么你可以使用环形:

      <?xml version="1.0" encoding="utf-8"?>
      <shape
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="ring"
          android:thickness="2dp"
          android:useLevel="false">
          <gradient
              android:startColor="@color/sea_green"
              android:endColor="@color/black"
              android:angle="270" />
      </shape>
      

      【讨论】:

      • 如何用矩形做出同样的效果?
      • @I.Step,也许尝试使用android:shape="rectangle"
      • 是的,但是对于矩形,您不会得到一个边框,您会得到一个矩形,如果您尝试使用两个矩形制作边框,其中一个与另一个几乎没有填充。它不适用于内部矩形透明背景。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      相关资源
      最近更新 更多