安卓Glide占位符大小

19

我在使用Android Glide时遇到了问题。我想要快速更换我的图片,但它们都变成了占位符大小,而我的占位符图片非常小。我需要做什么?

也许我需要检查它是否已加载或者做些其他操作,但我不知道该怎么做。

Glide.with(this)
    .load(quest.get(id).getImage())
    .placeholder(R.drawable.load)
    .fitCenter()
    .crossFade()
    .into(imageView);
5个回答

19
根据 Glide GitHub 页面上的这个问题,您可以通过将以下行添加到 Glide 请求中来解决它:

根据 这个 在 Glide GitHub 页面上的问题,你可以通过在 Glide 请求中添加以下代码行来修复它:

  .dontAnimate()

那将使你的全负荷线路:

  Glide.with(context)
            .load("http://lorempixel.com/150/150")
            .placeholder(R.drawable.no_image)
            .override(100, 100)
            .dontAnimate()
            .into(imageView);

3
那个链接也提到了,.don'tAnimate() 也可以替换为 .animate(android.R.anim.fade_in)。 - Chris

3

我会按照以下方式来做:

首先将比例尺类型设置为占位符所需的类型,然后附加侦听器以在下载图像后将比例尺类型更改为所需的类型。

//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>

1

在这方面我所做的是使用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);

0
如果有人像我一样通过搜索来到这里,并且想知道如何更改Glide的placeholder大小但保持图像大小不变(在我的情况下,placeholder太大了),这里是解决方案。将您的placeholder drawable包装在带有标签inset的另一个drawable中:
<?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(例如insetTopinsetBottom等),并在dp%中指定插入值。

这很不错,但是我需要从一组可绘制的资源中动态添加一个可绘制的资源。我是否需要为所有这些占位符创建XML包装器才能使其与此解决方案配合使用呢?;(? - Dwagner

0
如果我正确理解您的问题,您需要加载图像,但它们必须具有比占位符图像更大的宽度和高度。 要解决您的问题,您首先应该阅读Glide的文档。 我看不到xml文件中的ImageView定义,但我认为您使用了wrap_content属性来设置宽度和高度。 如果我是对的,那么这就是您的瓶颈所在。 在开始加载图像之前,Glide会获取确切的ImageView宽度和高度。然后,它从网络加载图像,并同时根据ImageView属性(宽度或高度)调整其大小。 这就是为什么加载后您会得到小图像的原因。 要解决您的问题,只需将ImageView的宽度或高度设置为您希望看到的值。图像的另一侧将由Glide逻辑自动计算。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接