我在使用Android Glide时遇到了问题。我想要快速更换我的图片,但它们都变成了占位符大小,而我的占位符图片非常小。我需要做什么?
也许我需要检查它是否已加载或者做些其他操作,但我不知道该怎么做。
Glide.with(this)
.load(quest.get(id).getImage())
.placeholder(R.drawable.load)
.fitCenter()
.crossFade()
.into(imageView);
我在使用Android Glide时遇到了问题。我想要快速更换我的图片,但它们都变成了占位符大小,而我的占位符图片非常小。我需要做什么?
也许我需要检查它是否已加载或者做些其他操作,但我不知道该怎么做。
Glide.with(this)
.load(quest.get(id).getImage())
.placeholder(R.drawable.load)
.fitCenter()
.crossFade()
.into(imageView);
根据 这个 在 Glide GitHub 页面上的问题,你可以通过在 Glide 请求中添加以下代码行来修复它:
.dontAnimate()
那将使你的全负荷线路:
Glide.with(context)
.load("http://lorempixel.com/150/150")
.placeholder(R.drawable.no_image)
.override(100, 100)
.dontAnimate()
.into(imageView);
我会按照以下方式来做:
首先将比例尺类型设置为占位符所需的类型,然后附加侦听器以在下载图像后将比例尺类型更改为所需的类型。
//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);
我的过渡drawable(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>
在这方面我所做的是使用override()来强制规定图像大小。如果你有一个网格视图,并且你需要每行放置两张图片,那么这就是如何找到屏幕尺寸(以像素为单位)来计算图像的正确宽度和高度:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
placeholderWidth = size.x / 2 ;
placeholderHeight = size.x * 3 / 2 ; // based on the image's height to width ratio
一旦确定了每个图像的所需宽度和高度,使用override很容易:
Glide.with(context)
.load(url)
.placeholder(R.drawable.loading)
.override(placeholderWidth,placeholderHeight)
.into(viewHolder.imageViewHolder);
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:inset="20%">
<animated-rotate
android:drawable="@drawable/ic_loading"
android:pivotX="50%"
android:pivotY="50%" />
</inset>
animated-rotate
替换为任何允许的标签。此外,您可以为可绘制对象的每个侧面设置不同的insets
(例如insetTop
,insetBottom
等),并在dp
或%
中指定插入值。