I have image in drawable, which needs to work as the background. It needs to stretch to fill the screen..
I know how to stretch the image full screen using java code.. but in XML itselt how can do this.
android:scaleType="fitXY" will stretch the image to fit the full size of the ImageView
Try with this property:
android:scaleType="fitXY"
Here you can find other constant values for resizing.
Try to implement your ImageView like this and change the ic_launcher to your own image name which you are trying to add.
<ImageView
android:id="#+id/mImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
Hope this will be helpful. Thanks
I prefer to use scaleType="centerCrop". This way the image isn't stretched out of its original proportions.
Related
Please don't mark this a duplicate, as I am unable to find the solution.
Hi, I am trying to add src image with some background color
Here is what I am doing
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:background="#drawable/bg_layout_corners"
android:backgroundTint="#color/color_fbfbfa"
android:gravity="center">
<ImageView
android:id="#+id/iv_dashboard_niggle"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:scaleType="centerInside"
android:background="#color/colorAccent"
android:src="#drawable/niggle2_3_3_3" />
</RelativeLayout>
and here is the output I am getting
When I try to decrease the width or height of imageView, the actual image is placing at the center and some border is visible.
I also checked for scaleType="center", this is also not working
Please help me.
Hope this solves your problem.
iv_dashboard_niggle.setScaleType(ImageView.ScaleType.fitXY);
Set wrap_content as height for your relative layout.
Then the space will go.
The image that you wanted to set in a image view needed to be in the same aspect ratio to fit exactly without applying scaleType...
for example
If you want to set a square image that have height and width ratio 1:1 then ImageView ratio needs to be 1:1
Well but also we have ScaleType if images not according ratio then we can set scale type to match the size of image view by resizing our image
In your case it's completely depend on what you want to show.
If you don't want background border then simply set background color to transparent by using
android:background="#android:color/transparent"
Best way is define ImageView size according aspect ratio if you don't want to use scale type
I am trying to make a simple LinearLayout composed of an ImageView and TextView.
The ImageView should scale to match the LinearLayout height and not lose proportions while doing so.
This is the xml I currently have.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="#CCCCCC"
android:scaleType="fitCenter"
android:src="#drawable/strip" />
<TextView
android:id="#+id/logoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="what an awesome text"
android:textSize="18sp" />
</LinearLayout>
Using the above xml, the result is that the ImageView height will indeed match the LinearLayout height and it's width will be the same as the src image but the rendered image will scale properly and center, but this leaves the ImageView itself filling about 90% of the Layout's width as it's the src image width, leaving no space for the TextView.
I just would like to scale the ImageView to match the parent's height and it's width should be just as much needed to scale it proportionately.
Add a linear layout on the image view. Just the image view and nothing else. :) hope it helps
EDIT 1: As many of the other answers mentioned Using the layout_weight property will also help resolve this issue.
add weight 1 on imageview so there will be space for the textview
As I mentioned in the comment under your answer post where you cite KISHORE_ZE, it seems to be helpful only with an image of particular size, and on a particular screen density and resolution. So while it looks acceptable on your current device, it might be a mess on a different one. I suggest that you figure out what part of the layout you want to use for the image and use android:weight attributes for your ImageView and TextView to achieve wanted result.
As #KISHORE_ZE said, this solves the problem.
"Put the image view in a linear layout. Just the image view. Nothing else. "
this visual guide is a lot helpful.
link
in your image view add:
android:scaleType="fitCenter"
or
android:scaleType="fitXY"
whichever suites you.
I made a listView images. My goal that each item of my listView have the same height of 100dp. I encounter a problem that my imageView is resized automatically, but does not take the entire space of the parent.
Here is my current result and objectif result :
Does anyone know the option to add to make crop center automatically?
I am not sure if I understood correctly. It must be either of the two, that you want, I guess.
In your image view set the attribute
android:scaleType="fitXY"
to fit the ImageView completely.
You can choose
android:scaleType="centerCrop"
to crop Center.
i know it's late but maybe it would be helpful for someone coming on this question page by using android:scaleType="centerCrop" image is centered but it is cropped from all sides so it would be better to use this property instead that is fitCenter and with this use another property too to maintain the aspect ratio
android:adjustViewBounds="true"
android:scaleType="fitCenter"
Are you aware of the cropping options on ImageView?
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You will just apply these as an attribute in your XML:
like:
android:scaleType="centerCrop"
in java: imageView.setScaleType(ScaleType.FIT_XY);
I have an Android Activity with an ImageView.
I put the ScaleType of the imageview on CENTER_CROP, so the image itself would not stretch.
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:src="#drawable/capture" />
The image that's loaded at first does what I expect, it's cropped to the center, filling the whole ImageView.
Now, if I set the image of the ImageView programatically,
imgView.setImageBitmap(b)
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
The picture gets displayed, but it's completely stretched out to fill the ImageView...
AdjustViewBounds has no effect what so ever, nor does anything else.
The difference between the working XML and the not-working java code is that in XML I'm putting the image as a resource, and the code is putting a Drawable in the ImageView from a web-service...
Does anyone have an idea to the solution?
I am not sure but android:scaleType="fitXY" or android:scaleType="fitCenter" may be the solution of your problem.
I solved this by using the original image from my camera-intent. For some reason, all thumbnail bitmaps were stretched out and in landscape mode.
I have an ImageView that is defined in the following way:
<ImageView
android:id="#+id/cover_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_above="#id/divider"
android:adjustViewBounds="true"
android:src="#drawable/image_placeholder"
android:scaleType="fitStart"/>
Now after downloading a new bitmap I change the drawable. The image now appears in the top left corner of the ImageView. Is there a way to have the image fill up the whole height that is possible and then adjust the width of the view to enable scaling the image without changing the ascpect ratio?
The image fills up all the space on a standard screen but on a WVGA Resolution the image takes only about half of the actual height of the ImageView.
If I'm understanding you correctly, what you need to use is the centerCrop scaleType. fitStart scales the image proportionally, but neither the width nor height will exceed the size of the view, and the image will, as you said, have a top|left gravity.
Using centerCrop scales the image proportionally, but causes the shortest edge of the image to match the size of the view, and if there is additional data on the long side that does not fit, it is simply cropped off. The gravity is, of course, center. The below worked for me:
<ImageView
android:src="#drawable/image_placeholder"
android:id="#+id/cover_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
/>
You can change scale type to fitXY via call to
imageView.setScaleType(ScaleType.FIT_XY);
simply do it in xml like
android:scaleType="fitXY"
Basically the answer here is that there is no predefined value for what you are trying to achieve. The solution is to create a Matrix that fits to your needs and call setImageMatrix(matrix) on your ImageView.