溶解背景可绘制对象

Dissolve a background drawable

我正在尝试用背景图像溶解做类似的事情:

这是我正在使用的代码:

<LinearLayout
    android:id="@+id/layout"
    android:background="@color/yellow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizonal">        
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>

我正在以编程方式设置背景图片,如下所示:

((ImageView)findViewById(R.id.image)).setBackground(ContextCompat.getDrawable(this, R.drawable.bg_image);

如果我不得不猜测的话,我需要设置布局的不透明度,因为溶解的不是图像而是图像周围的布局。我搜索过,我想我需要使用 setAlpha,但我不希望整个图像透明。

您可以在 XML 资源中使用渐变。

bg_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="@color/blue"
        android:endColor="@android:color/transparent"/>

</shape>

activity_main.xml

在背景布局和设置的渐变之上设置图像。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/dimen_200dp"
    android:orientation="vertical"
    android:background="@drawable/image"
    app:layout_constraintTop_toBottomOf="@id/textView_version">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@drawable/bg_gradient"/>
</LinearLayout>

Result