2016-04-11 27 views
1

ImageViewにGIFを表示するには、Glideをすっかり利用しています。私はそれを複数のサイトに渡って与えられた方法でコーディングしました。しかし、それは動作していません。 (私は何も間違っている場合は私に知らせてください)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 
} 

アプリケーションレベルのbuild.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画像が表示されません。グライドドキュメントが言うように

+1

私はそれが役に立つかもしれないと思うhttp://stackoverflow.com/questions/31082330/show-gif-file-with-glide-image-loading-and-caching-library –

答えて

2

あなたはアニメーションGIFをロードし、そうでない場合に失敗するライブラリを強制的に.asGif()を使用することができます。

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

私は何をするあなたをrecommandうより多くの情報については

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 https://github.com/bumptech/glide/wiki

0123についてグライド例を読んでみてください。

コード例が正常に動作します。

+0

関連性はないが、私はCahrmander gif xDありがとう – magorich

0

これで行くことができます。それは私のために働いた。それがあなたにとってもうまくいくことを願っています。

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と同様に両方の画像に使用できます。

0

グライドを使用してgifをロードするには、グライド処理に数秒かかります。だから、イメージはその時間の後にしかロードできません。だから、あなたはプレースホルダを使うことができます。

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

関連する問題