【问题标题】:Android Glide placeholder sizeAndroid Glide 占位符大小
【发布时间】:2021-03-17 22:37:17
【问题描述】:

我在使用 Android Glide 时遇到问题。我正在尝试快速交换我的图像,它们只是变成了所有占位符大小,而我的占位符图像非常小。那么我需要做什么?

也许我需要检查它是否已加载或其他什么,但我不知道如何。

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

【问题讨论】:

    标签: java android android-layout android-glide


    【解决方案1】:

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

      .dontAnimate()
    

    这将使您的负载线满载:

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

    【讨论】:

    • 该链接也提到,.don'tAnimate() 也可以替换为 .animate(android.R.anim.fade_in)
    【解决方案2】:

    我会像下面提到的那样做:

    想法是最初将比例类型设置为占位符的要求并附加侦听器以在下载图像后再次将比例类型更改为下载图像的要求。

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

    【讨论】:

      【解决方案3】:

      我在这方面所做的是使用 override() 来强制图像大小。如果您有一个gridview,并且您需要在每行中有两个图像,那么您可以通过以下方式找到以像素为单位的屏幕尺寸来计算图像的正确宽度和高度:

          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
      

      一旦掌握了每张图片所需的宽度和高度,就可以轻松使用覆盖:

          Glide.with(context)
                  .load(url)
                  .placeholder(R.drawable.loading)
                  .override(placeholderWidth,placeholderHeight)                
                  .into(viewHolder.imageViewHolder);
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          如果有人像我一样从搜索中来到这里,想知道如何更改 Glide 的 placeholder 尺寸但保持图像尺寸原始(在我的情况下占位符太大),这就是解决方案。将您的 placeholder 可绘制对象包装在另一个带有标签 inset 的可绘制对象中:

          <?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 替换为任何允许的标签。此外,您可以为drawable的每一侧设置不同的insets(insetTop、insetBottom等),并在dp或%中指定插入

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-11-17
            • 1970-01-01
            • 1970-01-01
            • 2019-01-24
            • 1970-01-01
            • 1970-01-01
            • 2017-09-26
            • 2014-03-15
            相关资源
            最近更新 更多