透明渐变问题

5

我有一个包含Google地图碎片的FrameLayout布局。此外,我还在地图上放置了一个带有渐变背景的新视图。

布局:
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    >

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <View
        android:id="@+id/fadeTop"
        android:layout_width="match_parent"
        android:layout_height="@dimen/fade_height"
        android:layout_gravity="top"
        android:background="@drawable/list_fade_top"/>
    <include layout="@layout/top_line_mid_gray"/>

</FrameLayout>

list_fade_top:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:angle="90"
              android:startColor="@color/fade_start_color"
              android:centerColor="@color/fade_center_color"
              android:endColor="@color/fade_end_color"
        android:type="linear"/>
</shape>
  1. 起始/结束和中心颜色。这个渐变使用两条亮线绘制:

<color name="fade_start_color">#00000000</color> <color name="fade_center_color">#4f000000</color> <color name="fade_end_color">#ff000000</color> enter image description here

  1. 起始/结束和中心颜色。这个渐变使用一条亮线绘制:

<color name="fade_start_color">#00000000</color> <color name="fade_center_color">#6f000000</color> <color name="fade_end_color">#ff000000</color> enter image description here

  1. The same behavior for gradient without center color:

    <gradient
        android:angle="90"
              android:startColor="@color/fade_start_color"
              android:endColor="@color/fade_end_color"
        android:type="linear"/>
    

enter image description here

我该如何绘制一个从黑色(或半透明黑色)到完全透明的平滑线性渐变?

我尝试了硬件加速的开启和关闭,'getWindow().setFormat(PixelFormat.RGBA_8888);',但都没有帮助。

更新1: 我在Github上创建了一个简单的应用程序。 https://github.com/ralexey/TestApp 只是一个包含颜色背景和渐变效果的活动。

2个回答

1
在你的渐变中添加这行代码:android:centerY="y%p",可能会对你有所帮助。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:centerY="25%p"
        android:centerColor="@color/fade_center_color"
        android:endColor="@color/fade_end_color"
        android:startColor="@color/fade_start_color"
        android:type="linear"
        android:dither="true"
        android:useLevel="true" />

</shape>

谢谢,但并没有太大帮助。 - Alexey Rogovoy
请添加此行代码 android:dither="true" - prakash ubhadiya
结果是相同的 https://github.com/ralexey/TestApp/blob/prakash_u/app/src/main/res/drawable/list_fade_top.xml - Alexey Rogovoy
android:centerY="50%p" 根据您的需求进行调整。 - prakash ubhadiya
我设置了50%,只是为了显示渐变上的两条亮线。我可以删除中心颜色和50%的所有内容,但这将在渐变底部绘制白线,而我想获得一个没有这条线的平滑渐变。 - Alexey Rogovoy

0

尝试使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" android:padding="10dp">
            <gradient
                android:startColor="#00000000"
                android:centerColor="#6f000000"
                android:endColor="#ff000000"
                android:angle="90" />

        </shape>
    </item>

</layer-list>

谢谢您的回答,但是它并没有帮助到我。 - Alexey Rogovoy

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