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.
Related
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
I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);
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);
}
});
I would like to know more details about what is transformation of image in Picasso what will happen if i use transformation for image in android and what is resize and transformation of image.
Picasso.resize(50, 50)
resizes the image to these dimensions (in pixel). does not respect aspect ratio
The transformations will give you enough tools to change the image according to your needs.
I have spent a very long time researching this issue to no avail. I am attempting to automatically resize an ImageView to match the parent views width (maintaining aspect ratio), while at the same time only allowing the top of the image to be cropped. The left, right, and bottom portions of the image cannot be cut off, so centerCrop does not accomplish what I need.
The top of the image fades into the background color of the app, which is the only part of the image that can be cropped to achieve a proper aspect ratio.
I have also tried using fitXY, however, the aspect ratio is not kept and it looks very bad on some devices.
I have solved this issue. Using the custom ImageView class posted by Mark Martinsson (answer here https://stackoverflow.com/a/17424313/2040690), and setting the ImageView to alignParentBottom, the image will fit perfectly inside the view while maintaining aspect ratio, and the excess portion of the image on the top will simply extend out of the screen boundary, essentially "cropping" it.
Using Glide, I created a transformation to simply make the bitmap square
Glide.with(view.getContext()).load(someUrl))
.transform(new BitmapTransformation(view.getContext()) {
#Override
protected Bitmap transform(BitmapPool pool,
Bitmap toTransform, int outWidth,
int outHeight) {
return Bitmap.createBitmap(toTransform, 0, 0,
toTransform.getWidth(), toTransform.getWidth());
}
#Override
public String getId() {
return "some_unique_id";
}
}).into(imageView);