2017-08-03 39 views
19

我想使用Glide Android庫下載圖像並顯示在ImageViewGlide-4.0.0缺少佔位符,錯誤,GlideApp

在以前的版本中,我們使用:

Glide.with(mContext).load(imgUrl) 
       .thumbnail(0.5f) 
       .placeholder(R.drawable.PLACEHOLDER_IMAGE_NAME) 
       .error(R.drawable.ERROR_IMAGE_NAME) 
       .crossFade() 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .into(imageView); 

但是我看到的滑翔文檔:

it says use GlideApp.with() instead Glide.with()

我擔心的是缺少佔位符,錯誤,GlideApp和其他選項。

我使用

compile 'com.github.bumptech.glide:glide:4.0.0' 

我在哪裏做錯了嗎? 與參考here

如何GlideApp.with()已被使用?

該API在與AppGlideModule相同的包中生成,默認情況下被命名爲GlideApp。應用程序可以通過啓動所有負載通過API使用GlideApp.with(),而不是Glide.with()

GlideApp.with(fragment) 
    .load(myUrl) 
    .placeholder(placeholder) 
    .fitCenter() 
    .into(imageView); 
+1

的可能的複製[在滑翔佔位符/錯誤/後退V4](https://stackoverflow.com/questions/45187661/placeholder-error-fallback-in-glide-v4) –

+2

你有想法左右GlideApp?如何使用和何時使用?..如果不是請upvote –

+0

你缺少第2步檢查此:http://bumptech.github.io/glide/doc/generatedapi.html –

回答

41

試試這個使用RequestOptions

RequestOptions requestOptions = new RequestOptions(); 
requestOptions.placeholder(R.drawable.ic_placeholder); 
requestOptions.error(R.drawable.ic_error); 

Glide.with(context) 
    .setDefaultRequestOptions(requestOptions) 
    .load(url).into(holder.imageView); 

編輯

if.setDefaultRequestOptions(requestOptions)method is not work than use.apply(requestOptions)

Glide.with(MainActivity.this) 
      .load(url) 
      .apply(requestOptions) 
      .into(imageview); 
// or this 
Glide.with(MainActivity.this) 
      .load(url) 
      .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle)) 
      .into(imageview); 
+0

我可以知道投票的理由嗎? –

+0

誰投下了......不知道..如何檢查?順便說一句,它再次0由我upvoting .. :) –

+0

,我也不知道@RiteshBhavsar它在我的設備中工作與新的Glide庫,你可以檢查它 –

5

如果你想使用GlideApp必須添加到dependencies註解處理器一樣的截圖

how to add GlideApp to your project

然後在您的應用程序的AppGlideModule實現:

@GlideModule 
public final class MyAppGlideModule extends AppGlideModule {} 

不要忘了@GlideModule註解。 然後你需要建立項目。並且GlideApp會自動生成。 希望這仍然有幫助。

+0

同步gradle後它不工作。但我們需要重建才能使其工作。 –

+0

它的工作..謝謝。 –

11

如果使用腳墊包依賴性compile 'com.github.bumptech.glide:glide:3.7.0'不是使用下面的代碼

GlideApp 
    .with(your context) 
    .load(url) 
    .centerCrop() 
    .placeholder(R.drawable.loading_image) 
    .error(R.drawable.error_image) 
    .into(myImageView); 

Note: As in doc

Round Pictures: CircleImageView/CircularImageView/RoundedImageView are known to have issues with TransitionDrawable (.crossFade() with .thumbnail() or .placeholder()) and animated GIFs, use a BitmapTransformation (.circleCrop() will be available in v4) or .dontAnimate() to fix the issue.

最新版本,compile com.github.bumptech.glide:glide:4.1.1不是使用下面的代碼

Glide.with(this) 
    .load(url) 
    .apply(new RequestOptions() 
       .placeholder(R.mipmap.ic_loading_image) 
       .centerCrop() 
       .dontAnimate() 
       .dontTransform()) 
    .into(imageView); 

See Latest version of glide, Bug fixes, Features

1

依賴

compile 'com.github.bumptech.glide:glide:4.1.1' 
annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1' 

添加適當註釋AppGlideModule實施

import com.bumptech.glide.annotation.GlideModule; 
import com.bumptech.glide.module.AppGlideModule; 


@GlideModule 
public final class MyAppGlideModule extends AppGlideModule{} 

另外,如果你已經使用了千斤頂的選擇,以避免以下類似的錯誤,你需要使用Android 3.0.0工作室預覽。

Error:Execution failed for task ':app:transformJackWithJackForDebug'. com.android.jack.ir.JNodeInternalError: java.lang.Exception: java.lang.AssertionError: No yet implemented