Android渐变中出现的透明阴影白线

10

我需要在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>
1个回答

1

实际上,没有线条

这是一种光学效应,因为渐变是线性的。颜色从黑色平滑地过渡到透明,但在最后,颜色变得透明的速度会急剧改变。您可以在此处阅读更多信息。

Gradients example

要解决这个问题,您需要使渐变缓慢消失。由于Android不支持不同曲率的渐变,因此有几种解决方案:

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接