Different scale type for place holder and image

2k Views Asked by At

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

There are 2 best solutions below

1
On BEST ANSWER

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

0
On

You can use Best Image Loading Library Picasso for this. If their is no image it will show place holders image into imageview

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