Android,在 ImageView 中更改图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12123661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:48:49  来源:igfitidea点击:

Android, Change Image size in ImageView

androidandroid-layoutimageview

提问by LetsamrIt

For Android, How can I change the image size in an ImageView in a layout?

对于 Android,如何更改布局中 ImageView 中的图像大小?

My image is jpegformat.

我的图像是jpeg格式。

回答by Stefan Beike

<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.

只需更改宽度和高度属性。

回答by Jawad Zeb

You can also set Params using LayoutParams

您还可以使用 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);

回答by Scott Tesler

There are a few ways to change the size of an image in an imageView in XML.

有几种方法可以在 XML 中的 imageView 中更改图像的大小。

  1. 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).

  2. 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.

  3. Simply adjust the weight and height in the imageView (as others have mentioned).

  1. 通过在图形布局中拉动 imageView 的边缘来缩放图像(实际上并不建议这样做,因为图形布局有时会将不需要的属性添加到 XML 中)。

  2. 对 imageView 应用权重(这需要父对象指定一个 weightSum),使图像根据屏幕大小进行缩放。这似乎是衡量您的布局在许多不同大小的屏幕上的外观的最可靠方法。

  3. 只需在 imageView 中调整重量和高度(正如其他人所提到的)。

回答by Swayam

<ImageView
    android:id="@+id/imageViewId"
    android:src="@drawable/icon" 
    android:layout_width="xxdp"
    android:layout_height="yydp"/>

where xxis the width in pixels and yyis the height in pixels.

其中xx是以像素为单位的宽度,yy是以像素为单位的高度。

(give integer values as you desire the size to be)

(根据您希望的大小给出整数值)