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.
Related
I am trying to cover the imageView with the image without hampering the image ratio. I tried various scaleTypes as mentioned in : https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide but each of them has a space left if we don't want to disturb the ratio, I also tried.
How should I keep the ratio and at the same time fill in the imageView?
Please try with this android:scaleType="fitXY" into ImageView Tag...
I wan't to insert in my SimpleDraweeView with
android:layout_width="50dp"
android:layout_height=50dp
some picture, that may be bigger than this dimensions. I want to resize picture proportionally, so for example picture with source dimension 117x81 should show like 50x34. What should I do to that the picture is not circumcised, but decreased proportionally.
You should set actualImageScaleType in you xml and choose centerInside option. Do not use android:scaleType since it does not have effect on Drawee.
Please take a look at other available options as well.
Reference link: Fresco Drawee Scaling
You can doing that with is code:
simpleDraweeView.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);
I have a bitmap of 128*128 size. I want to make it of size 256*256. But I don't want to stretch the image. Instead, my old image should be placed at the center of the new image and remaining pixels should be filled with a certain color, for example white.
How can I do this?
ImageView have setScaleType(ScaleType type) method.
When ScaleType is http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Try to play with it. Good luck!!
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src
I am a puzzled about using src or background for an ImageView.
I know the former means the content of this ImageView and the latter means the background of the ImageView.
But how to decide which one to use? I don't see the difference.
All views can take a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.
when you use android:background, image will be set to fit on ImageView area(i.e according to width and height of ImageView). It doesn't matter if the image is smaller or larger than ImageView.
when you use android:src, then image will display in its original size. No
automatic scaling, adjustments will happen.
Note: Using android:src, we can get additional benefit of adjustViewBounds property
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image. Pretty much as it implies.
The difference between XML attribute src and background in ImageView:
The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.
In addition: ScaleType only works on SRC; BG can set transparency, for example, in ImageButton, you can use Android:scaletype to control how the image is scaled, sample code as follows:
<ImageView
android:id="#+id/img"
android:layout_height="60dip"
android:layout_width= "60dip"
android:src="#drawable/logo"
android:scaleType="centerInside"
android:layout_centerVertical= "true"/>
Feel free to ask doubt if you get any.