如何获取ImageView的maxWidth和maxHeight参数?
我在 xml 中声明了 ImageView:
...
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="60dip"
android:maxWidth="60dip"
android:scaleType="centerCrop" />
...
并且我想以编程方式获取 maxWidth 和 maxHeight 参数值:
...
ImageView imageView = (ImageView) findViewById(R.id.image);
int maxWidth = ?
int maxHeight = ?
...
我该怎么做?
I have ImageView declared at xml:
...
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="60dip"
android:maxWidth="60dip"
android:scaleType="centerCrop" />
...
And I want to get maxWidth and maxHeight paramater values programmatically:
...
ImageView imageView = (ImageView) findViewById(R.id.image);
int maxWidth = ?
int maxHeight = ?
...
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ImageView的相关成员变量是
mMaxWidth
和mMaxHeight
,两者都被声明为私有。也没有可用的 getter 方法,这给你留下了一个选择:使用反射来检查字段。
以下是如何做到这一点的示例:
其他答案提到了 getWidth() 和 getHeight() ,但它们并不相同。如果在上述检查后运行以下代码片段,您将看到差异(由于设置值或源图像大小,当前大小和最大大小不相同)::
The relevant member variables of the ImageView are
mMaxWidth
andmMaxHeight
, both are declared private. There are also no getter methods available, which leaves you with one option:Use reflection to inspect the fields.
Here is a sample how to do that:
The other answers mention
getWidth()
andgetHeight()
, but it's not the same. If you run the following snippet after the inspection above you will see differences (given that the size and the max size are not identical at the moment due to the set values or the source image size):设置宽度和高度周长以填充 xml 中的父级。然后从代码中
给出
当前在屏幕上绘制的 ImageView 的最大可用宽度/高度。
set width and height perimeters to fill parent in xml. then from code
and
will give you max available width/height of ImageView currently drawn on the screen.