首页 > 解决方案 > 如何在android studio中的背景图像上显示gif

问题描述

我想将 Gif 显示为背景图像的上层。我到底想在背景中有什么图像,在该图像上我也想显示 gif,但是当我运行代码时,我只看到 gif 背景图像被隐藏了有人可以告诉我现在应该做什么

我的 XML 代码在这里

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ice"
tools:context=".MainActivity">

<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/gif"
    android:background="@drawable/ice"

    /></android.support.constraint.ConstraintLayout>

我在 GifImage 视图和布局背景中都使用了背景图像,但在这两种情况下它都没有显示背景。

标签: javaandroidandroid-imageview

解决方案


这可以通过相对布局来实现,获取一个 imageview 并将其高度和宽度设置为 match_parent ,然后将背景添加到该 imageview 最后在图像视图下方添加您的 gif 视图,这将基本上将其对齐在 imageview 的顶部,这应该可以解决问题:

<Relativelayout>
<Imageview 
android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ice"/>
<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"  //layout added at last will be shown on top
    android:src="@drawable/gif"
    android:background="@drawable/ice"

    />
</Relativelayout>

推荐阅读