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);
Related
The first screenshot is with Picasso, the second one with Coil (both in latest versions). Any idea why is this happening?
Picasso: fit().centerInside()
Coil: scale(Scale.FILL).crossfade(true) (I tried with FIT also, same results)
ImageView: adjustViewBounds = true; scaleType = CENTER_INSIDE with MATCH_PARENT width and constant height in pixels.
Coil automatically adjusts to the scale type of the ImageView so you don't need to configure the scale.
Picasso does not, and Picasso's .fit().centerInside() is actually not equivalent to ImageView's CENTER_INSIDE but to FIT_CENTER (it will enlarge the image so that at least one dimension matches the ImageView). There is no equivalent to CENTER_INSIDE with Picasso but these are the closest options:
You can simply remove .fit().centerInside() and let the ImageView scale down the image if it's larger, but if the image is very large it will consume a lot of memory (and may fail to load if larger than the max texture size of the device).
You can use .resize(width, height).centerInside().onlyScaleDown() after measuring the size of the ImageView manually.
If you want Coil to resize the image the same way Picasso does with .fit().centerInside(), then just change the scale type of the ImageView to FIT_CENTER.
I have a recycler view where cells contain an ImageView. I'd like the cells to match_parent width (the screen width), but expand height wise as needed and keep aspect ratio (wrap_content). The problem is that Glide will resize the image to match the placeholder image or (when reusing a cell) match whichever the previous image size was. Is there a way I can tell Glide to get the native image size, every time? I don't want the image to resize, I want the imageview to resize.
Assume I have an image which is larger than an ImageView size (width and height) and the scaleType is set to CENTER or the scaleType is set to MATRIX and the bitmap is translated and scaled. Therefore parts of the image are not visible. How may I calculate the visible part of the bitmap and crop exact that part. I want to crop and store only the visible part.
How may I do that?
Any help will be appreciated.
So you want exactly what the image view shows? I'd enable the drawing cache and grab it from the view. That should be exactly what's on screen.
I am trying to get my images to appear in my image view so that if an image is say 600x400 and my image view is 200x200 that the image is scaled to 300x200 and then the extra width is cropped out so that the image appears as a 200x200 image. I have played with the various ScaleTypes but i can't find one that suits what i want. I want something like a combo of ScaleType.FIT_XY and ScaleType.CENTER_CROP
What you need to do is to scale down the image using the inSampleSize as shown here:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
and then use ScaleType.CENTER_CROP to show the image in the view.
In my code I'm loading image from source into my ImageView. ScaleType of this image view is FIT_CENTER.
My original image is far bigger than on screen. Is it possible to obtain width and height or scale ratio of image on screen?
I can do it manually like based on orientation of image, calculate ratio of area that ImageView is in and ratio of image itself. After ratio I can assume which dimension is fitted (like width or height) and so on...
Is there any other easiest way to do it?
You can scale your image retaining aspect ratio by setting the adjustViewBound to true and scaleType to fitXY.
android:adjustViewBounds="true"
android:scaleType="fitXY"
Use ImageView's
getDrawable().getIntrinsicHeight()
Taken from this related question.
hi set your scale type fitXY
like as this. android:scaleType="fitXY"
There is another way to "auto-scale" your image. You should set the loaded bitmap as background of ImageView control instead of set as source.
Hope this helps.