占位符和图像的不同比例类型

Different scale type for place holder and image

我想要 fitXY scaleType 用于 imagefitCenter 用于 place holder。我正在使用 volleyapi 获取数据,并使用 glide 加载图像。 我怎么知道图像是空的?

XML 和代码

<ImageView
    android:id="@+id/NewsImage"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:scaleType="fitCenter" />

Glide.with(context).load(news.getUrlToImage()).placeholder(R.drawable.place_holder).dontAnimate().into(holder.NewsImage);

1.USING 聆听者

使用 GLIDE,您可以添加一个用于图像加载的侦听器。加载图像后,更改 ImageView 的 SCALE 类型。

Glide.with(getActivity())
 .load(args.getString(IMAGE_TO_SHOW))
 .listener(new RequestListener<String, GlideDrawable>() {
     @Override
     public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
         return false;
     }

     @Override
     public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
         imgview.setScaleType(ImageView.ScaleType.FIT_XY);
         return false;
     }
 })
 .into(imgview)

2.USING 滑行

您可以对图像使用一种 GLIDE 转换或创建您的自定义转换 https://github.com/bumptech/glide/wiki/Transformations

您可以为此使用最佳图像加载库Picasso。 如果他们没有图像,它将在图像视图中显示占位符图像

    Picasso.get() 
            .load("http://i.imgur.com/DvpvklR.png") //loading url image
            .placeholder(R.drawable.custom_image) // during loading this image will be set imageview 
            .error(R.drawable.error) //if image is failed to load - this image is set to imageview 
            .networkPolicy(NetworkPolicy.OFFLINE) //stores images for offline view 
            .resize(50, 50) //resize 
            .centerCrop()   // apply scaling OR 
            .fit()          //apply scaling OR 
            .centerInside() //scaling 
            .into(imageView, new Callback() { 
                @Override 
                public void onSuccess() { 
                    //called when image is loaded successfully.. \n \n
                } 
                @Override 
                public void onError(Exception e) { 
                    //called when image is failed to be loaded into. 
                } 
            });