如何在 Android Studio 中制作淡化黑色背景

How to make a fade black background in Android Studio

我想用渐变黑色覆盖我的图像,就像这个一样,有文字、渐变背景,然后是我的图像。我只想知道如何实现这种淡入淡出。谢谢!

创建一个可绘制文件,我将其命名为gradient_background(你可以任意命名)

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient 
       android:angle="90" 
       android:startColor="#000000"
       android:centerColor="#00000000" 
       android:endColor="#00000000"/>

</shape>

然后在你的 target xml 中,创建一个像这样的 View,看看里面,我们是将 background 设置为可绘制文件。在这种情况下是 gradient_background

<View
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/gradient_background"
    app:layout_constraintBottom_toBottomOf="post_image"
    app:layout_constraintEnd_toEndOf="@+id/post_image"
    app:layout_constraintStart_toStartOf="post_image"
    app:layout_constraintTop_toTopOf="@+id/post_image" />

所以它应该是这样的:

:)

首先,制作bottom_transparent_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape>
        <gradient android:angle="270"
            android:endColor="#64000000"
            android:startColor="@android:color/transparent" />
    </shape>
  </item>
</selector>

然后,将该可绘制对象添加为您要在其中显示淡入淡出背景效果的视图背景。

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/bottom_transparent_gradient"/>

就是这样!你可以看到你想要的结果。 :)