In my app i let the user take a picture with a camera and then use that image as the source for an imageButton. I use android:scaleType fitCenter to scale the image down but no matter what I set for the max button height the button still doubles in size. Is there a way to constrain the size of the button after i put the picture in it?
try setting the width and height of the button to a specific dp rather than using a wrap_content
<Button
android:layout_width="10dp"
android:layout_height="10dp" />
something like that
Related
I am trying to scale an image inside an ImageView, but I want the image to get cropped if it goes outside the view boundaries. This is what it looks like. The image just gets rendered outside the View boundaries. I have tried all the different forms of android:scaleType and android:adjustViewBounds but none of them work to keep the image inside those boundaries. Basically what I want to have as an end result is an image that can zoom in and zoom out but when you zoom in, the image gets cropped to fit in the space. Maybe this is one of those things that is just not possible?
android:layout_width="wrap_content"
android:layout_height="wrap_content"
means that the view will adjust its size in acordance with the content. If you want the image to crop, specify those, e.g.
android:layout_width="40dp"
android:layout_height="40dp"
scaleX and scaleY is scalling the image. Removing both will solve your problem. Also give it a fixed height and width.
Edit these lines
android:layout_width="50dp"
android:layout_height="50dp"
And remove these lines
android:scaleX="2"
android:scaleY="2"
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 use two ways to set image for imageView, by xml and by code setBackgroundResource
eg. img.setBackgroundResource(R.drawable.task_learn_lock);
OR xml
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"
android:src="#drawable/task_learn_lock"
android:contentDescription="#string/desc"
android:scaleType="centerInside" />
But the result is different, the image created by code is streched vertically. Anyone know why? I need to set a new image when the app is running. I tried to post a screen shot, but i do not have enough reputation to do so. Thanks.
Most likely it is because the default content (defined by xml) of the imageView's are different for these two approaches.
In your second method that sets the background image programmatically, I guess you should have set a default drawable image that is different from #drawable/task_learn_lock in size and aspect ratio.
Since the layout_height and layout_width of your ImageView are defined as wrap_content, the aspect ratio of your ImageView will be the same as your default src image. Therefore when you set the background image by img.setBackgroundResource(R.drawable.task_learn_lock), the image will be stretched to fit the current ImageView's dimension.
It appears in your case that you want to set the content to be #drawable/task_learn_lock but not the background of your ImageView, you should use setImageResource instead of setBackgroundResource.
I have downloaded a Bitmap from a WebService, and set it to an ImageView. The ImageView has a set height of 120dip and a width set to FILL_PARENT, so it fills the complete width of the screen. Now i want to stretch the Bitmap to fit the width of the ImageView, but the if the height of the Bitmap is greater then the height of the ImageView, i want the remainder of the image to flow off the top and bottom of the ImageView. So in way, like i have a frame infront of the image and the only portion of the image visible is the contents of the frame. How can i do this ?
See Android Developers on ImageView ScaleType
You are interested in the scale_type enum. Use CENTER_CROP.
Here is complete example:
<ImageView android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/eureka"
android:scaleType="centerCrop">
</ImageView>
Here is also a cool visualization of all possible options for ImageView.
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.