Android渐变末端透明阴影白线

Android transparent shadow white line at the end of gradient

我需要有关 android 可绘制线性渐变的帮助,使用自定义渐变作为工具栏中的阴影,但我无法在渐变末尾设置白线。

XML 布局,最后一个 RelativeLayout 子元素 View 正在投影。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:titleTextColor="@android:color/white"
        android:weightSum="1">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabIndicatorColor="@color/tabIndicator"
            app:tabIndicatorHeight="3dp"
            app:tabMode="fixed"
            app:tabPaddingEnd="12dp"
            app:tabTextAppearance="@style/customTabLayout"
            android:layout_weight="0.90"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </android.support.design.widget.TabLayout>


    </android.support.v7.widget.Toolbar>

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager_main"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/toolbar" />

<View
    android:layout_width="match_parent"
    android:layout_height="14dp"
    android:background="@drawable/drop_shadow"
    android:layout_below="@+id/toolbar"/>
</RelativeLayout>

可绘制渐变

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:type="linear"
    android:angle="270"
    android:endColor="#00000000"
    android:startColor="#5a000000" />
</shape>

其实并没有线

这是一种光学效果,因为渐变是线性的。颜色从黑色平滑地过渡到透明,但颜色变为透明的“速度”在最后急剧变化。你可以阅读更多here

要解决此问题,您需要使渐变缓出。由于Android不支持渐变的不同曲率,所以有以下几种解决方法:

  1. 九补丁(推荐)
    在矢量图形编辑器中创建所需的渐变,并基于它创建 Nine-patch drawable。阅读有关 9 补丁的更多信息:https://developer.android.com/guide/topics/graphics/drawables#nine-patch

  2. 多重线性渐变
    创建一组具有选定颜色值的线性渐变,以便近似中的曲率对应于缓出曲率