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.
Related
I have attached a image in my android app but when i launch the app it is very small.Please suggest how to increase its size in app so that its clearly visible.
Below is the snippet code of my activity_main.xml :
<ImageView
android:id="#+id/power_image"
android:src="#mipmap/ic_launcher"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_above="#+id/logButton"
android:scaleType="fitCenter"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:visibility="visible"
/>
Change your scaleType to centerCrop and it will be worked. Here is full guide about ScaleType in ImageView in Android. The following is a list of all the most common types of scaleType:
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; 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.
In the imageView you have set dimensions as 200dp x 200dp for the imageView.
Set a background color for the imageView eg yellow. This will let you know the amount of area the imageView covers. In case if the imageView doesn't cover the area desirable to you, increase the height and width of the imageView.
In case if the imageView covers enough space but the image is still small and covers only a portion of the yellow area (bg of imageView). Then you can use scaleType = fitxy in imageView, if you dont want to maintain aspect ratio else go for scaleType = centerCrop or centerInside
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.
What i need is that image should be scaled till it retains the exact proportions after that it should not be scaled further, so that every time width is equal to height of the image view.
As if don't scale properly it will get out of shape, and i need it to be in 1:1 ratio of width and height.
Will
Imageview:scale="fitxy"
work in this case ?
Please help thnk you.
I am working on android ICS
To keep aspect ratio, you should make the image the src rather than background.
Take a look at the ScaleType documentation here http://developer.android.com/reference/android/widget/ImageView.ScaleType.html.
You should try using either android:scaleType="centerInside" or android:scaleType="centerCrop".
Hope that helps!
android:scaleType="fitXY"
Fits the screen size of the device.
Try this adding this
android:layout_height="150dp" //or your designed proportion
android:layout_width="150dp" //or your designed proportion
android:scaleType="fitXY"
What is the value for android:scaleType that will achieve the effect of scale aspect fill? In other words, I need the image to keep it's aspect ratio, but it should fill the canvas. This will obviously lead to part of the image being cut off (if canvas and image aren't similar rectangles) but I want it this way.
Well, you could look at the documentation. CENTER_CROP seems to be what you're looking for:
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
The value you appear to want is 'CENTER_CROP'.
Center_crop is what you're searching for. This property will expand the image until fill the container, preserving in center and ratio.
<ImageView
android:layout_width="100dp"
android:layout_height="400dp"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"/>
I will be displaying an image in an ImageView that will always be much higher and wider than the size of the screen. Is there a simple way to scale the image down so that it fills the ImageView exactly?
Try setting up the scale type in the xml, i believe it scaleXY is the one you are looking for scale types
<ImageView
android:scaleType="scaleXY"
android:src="#drawable/my_image"/>
If you're loading the view in XML, the android:scaleType parameter should do the trick.
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType