android - 保持位图比例

标签 android android-imageview

我在代码中用位图更改了 imageview 源,但它不保持比例。它使用旧图像的尺寸。

我设置位图;

imageView.setImageBitmap(bitmap);

它拉伸(stretch)位图以适应旧图像资源的尺寸。我想保持位图比例。

最佳答案

查看 imageView ScaleTypes:http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

CENTER Center the image in the view, but perform no scaling.

CENTER_CROP Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).

CENTER_INSIDE Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

FIT_CENTER Scale the image using CENTER.

FIT_END Scale the image using END.

FIT_START Scale the image using START.

FIT_XY Scale the image using FILL.

MATRIX Scale using the image matrix when drawing.

你想要 CENTER_CROP 或 CENTER_INSIDE 之类的东西

关于android - 保持位图比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23885414/

相关文章:

java - 如何同时运行两个 FOR 循环

java - 无法连接到 smtp 主机 :smtp. gmail.com,端口:465

AndroidX 突然导致 NativeScript 构建失败

android - 如何将 Android 小部件中的 ImageView 的高度和宽度动态设置为 "match_parent"?

android - 将自定义图钉放在图像上并获取其坐标

android - QuickBlox 使用 GeoRect 方法获取位置

java.lang.IllegalArgumentException : No suitable parent found from the given view. 请提供有效的 View

android - 以 XML 格式缩放和裁剪图像

android - 当我在 android 中单击 ImageView 时如何突出显示 ImageView 的边框

android - 带有来自 URL 的图像的 Imageview