
那里是 问题, o bug, 或者他们只是想这样做, 在对象中 ImageView, 在编程 安卓系统. 如果您指定宽度 match_parent 对该对象, 和一个高度 wrap_content, 事件中 图像 你去登记为 来源 有一些 更高的维度 到的 ImageView, 似乎有 问题 一些.
的 问题 在分配给 ImageView 对象的图像时,它发生 较低的维度 对该对象. 在这种情况下, 图像显示与原始尺寸, 和 不包括 ImageView 完全.
在这种情况下, 高达 戏剧 与各种 选项 和 属性 ImageView 对象, 永远不会 你得到你想要的结果. 您可以修改属性 scaleType, adjustViewBounds, layout_height, layout_width, … 并没有什么, 不会有成功.
如何使 ImageView 图像将完全调整到对象的大小
为 获取 目的 图像 的的 ImageView 调整到对象的大小 不败纪录 其比例, 解决方案是创建 新的 Java 类 在我们的项目, 扩展 ImageView 类和 修复 我们的问题.
要做到这一点, 在您的项目, Crea 一个 新类. 我们要调用 ResizableImageView, 和您的代码如下所示 ︰:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.mypackage; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } public ResizableImageView(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Drawable d = getDrawable(); if (d == null) { super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); return; } int imageHeight = d.getIntrinsicHeight(); int imageWidth = d.getIntrinsicWidth(); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); float imageRatio = 0.0F; if (imageHeight > 0) { imageRatio = imageWidth / imageHeight; } float sizeRatio = 0.0F; if (heightSize > 0) { sizeRatio = widthSize / heightSize; } int width; int height; if (imageRatio >= sizeRatio) { width = widthSize; height = width * imageHeight / imageWidth; } else { height = heightSize; width = height * imageWidth / imageHeight; } setMeasuredDimension(width, height); } } |
一次 创建 e 实施 类 ResizableImageView, 我们现在做的就是 修改 的 XML 的的 布局 查看在哪里是 ImageView 我们要调整图像大小的位置.
让我们假设。, 以前 若要更改, ImageView 来更改 XML 如下:
1 2 3 4 5 6 | <ImageView android:id="@+id/miImagen" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="https://cdn1.professor-falken.com/@drawable/imagen" /> |
嗯,只是有的 更改 这类对象的类 新增功能 创建的类, 通过以下方式:
1 2 3 4 5 6 | <com.mypackage.ResizableImageView android:id="@+id/miImagen" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="https://cdn1.professor-falken.com/@drawable/imagen" /> |
与此 类 和的变化 XML 代码, 我们会实现该对象中包含的图像 ImageView 将调整大小, 维持其相称, 即使在哪些图像到一个维度小于容器的情况下.
