Set ImageView`s size with Glide for each item in RecyclerView, the image size is changing
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
add a comment |
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
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
add a comment |
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
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
android android-recyclerview glide
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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);
);
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
add a comment |
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);
Thanks, but it`s not in line with the requirements
– zq Liu
Nov 14 '18 at 4:35
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
);
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
add a comment |
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);
);
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
add a comment |
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);
);
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);
);
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
add a comment |
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
add a comment |
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);
Thanks, but it`s not in line with the requirements
– zq Liu
Nov 14 '18 at 4:35
add a comment |
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);
Thanks, but it`s not in line with the requirements
– zq Liu
Nov 14 '18 at 4:35
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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