Android 计算ImageView的大小
来源:互联网 发布:博德之门2增强版 mac 编辑:程序博客网 时间:2024/06/05 13:34
1.获取资源文件中图片的大小,最简单的最直接的方法,就是使用Drawable的getIntrinsicHeight()和getIntrinsicWidth();
2.利用Bitmap来获取其大小,本质上和第一种方式没什么区别:
- /**
- * 计算ImageView的大小(BitmapDrawable)
- *
- * @param resources
- * @param resourceId
- * @return
- */
- public static int[] computeWH(Resources resources, int resourceId) {
- int[] wh = { 0, 0 };
- if (resources == null)
- return wh;
- Bitmap mBitmap = BitmapFactory.decodeResource(resources, resourceId);
- BitmapDrawable bDrawable = new BitmapDrawable(resources, mBitmap);
- wh[0] = bDrawable.getIntrinsicWidth();
- wh[1] = bDrawable.getIntrinsicHeight();
- return wh;
- }
3.可以利用BitmapFactory.Options的outWidth和outHeight两个参数获取其大小,下面给出简单的3种方法供参考:
注意:设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度。
- /**
- * 计算ImageView的大小(decodeFileDescriptor)
- *
- * @param imageFile
- * @return
- */
- public static int[] computeWH_1(String imageFile) {
- int[] wh = { 0, 0 };
- if (imageFile == null || imageFile.length() == 0)
- return wh;
- try {
- FileDescriptor fd = new FileInputStream(imageFile).getFD();
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 1;
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFileDescriptor(fd, null, options);
- if (options.mCancel || options.outWidth == -1
- || options.outHeight == -1) {
- return wh;
- }
- wh[0] = options.outWidth;
- wh[1] = options.outHeight;
- } catch (Exception e) {
- }
- return wh;
- }
- /**
- * 计算ImageView的大小(decodeFile)
- *
- * @param imgFile
- * @return
- */
- public static int[] computeWH_2(String imgFile) {
- int[] wh = { 0, 0 };
- if (imgFile == null || imgFile.length() == 0)
- return wh;
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 1;
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(imgFile, options);
- if (options.mCancel || options.outWidth == -1
- || options.outHeight == -1) {
- return wh;
- }
- wh[0] = options.outWidth;
- wh[1] = options.outHeight;
- return wh;
- }
- /**
- * 计算ImageView的大小(decodeResource)
- *
- * @param resources
- * @param resourceId
- * @return
- */
- public static int[] computeWH_3(Resources resources, int resourceId) {
- int[] wh = { 0, 0 };
- if (resources == null)
- return wh;
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inSampleSize = 1;
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeResource(resources, resourceId, options);
- if (options.mCancel || options.outWidth == -1
- || options.outHeight == -1) {
- return wh;
- }
- wh[0] = options.outWidth;
- wh[1] = options.outHeight;
- return wh;
- }
转载地址:http://chjmars.iteye.com/blog/1156832
0 0
- Android 计算ImageView的大小
- Android 改变ImageView图片的Bitmap大小
- Android 代码改变ImageView的大小
- android imageView 自适应大小
- Android ImageView设置大小
- android Imageview bitmap 占用内存空间的计算
- 设置ImageView的大小
- 设置ImageView的大小
- android 获得ImageView中Image的绘制大小
- Android 中如何动态设定imageview的大小
- Android 获得ImageView中Image的绘制大小
- android 获得ImageView中Image的绘制大小
- 动态改变ImageView的大小
- 修改cell.imageView的大小
- android 控件的大小计算以及自适应屏幕的大小
- Android中动态改变ImageView大小
- Android中动态改变ImageView大小
- android计算大小表
- SV的边界: x264编码参数解释
- linux下 c语言多文件编译
- 游戏《是男人每天十次》
- Hadoop学习笔记(1)——初识Hadoop
- linux 下eclipse+cdt安装 用于c++程序开发
- Android 计算ImageView的大小
- m_hWnd 0xfeeefeee
- 鸟哥lunux入门-
- HDOJ 1166 敌兵布阵
- ios7版本 控件设置圆角
- android开发框架供参考
- 批处理入门介绍
- Wikoi P1200 同余方程
- JAVA里的空白\t\n\r\b\p分别代表什么?