[代码片段] 【转】Android以最省内存的方式读取本地资源的
来源:互联网 发布:淘宝卖家采集器 编辑:程序博客网 时间:2024/06/05 07:48
1、获取本地图片并指定高度和宽度
代码片段,双击复制
2、以最省内存的方式读取本地资源的图片
代码片段,双击复制
3、以最省内存的方式读取本地资源的图片 或者SDCard中的图片
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 获取本地图片并指定高度和宽度
*/
public
static
Bitmap getNativeImage(String imagePath) {
BitmapFactory.Options options =
new
BitmapFactory.Options();
options.inJustDecodeBounds =
true
;
// 获取这个图片的宽和高
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath, options);
// 此时返回myBitmap为空
// 计算缩放比
int
be = (
int
) (options.outHeight / (
float
)
200
);
int
ys = options.outHeight %
200
;
// 求余数
float
fe = ys / (
float
)
200
;
if
(fe >=
0.5
)
be = be +
1
;
if
(be <=
0
)
be =
1
;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
options.inJustDecodeBounds =
false
;
myBitmap = BitmapFactory.decodeFile(imagePath, options);
return
myBitmap;
}
2、以最省内存的方式读取本地资源的图片
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
/**
* 以最省内存的方式读取本地资源的图片
*
* @param context
* @param resId
* @return
*/
public
static
Bitmap readBitMap(Context context,
int
resId) {
BitmapFactory.Options opt =
new
BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable =
true
;
opt.inInputShareable =
true
;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return
BitmapFactory.decodeStream(is,
null
, opt);
}
3、以最省内存的方式读取本地资源的图片 或者SDCard中的图片
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
/**
* 以最省内存的方式读取本地资源的图片 或者SDCard中的图片
*
* @param imagePath
* 图片在SDCard中的路径
* @return
*/
public
static
Bitmap getSDCardImg(String imagePath) {
BitmapFactory.Options opt =
new
BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable =
true
;
opt.inInputShareable =
true
;
// 获取资源图片
return
BitmapFactory.decodeFile(imagePath, opt);
}
- [代码片段] 【转】Android以最省内存的方式读取本地资源的
- Android以最省内存的方式读取本地资源的方法
- Android以最省内存的方式读取本地资源的
- Android以最省内存的方式读取本地资源的方法
- Android以最省内存的方式读取本地资源的方法
- Android以最省内存的方式读取本地资源的方法
- 以最省内存的方式读取本地资源的图片
- 以最省内存的方式读取本地资源的图片
- 在不改变大小的情况下,如何以最省内存的方式读取本地资源的图片
- Bitmap以最省内存的方式获取本地资源,转换drawable到bitmap
- Bitmap——在不改变大小的情况下,如何以最省内存的方式读取本地资源的图片
- [Android]最省内存的ViewPager添加小圆点指示器
- [Android]最省内存的ViewPager添加小圆点指示器
- 以最省内存的方式把大图片加载到内存及获取Exif信息和获取屏幕高度和宽度的新方法
- paip.最省内存的浏览器评测 cah
- 交通灯实例的实现方式一 省内存版
- android 图片最省内存压缩方法
- android独有的省内存的轻量级容器类
- Ubuntu-10.04.1下DirectFB与样例的编译安装、运行体验
- 块编程主题(三)
- 如何让IOS应用从容地崩溃
- nginx 学习笔录(支持thinkphp)
- Codeforces Round #174 (Div. 2)
- [代码片段] 【转】Android以最省内存的方式读取本地资源的
- 1.2 实验练习
- vsftpd不同用户访问不同目录的解决办法
- Portal-Basic Java Web 应用开发框架 v3.0.1 正式发布(源码、示例及文档)
- for_each使用方法详解[转]
- (转)解析bind1st和bind2nd的使用
- Druapl_让中英文切换器显示在Logo图片上
- 常用的一些动作 色彩渐变 转动、颤动 跳动 闪烁 放大 缩小
- 拥塞避免算法