Fit listview cell to content after Downloading an image [duplicate] - android

I have a vertical LinearLayout where one of the items is an ImageView loaded using Picasso. I need to rise the image's width to the full device width, and to display the center part of the image cropped by a fixed height (150dp). I currently have the following code:
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.centerInside()
.into(imageView);
Which values should I put into screenWidth and imageHeight (=150dp)?

You are looking for:
.fit().centerCrop()
What these mean:
fit - wait until the ImageView has been measured and resize the image to exactly match its size.
centerCrop - scale the image honoring the aspect ratio until it fills the size. Crop either the top and bottom or left and right so it matches the size exactly.

In some case the fit() is useless. Before you must wait for the width and height measurement to end. So you can use globallayoutlistener. for example;
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.fit
.centerInside()
.into(imageView);
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});

Related

How to choose values for resize() value in Picasso

I have added an Image View, and set these values.
<ImageView
android:id="#ivNewsHeader
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop" />
I get images from server, and they are very high res images, if i add them as is, i end up getting OutOfMemoryError.
So i learnt that picasso offers a great way to tackle this. I used following, and this scale down any image which is higher than the mentioned dimensions.
Picasso.with(getApplicationContext()).load(imageURL).
resize(180, 130).
onlyScaleDown()
.into(ivNews);
Now when i choose these resize(180, 130), the performance is very good and i don't get any OutOfMemoryError, but the image quality is very poor.
So my question is how to choose resize number, and what equation should is use to put correct width-height numbers in resize() method to get the perfect image without compromising the quality and performance.
My imageView height is 250dp and width is match_parent
Specify scale value based on device size
Device width in pixel
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
Convert DP to Pixel
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
Now use these two function
Picasso.with(getApplicationContext()).load(imageURL).
resize(getScreenWidth(), dpToPx(250)).
onlyScaleDown()
.into(ivNews);
I think fit() is what you need, because will crop your image to ImageView size on screen. In this case, you don't need do to any more calculations.
Picasso.with(getApplicationContext())
.load(imageURL)
.fit()
.centerInside()
.into(imageView);
Try using fit() which is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView.
Quoting from this article

android glide into(Target) OOM

I am using Glide frame to load a picture, I want to set the height and the width of it, and I know what is the width I want, but how can I set the height of it.
what I have tried is blow,and I got an error of OOM. waiting for help, thanks.
if you want change size
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
Reference
Using override method of glide you can set the dimension of image in pixels.
Glide
.with(context)
.load(url)
.override(50 , 50)
.into(imageview);

Draw image view height and width before loading from url

I am getting image's height and width from API. I'm using Picasso to load images from url. My query is once we would get the API response how can we draw image as per it's height and width before actual image loading by Picasso. I have set image's height and width to Picasso in resize() method.
But Picasso loads images directly once image successfully downloaded. Here the issue is that I want draw image's height and width in preloading state.
/*
* set image view's height and width from API
*/
int imageViewHeight = getHeight();
int imageViewWidth = getWidth();
// picasso to laod an image from url
Picasso.with(view.getContext())
.load(url).placeholder(getGradientDrawable())
.resize(imageViewWidth, imageViewHeight)
.centerInside()
.into(imageView);
// gradient for placeholder
static GradientDrawable getGradientDrawable() {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setColor(Color.GRAY);
return gradientDrawable;
}
Any help appreciated.
I'm not sure if I understood you right but if you want a place holder for your image while Picasso doing a background download, you can use placeholder() function like below:
Picasso.with(context).load(url).placeholder(drawable).into(imageview);
you can create a drawable with your desired width and height or use a resource ID
This helped do it for me, setting the imageview max and min height and width to the image's dimensions before calling the Picasso library to load the URL. It might help someone else who is looking at this.
imageView.setMinimumWidth(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMinimumHeight(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMaxWidth(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMaxHeight(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));

maintaing aspect ratio of image when loading image using picasso?

i am loading images using picasso. i want to resize the image maintaining the aspect ratio. By far, i have found that when i use fit() method. it will resize the image and fill the image view( full image will show but not maintaining aspect ratio), and if i use centercrop() it will crop the centre part of the image to meet the imageview size( but in this case the aspect ratio remains same). i want both to happen. in my XML the height of the image view is 200dp
Picasso.with(mcontext).setIndicatorsEnabled(true);
Picasso.with(mcontext).load("http://192.168.0.14:1337/show_all_offers/" + current.offercover).fit().centerCrop()
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.offercover, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Log.d("cover", "success");
}
#Override
public void onError() {
Log.d("cover", "failure");
}
});
I am using the below code to maintain the aspect ratio. The image will get cut from the sides.
Picasso.with(ctx).load(imageURL).fit().centerCrop().placeholder(R.mipmap.fallback_image).error(R.mipmap.fallback_image).into(imageView);
This might help you.

Resize image with Picasso library

I am using Picasso to show image from url. My question is how can I resize the image to a variable heigth and width without crop the image? Currently when I resize the image without centerCrop() it stretches the image. This is what I have:
// Get current display dimensions
DisplayMetrics metrics = getResources().getDisplayMetrics();
width = metrics.widthPixels;
height = metrics.heightPixels / 2;
// Set image from URL in ImageView
Picasso.with(getApplicationContext())
.load(path)
.placeholder(R.drawable.loading).resize(width, height)
.centerCrop().into(image);
You need to use a method that will preserve the aspect ratio of your image. I would try
Picasso.with(getApplicationContext())
.load(path)
.placeholder(R.drawable.loading)
.resize(width, height)
.centerInside()
.into(image);
first, but it's not really clear to me what your exact needs are from your code. Do you want there to be padding around the image?

Categories

Resources