Set ImageView`s size with Glide for each item in RecyclerView, the image size is changing










0















In RecyclerView, I want to load images for each item using their original aspect ratio. The server don`t return the size of image.



My code is as follows:



@Override
public void onBindViewHolder(K holder, int position)
// ....
Glide.with(mContext)
.load(imgUrls.get(position))
.listener(new RequestListener<String, GlideDrawable>()
// ...
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
int drawableWidth = resource.getIntrinsicWidth();
int drawableHeight = resource.getIntrinsicHeight();
double ratio = (drawableWidth * 1.0) / (drawableHeight * 1.0);
// depend on the ratio to set the imageView`s LayoutParams to change the imageView`s size.
.........
layoutParams.width = xx;
layoutParams.height = xx;
imageView.setLayoutParams(layoutParams);
return false;

)
.into(imageView);



But when I have scrolled the RecyclerView or refreshed the RecyclerView, the value of drawableWidth was changed, so the size of the imageView was changed that is not expected.



What is the right way to set ImageView`s size in RecyclerView?










share|improve this question
























  • in order to get original aspect ratio you should use wrap content form image view and use the glide no need to specify size

    – Jins Lukose
    Nov 14 '18 at 4:39















0















In RecyclerView, I want to load images for each item using their original aspect ratio. The server don`t return the size of image.



My code is as follows:



@Override
public void onBindViewHolder(K holder, int position)
// ....
Glide.with(mContext)
.load(imgUrls.get(position))
.listener(new RequestListener<String, GlideDrawable>()
// ...
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
int drawableWidth = resource.getIntrinsicWidth();
int drawableHeight = resource.getIntrinsicHeight();
double ratio = (drawableWidth * 1.0) / (drawableHeight * 1.0);
// depend on the ratio to set the imageView`s LayoutParams to change the imageView`s size.
.........
layoutParams.width = xx;
layoutParams.height = xx;
imageView.setLayoutParams(layoutParams);
return false;

)
.into(imageView);



But when I have scrolled the RecyclerView or refreshed the RecyclerView, the value of drawableWidth was changed, so the size of the imageView was changed that is not expected.



What is the right way to set ImageView`s size in RecyclerView?










share|improve this question
























  • in order to get original aspect ratio you should use wrap content form image view and use the glide no need to specify size

    – Jins Lukose
    Nov 14 '18 at 4:39













0












0








0








In RecyclerView, I want to load images for each item using their original aspect ratio. The server don`t return the size of image.



My code is as follows:



@Override
public void onBindViewHolder(K holder, int position)
// ....
Glide.with(mContext)
.load(imgUrls.get(position))
.listener(new RequestListener<String, GlideDrawable>()
// ...
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
int drawableWidth = resource.getIntrinsicWidth();
int drawableHeight = resource.getIntrinsicHeight();
double ratio = (drawableWidth * 1.0) / (drawableHeight * 1.0);
// depend on the ratio to set the imageView`s LayoutParams to change the imageView`s size.
.........
layoutParams.width = xx;
layoutParams.height = xx;
imageView.setLayoutParams(layoutParams);
return false;

)
.into(imageView);



But when I have scrolled the RecyclerView or refreshed the RecyclerView, the value of drawableWidth was changed, so the size of the imageView was changed that is not expected.



What is the right way to set ImageView`s size in RecyclerView?










share|improve this question
















In RecyclerView, I want to load images for each item using their original aspect ratio. The server don`t return the size of image.



My code is as follows:



@Override
public void onBindViewHolder(K holder, int position)
// ....
Glide.with(mContext)
.load(imgUrls.get(position))
.listener(new RequestListener<String, GlideDrawable>()
// ...
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
int drawableWidth = resource.getIntrinsicWidth();
int drawableHeight = resource.getIntrinsicHeight();
double ratio = (drawableWidth * 1.0) / (drawableHeight * 1.0);
// depend on the ratio to set the imageView`s LayoutParams to change the imageView`s size.
.........
layoutParams.width = xx;
layoutParams.height = xx;
imageView.setLayoutParams(layoutParams);
return false;

)
.into(imageView);



But when I have scrolled the RecyclerView or refreshed the RecyclerView, the value of drawableWidth was changed, so the size of the imageView was changed that is not expected.



What is the right way to set ImageView`s size in RecyclerView?







android android-recyclerview glide






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 2:19









MonLiH

8110




8110










asked Nov 14 '18 at 4:20









zq Liuzq Liu

83




83












  • in order to get original aspect ratio you should use wrap content form image view and use the glide no need to specify size

    – Jins Lukose
    Nov 14 '18 at 4:39

















  • in order to get original aspect ratio you should use wrap content form image view and use the glide no need to specify size

    – Jins Lukose
    Nov 14 '18 at 4:39
















in order to get original aspect ratio you should use wrap content form image view and use the glide no need to specify size

– Jins Lukose
Nov 14 '18 at 4:39





in order to get original aspect ratio you should use wrap content form image view and use the glide no need to specify size

– Jins Lukose
Nov 14 '18 at 4:39












2 Answers
2






active

oldest

votes


















1














Try this one:



 Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition)

/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
mImageView.requestLayout();
mImageView.getLayoutParams().height = newHeight;
mImageView.getLayoutParams().width = newWidth;


// Set the scale type for ImageView image scaling
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

mImageView.setImageBitmap(bitmap);

);





share|improve this answer

























  • You are not using the w and h anywhere in your code

    – Vivek Mishra
    Nov 14 '18 at 4:28











  • Please check my latest updated code thanks

    – Sultan Mahmud
    Nov 14 '18 at 4:33











  • Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

    – zq Liu
    Nov 14 '18 at 4:42












  • It is worked...

    – zq Liu
    Nov 14 '18 at 5:01


















1














You can set the size of the image views using glide it self.



Glide 
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);





share|improve this answer























  • Thanks, but it`s not in line with the requirements

    – zq Liu
    Nov 14 '18 at 4:35










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293145%2fset-imageviews-size-with-glide-for-each-item-in-recyclerview-the-image-size-is%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try this one:



 Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition)

