在图片视图中心固定占位符

时间:2018-09-28 05:51:32

标签: android imageview resize placeholder

通过使用 Glide ,我在图像视图上设置了占位符,但问题是,它在整个图像视图中显示,我希望占位符的大小为50dp / 50dp,但是图片应为setScale(fitXY)

我解决了glide listener中的问题,我正在运行时调整图像视图的大小,如果图像是从服务器获取的,则图像视图为 width,height(x,y),否则为(x,y)

Glide.with(context)
            .load(news.getUrlToImage())
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(131, 131);
                    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                    holder.NewsImage.setLayoutParams(layoutParams);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(495, 360);
                    holder.NewsImage.setLayoutParams(layoutParams);
                    holder.NewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
                    return false;
                }
            }).apply(new RequestOptions().placeholder(R.drawable.place_holder).dontAnimate())
            .into(holder.NewsImage);

但是问题是,如果没有来自服务器的图像,它会显示 size(50,50)的占位符,当它有图像并滑行时,上传图像需要花费几秒钟的时间,在整个图片视图上显示占位符的时间。

帮助我

I want this

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码。

  RequestOptions options = new RequestOptions()
            .priority(Priority.NORMAL)
            .placeholder(R.drawable.placeholderImageName)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .fitCenter()
            .error(R.drawable.errorImageName);
    Glide.with(mContext)
            .load("ImagePath")
            .apply(options)
            .into(Img_imageView);

答案 1 :(得分:0)

在xml中添加android:scaleType="center"

然后,Glide使您的真实图像适合中心,并且占位符通过xml位于中心。 您可以在Glide: load drawable but don't scale placeholder

中查看详细信息

如果要将scaleType设置为“ fitXY” 请尝试以下。

holder.NewsImage.setScaleType(ImageView.ScaleType.CENTER);
Glide.with(context)
        .load(news.getUrlToImage())
        .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(131, 131);
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                holder.NewsImage.setLayoutParams(layoutParams);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(495, 360);
                holder.NewsImage.setLayoutParams(layoutParams);
                holder.NewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
                return false;
            }
        }).apply(new RequestOptions().placeholder(R.drawable.place_holder).dontAnimate())
        .into(holder.NewsImage);