我通读了 documentation关于如何有效地加载位图,我完美地执行了它来创建缩略图。但是,当我想要加载位图而不是硬编码大小(对于缩略图,我使用 75x75 dp)的 ImageView 的宽度和高度是换行内容时,会发生什么情况?
更具体地说: here文档提供了这个功能
decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth,
int reqHeight)
并计算所需宽度和高度的inSampleSize。我虽然获取设备的宽度和高度并减去 ImageView 的填充,但这对我来说似乎有点黑客。还有其他办法吗? 感谢您抽出时间。
最佳答案
直接获取 View 的宽度/高度,而不是根据设备的宽度和高度计算出来。
您可以使用 myImageView.getHeight()
和 myImageView.getWidth();
http://developer.android.com/reference/android/view/View.html
[Width and Height]. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().
然后使用实际的高度/宽度,您可以有效地获取所需的精确分辨率的位图。
关于java - 当尺寸未知时有效加载位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249730/