> 图片大小 = 图片的总像素 * 每个像素占用的大小 * 单色图:每个像素占用1/8个字节 * 16色图:每个像素占用1/2个字节 * 256色图:每个像素占用1个字节 * 24位图:每个像素占用3个字节 --- #加载大图片到内存(掌握) >Android系统以ARGB表示每个像素,所以每个像素占用4个字节,很容易内存溢出 ##对图片进行缩放(掌握) *第一步:获取屏幕宽高Display dp = getWindowManager().getDefaultDisplay(); int screenWidth = dp.getWidth(); int screenHeight = dp.getHeight(); *第二步:获取图片宽高Options opts = new Options(); //请求图片属性但不申请内存 opts.inJustDecodeBounds = true; BitmapFactory.decodeFile("sdcard/dog.jpg", opts); int imageWidth = opts.outWidth; int imageHeight = opts.outHeight; * 第三步:图片的宽高除以屏幕宽高,算出宽和高的缩放比例,取较大值作为图片的缩放比例int scale = 1; int scaleX = imageWidth / screenWidth; int scaleY = imageHeight / screenHeight; if(scaleX >= scaleY && scaleX > 1){scale = scaleX; } else if(scaleY > scaleX && scaleY > 1){scale = scaleY; } *第四步:按缩放比例加载图片//设置缩放比例 opts.inSampleSize = scale; //为图片申请内存 opts.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opts); iv.setImageBitmap(bm); 部分代码:Options opts = new Options();
//只请求图片宽高,不解析图片像素
opts.inJustDecodeBounds = true;
//返回null,获取图片宽高,保存在opts对象中
BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
//获取图片宽高
int imageWidth = opts.outWidth;
int imageHeight = opts.outHeight;
//获取屏幕宽高
Display dp = getWindowManager().getDefaultDisplay();
int screenWidth = dp.getWidth();
int screenHeight = dp.getHeight();
//计算缩放比例
int scale = 1;
int scaleWidth = imageWidth / screenWidth;
int scaleHeight = imageHeight / screenHeight;
//判断取哪个比例
if(scaleWidth >= scaleHeight && scaleWidth > 1){
scale = scaleWidth;
}
else if(scaleWidth < scaleHeight && scaleHeight > 1){
scale = scaleHeight;
}
//设置缩小比例
opts.inSampleSize = scale;
opts.inJustDecodeBounds = false;
//获取缩小后的图片的像素信息
Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bm);
700字范文,内容丰富有趣,生活中的好帮手!