Android 如何在后台应用GIF?

Android 如何在后台应用GIF?,android,android-studio,Android,Android Studio,我的约束布局上有多个视图。编辑文本和按钮。 我想使用运动背景。像是到处移动的物体。我为同样的东西做了一张GIF。。但我如何将其应用于整个背景 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.

我的约束布局上有多个视图。编辑文本和按钮。 我想使用运动背景。像是到处移动的物体。我为同样的东西做了一张GIF。。但我如何将其应用于整个背景

<?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"
tools:context=".TypeSelection">

我该如何在这个文件中放置GIF?我想在这里声明它是在整个背景中应用它的最佳选择

如果还有别的办法的话。。请帮帮我。 如果您无法获得我的问题,请告诉我。

使用此库:

在依赖项块中的应用程序级别渐变中复制此行:

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.18'

您的build.gradle如下所示:

   apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "ir.hadi_ahmadi.myapplication"
        minSdkVersion 17
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.google.android.material:material:1.1.0-alpha10'

    //gif library added here
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.18'
}
然后轻松地向布局中添加类似其他视图的视图:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/gifImageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:src="@drawable/my_gif"
        android:background="@drawable/gif_for_bg"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello!"
        android:textSize="18sp"
        app:layout_constraintTop_toBottomOf="@id/gifImageView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

您的minSdk必须为17才能使用此库。 此外,您还可以使用许多与此库相关的方法,您可以在上面的链接中阅读这些方法

请告诉我您的问题是否已解决。

使用此库:

在依赖项块中的应用程序级别渐变中复制此行:

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.18'

您的build.gradle如下所示:

   apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "ir.hadi_ahmadi.myapplication"
        minSdkVersion 17
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.google.android.material:material:1.1.0-alpha10'

    //gif library added here
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.18'
}
然后轻松地向布局中添加类似其他视图的视图:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/gifImageView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:src="@drawable/my_gif"
        android:background="@drawable/gif_for_bg"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello!"
        android:textSize="18sp"
        app:layout_constraintTop_toBottomOf="@id/gifImageView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

您的minSdk必须为17才能使用此库。 此外,您还可以使用许多与此库相关的方法,您可以在上面的链接中阅读这些方法


请告诉我您的问题是否已解决。

如哈迪·艾哈迈迪所述,您需要使用第三方库:
pl.droidsonroids.gif.GifImageView
但他的答案不会将gif作为背景,而只是将其作为标题

开始制作一个新的布局
my\u GIF\u layout
,并将所需的GIF放入其中

将my_GIF_布局放在RelativeLayout中作为根布局,一切都会正常工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/my_GIF_Layout"/>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

    ...Your layout code ...

    </RelativeLayout >

</RelativeLayout >

…您的布局代码。。。

如哈迪·艾哈迈迪所述,你需要使用第三方库:
pl.droidsonroids.gif.GifImageView
但他的回答不会将gif作为背景,而只是将其作为标题,所以

开始制作一个新的布局
my\u GIF\u layout
,并将所需的GIF放入其中

将my_GIF_布局放在RelativeLayout中作为根布局,一切都会正常工作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/my_GIF_Layout"/>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

    ...Your layout code ...

    </RelativeLayout >

</RelativeLayout >

…您的布局代码。。。

我想最好的方法是使用match\u parent match\u parent声明图像视图,并使用Glide库加载gif。使用:或@WelbertMoreira这样的库我在另一个应用程序中尝试过,但问题是,如果我将高度和宽度的图像视图声明为match\u parent,我将无法使用其他按钮和编辑文本。您可以使用RelativeLayout或FrameLayout,并将其他视图放在上面。@WelbertMoreira Dam。。是的。。我会再打给你的。我想最好的方法是用match\u parent match\u parent声明一个ImageView,并使用Glide库加载gif。使用像:或@WelbertMoreira这样的库我在另一个应用程序中尝试过,但问题是,如果我声明高度和宽度为match\u parent的图像视图,我将无法使用其他按钮和编辑文本。您可以使用RelativeLayout或FrameLayout,并将其他视图放在上面。@WelbertMoreira Dam。。是的。。我会回复您。获取此错误错误:启动失败:生成文件“C:\Users\deep\AndroidStudioProjects\Gggif\app\build.gradle”:28:应为“}”,找到“:”@第28行,第25列。pl.droidsonroids.gif:android gif drawable:1.2.18^最小SDK为21目标SDK为28获取此错误:启动失败:生成文件“C:\Users\deep\AndroidStudioProjects\Gggif\app\build.gradle”:28:应为“}”,找到“:”@第28行,第25列。pl.droidsonroids.gif:android gif可绘制:1.2.18^最小SDK为21目标SDK为28完成。。把@Hadi Ahmadi的代码和你的建议结合起来。完成了。。结合@Hadi Ahmadi的代码和您的建议。