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.
Related
I have been using Picasso from a long time. Today, I am migrating to Glide. In Picasso, I used to use following loading pattern:
Picasso.get()
.load(file)
.resize(targetSize, 0)
.onlyScaleDown()
.placeholder(R.color.default_surface)
.error(R.color.default_surface_error)
.into(imageView)
According to resize(int, int) documentation,
Use 0 as desired dimension to resize keeping aspect ratio
According to onlyScaleDown() documentation,
Only resize an image if the original image size is bigger than the target size specified by resize(int, int)
Here's what I am trying:
Glide.with(imageView)
.log(this, thumbnailUrl?.toString())
.load(thumbnailUrl)
.override(600)
.placeholder(R.color.default_surface)
.error(R.color.default_surface_error)
.into(imageView)
Glide uses a default downsampling strategy when loading images using DownsampleStrategy.CENTER_OUTSIDE. It says that image is upscaled to match the overridden size such that one of the dimension (smallest?) is equal to overridden size. And, following comment:
Scales, maintaining the original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other dimension is greater than or equal to the requested size.
This method will upscale if the requested width and height are greater than the source width and height. To avoid upscaling, use {#link #AT_LEAST}, {#link #AT_MOST}, or {#link #CENTER_INSIDE}.
Options in DownsampleStrategy.java confused me. I don't know which one I should use. I want the large images to scale down to overridden size, and small images to never upscale. How to achieve this in Glide?
I have found an answer in Github Issue #3215 where following is suggested:
Pick a useful DownsampleStrategy, in particular CENTER_INSIDE may be what you're looking for. The default DownsampleStrategy will upscale to maximimize Bitmap re-use, but typically there's an equivalent strategy available that will not upscale.
And, DownsampleStrategy.CENTER_INSIDE fits what I wanted:
Returns the original image if it is smaller than the target, otherwise it will be downscaled maintaining its original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other is less or equal than the requested size. Does not upscale if the requested dimensions are larger than the original dimensions.
I was confused by the documentation for DownsampleStrategy.CENTER_INSIDE in code.
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 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 know about the matrix, its structure and working about image view's scale-type. but,
I can't find the exact meaning of ImageView.ScaleType="MATRIX". By declaring this, what exactly happens while drawing the image view.
When can I use ImageView.ScaleType="MATRIX" ?
How does it differ from FIT_END & FIT_START
I googled about it and also referred the official link but was not able to find the exact answer.
ImageView.ScaleType.MATRIX lets you use a Matrix to scale the image. You can set the Matrix using ImageView.setImageMatrix(matrix). So by declaring the scaleType to MATRIX you are saying that you want to use an explicit Matrix to do that.
You can use imageView.setScaleType(ImageView.ScaleType.MATRIX) whenever you want to customize the way the your image scales, rotates, etc. at your desire.
FIT_END and FIT_START are default types of scale. So, if you use FIT_END for instance, your image will maintain the original aspect ratio and it will align the result of the right and bottom edges of your image view. So basically, the difference is that FIT_END and FIT_START are "presets" while with MATRIX you declare that you want to use your own matrix to scale.
Read the docs for more info
As per my understanding, Use below details for each ImageView's ScaleType attributes
center
Displays the image centered in the view with no scaling.
centerCrop
Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.
centerInside
Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.
fitCenter
Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.
fitStart
Same as fitCenter but aligned to the top left of the view.
fitEnd
Same as fitCenter but aligned to the bottom right of the view.
fitXY
Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.
matrix
Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.
The documentation you linked to provides the answer you are looking for.
Scale using the image matrix when drawing. The image matrix can be set using setImageMatrix(Matrix). From XML, use this syntax: android:scaleType="matrix"
Although you mentioned that you already know what a Matrix is in the context of graphics, I will explain briefly for the sake of other users who come across this question- A matrix can be used to manipulate the canvas when drawing graphics. In the case of an ImageView's matrix, you can use it to translate, flip, rotate, or otherwise move the image around on the screen.
when i can use ImageView.ScaleType="MATRIX"
You can use it whenever you want with an ImageView. You can call setScaleType() in your Java code to use a matrix scale type or you can add the android:scaleType="matrix" attribute in your layout XML.
how it differs from FIT_END & FIT_START
FIT_END and FIT_START both actually use a Matrix to scale your image. Both maintain the original aspect ratio and fit the image entirely within the view, but just align the result differently. End will align the scaled image to the end of the ImageView, whereas start will align the scaled image to the start of the ImageView
ScaleType="MATRIX"
A Matrix is constructed and scaled based on the user’s pinch gesture scale factor and then the ImageView set based on this scaled Matrix.
Please visit here Android ImageView ScaleType
Check the Effect.And https://guides.codepath.com/android/Working-with-the-ImageView
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.