问题 Android Glide占位符大小


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

也许我需要检查它是否已装载或其他东西,但我不知道如何。

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

6755
2017-08-18 15:31


起源



答案:


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

  .dontAnimate()

这将使你的满载线:

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

14
2017-10-27 07:17



正如该链接所提到的,.don'tAnimate()也可以替换为.animate(android.R.anim.fade_in) - Chris
真棒!有效。 - Chandru
谢谢,这很容易。 - Manish Gupta


答案:


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

  .dontAnimate()

这将使你的满载线:

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

14
2017-10-27 07:17



正如该链接所提到的,.don'tAnimate()也可以替换为.animate(android.R.anim.fade_in) - Chris
真棒!有效。 - Chandru
谢谢,这很容易。 - Manish Gupta


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

1
2018-02-04 19:52





我这样做如下所述:

我们的想法是首先根据占位符的要求将比例类型设置为附加侦听器,以便在下载图像后再次将比例类型更改为下载图像所需的比例类型。

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

1
2018-01-04 08:31





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


0
2017-08-20 16:11