可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to use Glide
Android library to download image and show in ImageView
.
in previous version we used:
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);
But I have seen Glide documentation:
it says use GlideApp.with()
instead Glide.with()
my concern is missing placeholder, error, GlideApp and other options.
I am using
compile 'com.github.bumptech.glide:glide:4.0.0'
Where am I doing wrong? with reference here
how GlideApp.with()
has been used??
The API is generated in the same package as the AppGlideModule
and is named GlideApp
by default. Applications can use the API by starting all loads with GlideApp.with()
instead of Glide.with()
:
GlideApp.with(fragment) .load(myUrl) .placeholder(placeholder) .fitCenter() .into(imageView);
回答1:
Try using 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);
EDIT
If .setDefaultRequestOptions(requestOptions)
does not work, 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);
回答2:
If you use Glide package dependences compile 'com.github.bumptech.glide:glide:3.7.0'
than use should to use below code
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.
Latest update version compile com.github.bumptech.glide:glide:4.1.1
than use should to use below code
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
回答3:
If you wanna use GlideApp you have to add to dependencies
annotation processor like on the screenshot

then include an AppGlideModule implementation in your application:
@GlideModule public final class MyAppGlideModule extends AppGlideModule {}
Do not forget about the @GlideModule
annotation. Then you need to Build project. And GlideApp
will be automatically generated. Hope this is still helpful.
回答4:
dependencies
compile 'com.github.bumptech.glide:glide:4.1.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'
add an appropriately annotated AppGlideModule implementation
import com.bumptech.glide.annotation.GlideModule; import com.bumptech.glide.module.AppGlideModule; @GlideModule public final class MyAppGlideModule extends AppGlideModule{}
In addition, if you have used the jack option, in order to avoid the following similar errors, you need use Android Studio 3.0.0 preview.
Error:Execution failed for task ':app:transformJackWithJackForDebug'. com.android.jack.ir.JNodeInternalError: java.lang.Exception: java.lang.AssertionError: No yet implemented
回答5:
We have no need to use RequestOptions also.
The generated API adds a GlideApp class, that provides access to RequestBuilder and RequestOptions subclasses. The RequestOptions subclass contains all methods in RequestOptions and any methods defined in GlideExtensions. The RequestBuilder subclass provides access to all methods in the generated RequestOptions subclass without having to use apply:
Using Glide :-
A request without the generated API might look like this:
Glide.with(fragment) .load(url) .apply(centerCropTransform() .placeholder(R.drawable.placeholder) .error(R.drawable.error) .priority(Priority.HIGH)) .into(imageView);
Using GlideApp :-
With the generated API, the RequestOptions
calls can be inlined:
GlideApp.with(fragment) .load(url) .centerCrop() .placeholder(R.drawable.placeholder) .error(R.drawable.error) .priority(Priority.HIGH) .into(imageView);
You can still use the generated RequestOptions subclass to apply the same set of options to multiple loads, but generated RequestBuilder subclass may be more convenient in most cases.