【问题标题】:setImageResource then getting width and heightsetImageResource 然后获取宽度和高度
【发布时间】:2013-08-15 12:25:52
【问题描述】:

我需要更改图像视图中的图像然后获取宽度。问题是,它给了我最后一张图片的宽度。

headpic.setImageResource(head[x]);
int width = headpic.getWidth();

如果我再次运行此代码,x 是相同的值,它会得到正确的宽度。

【问题讨论】:

  • 使用 .getWidth() 方法时,headpic 可能尚未更新。
  • 是的,这就是我想的,它不在 onCreate() 中,它是一个按钮的方法。
  • 您可能需要 OnPreDraw 事件。在这里查看:stackoverflow.com/questions/4649438/…

标签: java android


【解决方案1】:

我假设 headpic 是您的图像视图。如果是这样,headpic.getWidth(); 不是获取图像视图的宽度而不是图像的宽度吗?

您可以从图像视图中获取可绘制对象并使用.getIntrinsicWidth();

【讨论】:

  • 这非常有效,谢谢。 int width = headpic.getDrawable().getIntrinsicWidth();
猜你喜欢
  • 2017-12-26
  • 1970-01-01
  • 2012-10-10
  • 2021-06-30
  • 2010-11-18
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多