使用Glide在ImageView中显示GIF [英] Displaying GIF in ImageView using Glide

查看:98
本文介绍了使用Glide在ImageView中显示GIF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Glide来在ImageView中显示GIF.我已经按照在多个站点上给出的方式对其进行了编码.但这是行不通的.我已经给出了下面的所有代码:(如果我弄错了任何东西,请告诉我)

I am using Glide for the very fist time to display GIF inside ImageView. I have coded it the way it is given across several sites. But it is not working. I have given all the code below:(Please let me know if I have mistaken anything)

项目级别build.gradle

Project level 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
}

应用程序级别的build.gradle文件:

App level build.gradle file:

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

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图像.

I do not see the GIF image after clicking on the Submit button.

推荐答案

正如Glide文档所说,

As Glide Documentation says,

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

You can use .asGif() to force the library to load an animated gif and fail if it is not :

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

For more informations, please consider reading Glide examples about loading Gif https://github.com/bumptech/glide/wiki

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

Code example provided works well !

这篇关于使用Glide在ImageView中显示GIF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