如何获取ImageView的maxWidth和maxHeight参数?

发布于 2024-12-19 02:43:17 字数 532 浏览 2 评论 0原文

我在 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" />
 ...

并且我想以编程方式获取 ma​​xWidthma​​xHeight 参数值:

...
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟的代价 2024-12-26 02:43:17

ImageView的相关成员变量是mMaxWidthmMaxHeight,两者都被声明为私有。也没有可用的 getter 方法,这给你留下了一个选择:
使用反射来检查字段。

以下是如何做到这一点的示例:

int maxWidth = -1;
int maxHeight = -1;
ImageView iv = (ImageView) findViewById(R.id.imageview);

try {
     Field maxWidthField = ImageView.class.getDeclaredField("mMaxWidth");
     Field maxHeightField = ImageView.class.getDeclaredField("mMaxHeight");
     maxWidthField.setAccessible(true);
     maxHeightField.setAccessible(true);

     maxWidth = (Integer) maxWidthField.get(iv);
     maxHeight = (Integer) maxHeightField.get(iv);
} catch (SecurityException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

其他答案提到了 getWidth() 和 getHeight() ,但它们并不相同。如果在上述检查后运行以下代码片段,您将看到差异(由于设置值或源图像大小,当前大小和最大大小不相同):

Log.d("TEST", "width="+iv.getWidth()+" || height="+iv.getHeight());
Log.d("TEST", "maxWidth="+maxWidth+" || maxHeight="+maxHeight);

The relevant member variables of the ImageView are mMaxWidth and mMaxHeight, 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:

int maxWidth = -1;
int maxHeight = -1;
ImageView iv = (ImageView) findViewById(R.id.imageview);

try {
     Field maxWidthField = ImageView.class.getDeclaredField("mMaxWidth");
     Field maxHeightField = ImageView.class.getDeclaredField("mMaxHeight");
     maxWidthField.setAccessible(true);
     maxHeightField.setAccessible(true);

     maxWidth = (Integer) maxWidthField.get(iv);
     maxHeight = (Integer) maxHeightField.get(iv);
} catch (SecurityException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

The other answers mention getWidth() and getHeight(), 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):

Log.d("TEST", "width="+iv.getWidth()+" || height="+iv.getHeight());
Log.d("TEST", "maxWidth="+maxWidth+" || maxHeight="+maxHeight);
全部不再 2024-12-26 02:43:17

设置宽度和高度周长以填充 xml 中的父级。然后从代码中

imageview.getHeight();

给出

imageview.getWidth();

当前在屏幕上绘制的 ImageView 的最大可用宽度/高度。

set width and height perimeters to fill parent in xml. then from code

imageview.getHeight();

and

imageview.getWidth();

will give you max available width/height of ImageView currently drawn on the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文