Ubaid Tanoli
Ubaid Tanoli

Reputation: 173

fixed placeholder in the center of image view

By using Glide I set placeholder on image view but the problem is, it is shown on whole image view, I want my placeholder size to be 50dp/50dp but image should be setScale(fitXY).

I solved the problem in glide listener, I am resizing image view on runtime, if it get image from server then imageview width,height(x,y) if not then (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);

But the problem is, if there is no image from server it show placeholder of size(50,50) fine and when it has image and glide take some mili seconds to upload image, in that time it shows placeholder on whole image view.

HELP ME

I want this

Upvotes: 0

Views: 2224

Answers (2)

Yogesh Sarvaiya
Yogesh Sarvaiya

Reputation: 571

You can try this code.

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

Upvotes: 0

sso.techie
sso.techie

Reputation: 146

Add android:scaleType="center" in xml.

Then, Glide makes your real image fitted center and placeholder is located in center by xml. You can check the details in Glide: load drawable but don't scale placeholder

If you want that scaleType to "fitXY" Try below.

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

Upvotes: 1

Related Questions