当前位置: 首页 > 知识库问答 >
问题:

使用Glide在ImageView中显示GIF

艾文斌
2023-03-14

我第一次使用Glide在ImageView中显示GIF。我已经按照几个站点的方式对其进行了编码。但它不起作用。我已经给出了以下所有代码:(如果我弄错了什么,请告诉我)

项目级build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.google.gms:google-services:2.1.0-alpha5'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序级构建。gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.winner.myapplication"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:22.2.1'
    //compile files('libs/ion-2.1.6.jar')
    //compile files('libs/androidasync-2.1.6.jar')
    compile 'com.github.bumptech.glide:glide:3.7.0'
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.winner.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/layoutImage1"
        android:orientation="vertical"
        android:layout_gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/layoutImage2"
        android:orientation="vertical"
        android:layout_below="@+id/layoutImage1"
        android:layout_gravity="center"
        >
        <ImageView
            android:id="@+id/test_image"
            android:layout_width="160dp"
            android:layout_height="90dp"
            android:scaleType="fitXY"
            android:layout_gravity="center"
            android:src="@drawable/test"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/layoutImage3"
        android:orientation="vertical"
        android:layout_below="@+id/layoutImage2"
        android:layout_gravity="center"
        >
        <TextView android:text="Hello World!" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="right"
            android:id="@+id/submit"
            android:text="Submit" />

    </LinearLayout>

</RelativeLayout>

活动Java代码:

package com.example.winner.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(onClickSubmit);
    }

    View.OnClickListener onClickSubmit = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView iView = (ImageView) findViewById(R.id.test_image);
            Glide.with(getApplicationContext()).
                    load("http://i.imgur.com/1ALnB2s.gif").into(iView);

        }
    };
}

单击“提交”按钮后,我没有看到GIF图像。

共有3个答案

郑嘉年
2023-03-14

要使用glide加载gif,glide需要几秒钟来处理gif。因此,图像只能在该时间之后加载。所以,虽然你可以使用占位符。

glide.with(this). load("http://i.imgur.com/1ALnB2s.gif"). asGif(). place eHolder(R.drawable.any_normal_image.png). into(iView);

习高格
2023-03-14

你可以用这个。它对我有用。我希望它也对你有用。

ImageView iView = (ImageView) findViewById(R.id.test_image); 
    GlideDrawableImageViewTarget imageViewPreview = new GlideDrawableImageViewTarget(iView);
    Glide
            .with(this)
            .load(url)
            .listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {                       
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    return false;
                }
            })
            .into(imageViewPreview);
}

它适用于图像和GIF。

爱花蜂
2023-03-14

正如Glide文档所述,

可以使用.asGif()强制库加载动画gif,如果未加载,则加载失败:

Glide.with(activity).load(url).asGif().into(view);

我建议你做以下事情

ImageView iView = (ImageView) findViewById(R.id.test_image);
if (iView != null)
    Glide.with(this).load("http://i.imgur.com/1ALnB2s.gif").asGif().into(iView);

有关更多信息,请考虑阅读有关加载Gif的Glide示例https://github.com/bumptech/glide/wiki

提供的代码示例效果良好!

 类似资料:
  • 我正在使用Glide库在imageview中加载图像,并使用以下代码。 灰色占位符在图像未加载之前可见,但在图像在imageview中加载后,占位符仍会出现,并在该图像后显示一些空白。 我如何解决这个问题。如果你有任何想法,请帮助我。

  • 问题内容: 如何使用Glide库将位图加载到ImageView中?我想使用文本创建自定义图像,然后使用Glide将其加载到imageview中。 这是我用文本创建自定义位图的方法 But when i try to load this bitmap using glide i’m getting error 问题答案: @rookiedev是正确的, Glide中没有load(Bitmap)它,这

  • 如何使用Glide库将位图加载到ImageView中?我想用文本创建一个自定义图像,并使用Glide将其加载到imageview中。 这是我用文本创建自定义位图的方法 但是当我尝试使用glide加载这个位图时,我得到了一个错误

  • 因此,在Android的ImageView中显示图像非常简单。我从图库中挑选了一张图片,保存了它到数据库的链接,然后尝试在ImageView中显示它。 我也尝试了这个小的图像不是由相机拍摄的,这是工作的。但每次我选择用手机相机拍摄的图像时,都会有一个错误: 那么,在超过300万像素的相机中,如何在我的ImageView中显示图像呢?

  • 我想在JavaFX的ImageView中显示图像。图像应该从SQLite数据库中获取,并在场景加载时显示。我使用以下代码片段从数据库获取代码,并在之前创建的ImageView中显示它。但是图像没有按预期显示。我做错了吗?P. S.我测试了改变路径在两个和当读取文件从目录到。但似乎没有什么奏效。但是当我手动将一个图像文件放在src目录中并尝试读取它时,它就像一个魅力。但是我想展示一张从数据库中提取的

  • 问题内容: 我有这段代码,它已使用Webservice获取已经转换为字符串的BLOB图像。这是JSON输出。 这是我在android中获取JSON并将其显示在布局中的代码。除图像外,其他值也已显示。 我的代码有什么问题,为什么图像没有显示在ImageView中?提前致谢。:) 问题答案: 将 base64 转换为 位图后 将位图调整为Imageview的高度和宽度