I have a layout in which I resize an image by doing a transformation with prescale. After the scale the image has the right size, but the layout container around it cuts its size of. I want the layout container arround it to show to complete image.
I just tried invalidate on the image, the container as well as requestLayout(). Any ideas?
Thanks
How are you transforming the image? If you are setting a matrix on an ImageView, you need to tell the ImageView to adjust its bounds to the size of the content. If you are scaling the image by setting a transformation on the Canvas, you must change the dimensions of the View yourself.
Related
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.
I am setting a imgage in an ImageView. I want to scale down the image size without reducing the imageview size. Please guide me step by step.
Use scaleType parameter for your imageviews and set the value to centerFit or centerCrop. More here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Taken from this link: source
You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.
Is there a way to set the ScaleType of a ImageView as CENTER using Matrix ScaleType?
I'm in need of Center orientation of an image in the ImageView. Problem occurs when I set the size of the ImageView larger than the screen say 1000dp. By doing so my image sticks to the corners.
If I use layout_margins, I'm not able to pinch and zoom the image properly.
Please help me find a solution.
I am trying to apply image on an image view instance...but it doesnt cover it properly...
please advise
here it is my image view code:
android:id="#+id/imageViewVessel"
android:layout_width="fill_parent"
android:scaleType="fitStart"
android:layout_height="170dip"
android:src="#drawable/vessel"
EDIT by kcoppock: Adding code from devaditya's comment below
TableRow rowImage = new TableRow(this);
rowImage.setBackgroundColor(Color.GRAY);
rowImage.setMinimumHeight(150);
rowImage.setGravity(Gravity.CENTER);
rowImage.setMinimumWidth(LayoutParams.FILL_PARENT);
ImageView imgViewVessel=new ImageView(this);
imgViewVessel.setImageResource(R.drawable.vessel);
imgViewVessel.setMinimumHeight(150);
imgViewVessel.setMinimumWidth(LayoutParams.FILL_PARENT);
imgViewVessel.setScaleType(ScaleType.FIT_XY);
rowImage.addView(imgViewVessel);
To expand on Gao's answer, you do need to set a scaleType for your ImageView, but it is unlikely that fitXY is the scaleType that you are looking for. You can find the complete list at the above link, but a few of the most common are:
centerCrop: This will maintain the aspect ratio of the image, filling the frame entirely, but cropping off either the left and right, or top and bottom of the if the aspect ratio of the frame and source image are different.
centerInside: This also maintains the aspect ratio, but the image is scaled to fit entirely within the view, so that the longest edge is the same size as the frame. This can give you a letterbox type of effect if the aspect ratios of the frame and source image are different. fitStart and fitEnd are the same scaling method, but they have different placement of the image (top-left and bottom-right, respectively).
fitXY: This one should only be used if disproportionate scaling does not affect the graphic. In the case of bitmap graphics, this is almost always bad to use. This sets the width of the source image to the width of the view, and the height of the source image to the height of the view, without maintaining the aspect ratio of the source image.
You can set scale type in the layout file : android:scaleType="fitXY" or call setScaleType with fitXY.