展会信息港展会大全

将图片转换成一个ImageView大小,如何获取这个ImageView的大小
来源:互联网   发布日期:2015-10-03 11:21:18   浏览:2116次  

导读:将图片转换成一个ImageView大小,如何获取这个ImageView的大小从开源项目ImageLoader中摘出:/** * Defines image size for loading at memory (for memory economy) by {@link ImageView......

将图片转换成一个ImageView大小,如何获取这个ImageView的大小

从开源项目ImageLoader中摘出:

/**

* Defines image size for loading at memory (for memory economy) by {@link ImageView} parameters.<br />

* Size computing algorithm:<br />

* 1) Get <b>maxWidth</b> and <b>maxHeight</b>. If both of them are not set then go to step #2.<br />

* 2) Get <b>layout_width</b> and <b>layout_height</b>. If both of them haven't exact value then go to step #3.</br>

* 3) Get device screen dimensions.

*/

private ImageSize getImageSizeScaleTo(ImageView imageView) {

int width = -1;

int height = -1;

// Check maxWidth and maxHeight parameters

try {

Field maxWidthField = ImageView.class.getDeclaredField("mMaxWidth");

Field maxHeightField = ImageView.class.getDeclaredField("mMaxHeight");

maxWidthField.setAccessible(true);

maxHeightField.setAccessible(true);

int maxWidth = (Integer) maxWidthField.get(imageView);

int maxHeight = (Integer) maxHeightField.get(imageView);

if (maxWidth >= 0 && maxWidth < Integer.MAX_VALUE) {

width = maxWidth;

}

if (maxHeight >= 0 && maxHeight < Integer.MAX_VALUE) {

height = maxHeight;

}

} catch (Exception e) {

Log.e(TAG, e.getMessage(), e);

}

if (width < 0 && height < 0) {

// Get layout width and height parameters

LayoutParams params = imageView.getLayoutParams();

width = params.width;

height = params.height;

}

if (width < 0 && height < 0) {

// Get device screen dimensions

width = configuration.maxImageWidthForMemoryCache;

height = configuration.maxImageHeightForMemoryCache;

// Consider device screen orientation

int screenOrientation = imageView.getContext().getResources().getConfiguration().orientation;

if ((screenOrientation == Configuration.ORIENTATION_PORTRAIT && width > height)

|| (screenOrientation == Configuration.ORIENTATION_LANDSCAPE && width < height)) {

int tmp = width;

width = height;

height = tmp;

}

}

return new ImageSize(width, height);

}

赞助本站

人工智能实验室

相关热词: 转换 大小

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港