/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
mImageView.requestLayout();
mImageView.getLayoutParams().height = newHeight;
mImageView.getLayoutParams().width = newWidth;


// Set the scale type for ImageView image scaling
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

mImageView.setImageBitmap(bitmap);

);





share|improve this answer

























  • You are not using the w and h anywhere in your code

    – Vivek Mishra
    Nov 14 '18 at 4:28











  • Please check my latest updated code thanks

    – Sultan Mahmud
    Nov 14 '18 at 4:33











  • Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

    – zq Liu
    Nov 14 '18 at 4:42












  • It is worked...

    – zq Liu
    Nov 14 '18 at 5:01















1














Try this one:



 Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition)

/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
mImageView.requestLayout();
mImageView.getLayoutParams().height = newHeight;
mImageView.getLayoutParams().width = newWidth;


// Set the scale type for ImageView image scaling
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

mImageView.setImageBitmap(bitmap);

);





share|improve this answer

























  • You are not using the w and h anywhere in your code

    – Vivek Mishra
    Nov 14 '18 at 4:28











  • Please check my latest updated code thanks

    – Sultan Mahmud
    Nov 14 '18 at 4:33











  • Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

    – zq Liu
    Nov 14 '18 at 4:42












  • It is worked...

    – zq Liu
    Nov 14 '18 at 5:01













1












1








1







Try this one:



 Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition)

/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
mImageView.requestLayout();
mImageView.getLayoutParams().height = newHeight;
mImageView.getLayoutParams().width = newWidth;


// Set the scale type for ImageView image scaling
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

mImageView.setImageBitmap(bitmap);

);





share|improve this answer















Try this one:



 Glide.with(getContext().getApplicationContext())
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(Bitmap bitmap,
Transition<? super Bitmap> transition)

/*
requestLayout()
Call this when something has changed which has
invalidated the layout of this view.
*/
mImageView.requestLayout();
mImageView.getLayoutParams().height = newHeight;
mImageView.getLayoutParams().width = newWidth;


// Set the scale type for ImageView image scaling
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

mImageView.setImageBitmap(bitmap);

);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 4:33

























answered Nov 14 '18 at 4:27









Sultan MahmudSultan Mahmud

23017




23017












  • You are not using the w and h anywhere in your code

    – Vivek Mishra
    Nov 14 '18 at 4:28











  • Please check my latest updated code thanks

    – Sultan Mahmud
    Nov 14 '18 at 4:33











  • Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

    – zq Liu
    Nov 14 '18 at 4:42












  • It is worked...

    – zq Liu
    Nov 14 '18 at 5:01

















  • You are not using the w and h anywhere in your code

    – Vivek Mishra
    Nov 14 '18 at 4:28











  • Please check my latest updated code thanks

    – Sultan Mahmud
    Nov 14 '18 at 4:33











  • Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

    – zq Liu
    Nov 14 '18 at 4:42












  • It is worked...

    – zq Liu
    Nov 14 '18 at 5:01
















You are not using the w and h anywhere in your code

– Vivek Mishra
Nov 14 '18 at 4:28





You are not using the w and h anywhere in your code

– Vivek Mishra
Nov 14 '18 at 4:28













Please check my latest updated code thanks

– Sultan Mahmud
Nov 14 '18 at 4:33





Please check my latest updated code thanks

– Sultan Mahmud
Nov 14 '18 at 4:33













Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

– zq Liu
Nov 14 '18 at 4:42






Thanks a lot, in this way, the size of the imageView would not changed after refreshed, I`m still try.

– zq Liu
Nov 14 '18 at 4:42














It is worked...

– zq Liu
Nov 14 '18 at 5:01





It is worked...

– zq Liu
Nov 14 '18 at 5:01













1














You can set the size of the image views using glide it self.



Glide 
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);





share|improve this answer























  • Thanks, but it`s not in line with the requirements

    – zq Liu
    Nov 14 '18 at 4:35















1














You can set the size of the image views using glide it self.



Glide 
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);





share|improve this answer























  • Thanks, but it`s not in line with the requirements

    – zq Liu
    Nov 14 '18 at 4:35













1












1








1







You can set the size of the image views using glide it self.



Glide 
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);





share|improve this answer













You can set the size of the image views using glide it self.



Glide 
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
.into(imageViewResize);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 4:25









Amit JangidAmit Jangid

1,0171816




1,0171816












  • Thanks, but it`s not in line with the requirements

    – zq Liu
    Nov 14 '18 at 4:35

















  • Thanks, but it`s not in line with the requirements

    – zq Liu
    Nov 14 '18 at 4:35
















Thanks, but it`s not in line with the requirements

– zq Liu
Nov 14 '18 at 4:35





Thanks, but it`s not in line with the requirements

– zq Liu
Nov 14 '18 at 4:35

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293145%2fset-imageviews-size-with-glide-for-each-item-in-recyclerview-the-image-size-is%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto