2016-08-20 128 views
5

沒能找到完美的解決方案中的佔位符加載GIF圖像的滑翔版本3.7.0如何的佔位符加載GIF圖片滑翔/畢加索/離子等

Glide 
    .with(context) 
    .load("imageUrl") 
    .asGif() 
    .placeholder(R.drawable.gifImage) 
    .crossFade() 
    .into(imageView) 

嘗試asGif()屬性了。但沒有運氣!

+0

你會喜歡什麼加載gif圖片,從庫中滑翔/離子...我已經使用了兩種,Glide load gif非常緩慢,而Ion的載入速度比滑翔庫快2倍。但是在圖像質量方面,Glide的離子效果更好....您對它的建議@Dinesh ... –

回答

11

這裏是最好的方式..

Glide.with(getContext()).load(item[position]) 
       .thumbnail(Glide.with(getContext()).load(R.drawable.preloader)) 
       .fitCenter() 
       .crossFade() 
       .into(imageView); 
+1

Op應將此標記爲正確答案 – suku

4

使用進度爲加載GIF:

Glide.with(context). 
      load(url) 
      .listener(new RequestListener<String, GlideDrawable>() { 
       @Override 
       public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) { 
        progressBar.setVisibility(View.GONE); 
        return false; 
       } 

       @Override 
       public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { 
        progressBar.setVisibility(View.GONE); 
        return false; 
       } 
      }) 
      .crossFade(1000) 
      .into(imageView); 
3

我不喜歡它提到如下:

的想法是用來創建GIF transition drawables &將標尺類型設置爲最初按照地點所有者的要求&附加聽衆再次將標尺類型更改爲按要求d下載完圖像後下載的圖像。 (最後一步可以,如果你不需要它跳過)

//ivBuilderLogo = Target ImageView 
//Set the scale type to as required by your place holder 
//ScaleType.CENTER_INSIDE will maintain aspect ration and fit the placeholder inside the image view 
holder.ivBuilderLogo.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

//AnimationDrawable is required when you are using transition drawables 
//You can directly send resource id to glide if your placeholder is static 
//However if you are using GIFs, it is better to create a transition drawable in xml 
//& use it as shown in this example 
AnimationDrawable animationDrawable; 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    animationDrawable=(AnimationDrawable)context.getDrawable(R.drawable.anim_image_placeholder); 
else 
    animationDrawable=(AnimationDrawable)context.getResources().getDrawable(R.drawable.anim_image_placeholder); 
animationDrawable.start(); 

Glide.with(context).load(logo_url) 
        .placeholder(animationDrawable) 
        .listener(new RequestListener<String, GlideDrawable>() { 
         @Override 
         public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) 
         { 
          return false; 
         } 

         //This is invoked when your image is downloaded and is ready 
         //to be loaded to the image view 
         @Override 
         public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) 
         { 
         //This is used to remove the placeholder image from your ImageView 
         //and load the downloaded image with desired scale-type(FIT_XY in this case) 
         //Changing the scale type from 'CENTER_INSIDE' to 'FIT_XY' 
         //will stretch the placeholder for a (very) short duration, 
         //till the downloaded image is loaded 
         //setImageResource(0) removes the placeholder from the image-view 
         //before setting the scale type to FIT_XY and ensures that the UX 
         //is not spoiled, even for a (very) short duration 
          holder.ivBuilderLogo.setImageResource(0); 
          holder.ivBuilderLogo.setScaleType(ImageView.ScaleType.FIT_XY); 
          return false; 
         } 
        }) 
        .into(holder.ivBuilderLogo); 

我的過渡繪製(R.drawable.anim_image_placeholder):

<?xml version="1.0" encoding="utf-8"?> 
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/loading_frame1" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame2" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame3" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame4" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame5" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame6" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame7" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame8" android:duration="100" />--> 
    <item android:drawable="@drawable/loading_frame9" android:duration="100" /> 
    <!--<item android:drawable="@drawable/loading_frame10" android:duration="100" />--> 
</animation-list> 

(如果使用的是靜態圖像不需要)

0

(static_placeholder是,比方說,GIF的第一幀)

Glide... 
.load("http://...") 
.placeholder(R.drawable.static_placeholder) 
.thumbnail(Glide.with(...).load(R.raw.gif_placeholder)) 
.dontAnimate() //so there's no weird crossfade 

佔位符被設定多earli呃比縮略圖,所以它防止「長」空的白色ImageViews。儘管你可以跳過佔位符來簡化。

或另一種選擇是使用AnimationDrawable(你可以從here您的GIF轉換爲AnimationDrawable):

AnimationDrawable animPlaceholder = (AnimationDrawable) ContextCompat.getDrawable(activity, R.drawable.animatedDrawable); 
animPlaceholder.start(); // probably needed 
Glide... 
.load("http://...") 
.placeholder(animPlaceholder) 

編號:link