Android, Change Image size in ImageView - android

For Android, How can I change the image size in an ImageView in a layout?
My image is jpeg format.

<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
Just change width and height attribute.

You can also set Params using LayoutParams
ImageView yourImageView= (ImageView)findViewById(R.id.imageId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
//width and height of your Image ,if it is inside Relative change the LinearLayout to RelativeLayout.
yourImageView.setLayoutParams(layoutParams);

There are a few ways to change the size of an image in an imageView in XML.
Scale the image by pulling around the edges of the imageView in the Graphical Layout (this is not really advised since the Graphical Layout sometimes adds undesired properties into the XML).
Apply a weight to the imageView (this requires the parent object to have specified a weightSum), causing the image to scale according to the screen size. This seems to be the most reliable way to gauge how your layout will look on many different sized screens.
Simply adjust the weight and height in the imageView (as others have mentioned).

<ImageView
android:id="#+id/imageViewId"
android:src="#drawable/icon"
android:layout_width="xxdp"
android:layout_height="yydp"/>
where xx is the width in pixels and yy is the height in pixels.
(give integer values as you desire the size to be)

Related

Setting width and height of ImageView using Aspect Ratio of the original image like whatsapp

I have a listview of the images of different sizes. Images are loaded using glide library.
Currently I have set the height and width of the imageview as wrap_content to get the original feel of the image, but it's not behaving properly.
Therefore I want to set imageview width and height using aspect ratio.
I have used different scaleType, but not getting desired result.
Any help is appreciated
I would suggest you use the DimensionConstraints from ConstraintLayout. You can define an aspect ratio using the app:layout_constraintDimensionRatio attribute, so your ImageView can be something like this:
<ImageView android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,2:1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
So the image view height will always be half the value of the width.

Android: tag rotation in imageview cutting image inside listview

i'm new in android development and i'm trying rotating an image view inside a listview. I have the follow xml for the custom cell:
<RelativeLayout
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true">
<ImageView
android:id="#+id/borderImageView"
android:layout_width="#dimen/photoReceiverWidth"
android:layout_height="#dimen/photoReceiverHeight"
android:scaleType="fitXY"
android:rotation="-10"/>
</RelativeLayout>
When it display, it cut a peace of the image. The view is not calculating the size for the rotation.
I don't want to create a new bitmap because the cost is too high.
android:adjustViewBounds="true"
This is an image view property, so place it within the image view rather than the relative layout. I am not sure if adjustViewBounds works on a fixed size ImageView.
You could change the width and height to wrap_content and use a maxHeight or minHeight property to define some additional bounding rules.
The scale type should also probably be centerInside, but play with these to see what suits.

Setting programmatically the pixel size of a remote ImageVIew?

Let's say that I have a layout that contains
<ImageView
android:id="#+id/my_id"
android:layout_width="123px"
android:layout_height="345px"
android:padding="0dip"
android:scaleType="fitXY" />
which is used in an AppWidget and is set via a RemoteViews. How can I programmatically set the width and height in pixels (overriding the 123 and 345 in the above xml)?
(motivation: this image view shows a dynamic png file that is set at runtime via a file URL. If the layout_width and layout_height above are set to wrap_content, the image is displayed scaled down the the device's density(). Setting the exact width and height in pixels solves the problem but the file size changes and I don't know how to set it programmatically).
You can do it in a different way. Put your ImageView in a LinerLayout and set layout_width and layout_height of ImageView to "fill_parent". Then change LinearLayout's width and height.
I assume you know about app widgets size rules.

Android ImageView will not match_parent width

I have an ImageView that is 1x20px. The image is a .9.png. I am trying to use match_parent but when I run the app on my device, it is not stretched to fill the width of the screen. Here's the code in the XML for the ImageView
<ImageView
android:id="#+id/dividerBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_bar"
android:layout_gravity="center_horizontal"
android:scaleType="centerInside" />
Any ideas on how I can get the ImageView to cover the entire width of the screen?
If it's a 9 patch it needs to be set as the background attribute of the imageview for it to scale properly. Right now it's trying to scale the image as it would any other image using the scaleType tag that you provided.
Try setting
android:background="#drawable/ic_bar"
and remove the src attribute
Additionally, if you are using the imageView only to display the 9-patch you can switch to using a regular View instead of the ImageView. As long as you set the background attribute you will be good to go.

How to create an ImageView that fills the parent height and displays an Image as big as possible?

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.

Categories

Resources