Search code examples
androidscaletype

Different scale type for place holder and image


I want fitXY scaleType for image and fitCenter for place holder. I am fetching data from api using volley and loading image using glide. How am I supposed to know that image is null ?

XML and code

<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);

Solution

  • 1.USING LISTENER

    Using GLIDE you can add a listener for image loading. Once the image is loaded change the SCALE type of the ImageView.

    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

    You can use one of the GLIDE transformations for the image or create your custom transformation https://github.com/bumptech/glide/wiki/Transformations