Android ImageView 固定图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12049626/
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
Android ImageView Fixing Image Size
提问by Shalabh
I have an Android Application, where I have an ImageView
, I need to keep the size constant, there can be various images that I need to put into this ImageView
, of different sizes.
我有一个 Android 应用程序,其中有一个ImageView
,我需要保持大小不变,可以有各种ImageView
不同大小的图像需要放入其中。
I just need to ensure that all the Images must fit into the ImageView
,
the Image should increase in size if it is smaller and should decrease, in case it is bigger.
我只需要确保所有图像都必须适合ImageView
,如果图像较小,则图像应增大,如果较大,则应减小。
Thanks a lot for your time.
非常感谢您的时间。
回答by Youngsik Oh
Fix ImageView's size with dp
or fill_parent
and set android:scaleType
to fitXY
.
使用dp
或修复 ImageView 的大小fill_parent
并设置android:scaleType
为fitXY
。
回答by Lazy Ninja
In your case you need to
在你的情况下,你需要
- Fix the ImageView's size. You need to use dpunit so that it will look the same in all devices.
- Set
android:scaleType
tofitXY
- 修复 ImageView 的大小。您需要使用dp单位,以便它在所有设备中看起来都相同。
- 设置
android:scaleType
为fitXY
Below is an example:
下面是一个例子:
<ImageView
android:id="@+id/photo"
android:layout_width="200dp"
android:layout_height="100dp"
android:src="@drawable/iclauncher"
android:scaleType="fitXY"/>
For more information regarding ImageView scaleType please refer to the developer website.
有关 ImageView scaleType 的更多信息,请参阅开发者网站。
回答by Atul Bhardwaj
Try this
尝试这个
ImageView img
Bitmap bmp;
int width=100;
int height=100;
img=(ImageView)findViewById(R.id.imgView);
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.image);//image is your image
bmp=Bitmap.createScaledBitmap(bmp, width,height, true);
img.setImageBitmap(bmp);
OrIf you want to load complete image size in memory then you can use
或者如果你想在内存中加载完整的图像大小,那么你可以使用
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/image"
android:scaleType="fitXY"/>
回答by Nikhil jassal
You can also try this, suppose if you want to make a back image button and you have "500x500 png" and want it to fit in small button size.
你也可以试试这个,假设你想制作一个背景图像按钮并且你有“500x500 png”并希望它适合小按钮尺寸。
Use dp to fix ImageView's size.
使用 dp 来修复 ImageView 的大小。
add this line of code to your Imageview.
将此行代码添加到您的 Imageview。
android:scaleType="fitXY"
android:scaleType="fitXY"
EXAMPLE:
例子:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageView2"
android:src="@drawable/Backicon"
android:scaleType="fitXY"
/>
回答by Braj
Along with @Youngsik Oh indicated answer, below link is useful for what you want to achieve.
连同@Youngsik Oh 指出的答案,下面的链接对您想要实现的目标很有用。
Large images some times cause OutOfMemory error. In provided link, its taking images from Asset folder. You can modify it according to your need. Hope it helps.
大图像有时会导致 OutOfMemory 错误。在提供的链接中,它从资产文件夹中获取图像。您可以根据需要对其进行修改。希望能帮助到你。