您的位置:首页 > 移动开发 > Android开发

Androidx学习笔记(72)--- 加载大图片

2016-02-24 19:56 513 查看

多媒体编程

文本、图片、音频、视频

图片

图片在计算机中的大小图片的总大小=图片的总像素*每个像素占用的大小单色位图:只能表示2种颜色使用两个数字:0和1使用一个长度为1的二进制数字就可以表示了每个像素占用1/8个字节16色位图:能表示16种颜色需要16个数字:0-15,0000-1111使用一个长度为4的二进制数组就可以表示了每个像素占用1/2个字节256色位图:能表示256种颜色需要256个数字:0-255,00000000-11111111使用一个长度为8的二进制数字每个像素占用1个字节24位位图:每个像素占用24位,也就是3个字节,所在叫24位位图R:0-255,需要一个长度为8的二进制数字,占用1个字节G:0-255,需要一个长度为8的二进制数字,占用1个字节B:0-255,需要一个长度为8的二进制数字,占用1个字节

加载大图片

计算机把图片所有像素信息(图片的总像素*每个像素占用的大小)全部解析出来,保存至内存Android保存图片像素信息,是用ARGB保存手机屏幕320*480,总像素:153600图片宽高2400*3200,总像素76800002400(图片宽)/320(手机屏幕宽)=73200(图片高)/480(手机屏幕高)=6使用最大的比例也就是要使用7即可。------

对图片进行缩放

获取屏幕宽高
Displaydp=getWindowManager().getDefaultDisplay();
intscreenWidth=dp.getWidth();
intscreenHeight=dp.getHeight();
获取图片宽高
Optionsopts=newOptions();
//请求图片属性但不申请内存
opts.inJustDecodeBounds=true;
BitmapFactory.decodeFile("sdcard/dog.jpg",opts);
intimageWidth=opts.outWidth;
intimageHeight=opts.outHeight;
图片的宽高除以屏幕宽高,算出宽和高的缩放比例,取较大值作为图片的缩放比例
intscale=1;
intscaleX=imageWidth/screenWidth;
intscaleY=imageHeight/screenHeight;
if(scaleX>=scaleY&&scaleX>1){
scale=scaleX;
}
elseif(scaleY>scaleX&&scaleY>1){
scale=scaleY;
}
按缩放比例加载图片
//设置缩放比例
opts.inSampleSize=scale;
//为图片申请内存
opts.inJustDecodeBounds=false;
Bitmapbm=BitmapFactory.decodeFile("sdcard/dog.jpg",opts);
iv.setImageBitmap(bm);
--------------
publicclassMainActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
publicvoidclick(Viewv){
	//解析图片时需要使用到的参数都封装在这个对象里了
	Optionsopt=newOptions();
	//不为像素申请内存,只获取图片宽高
	opt.inJustDecodeBounds=true;
	BitmapFactory.decodeFile("sdcard/dog.jpg",opt);
	//拿到图片宽高
	intimageWidth=opt.outWidth;
	intimageHeight=opt.outHeight;
	
	Displaydp=getWindowManager().getDefaultDisplay();
	//拿到屏幕宽高
		intscreenWidth=dp.getWidth();
	intscreenHeight=dp.getHeight();
	
	//计算缩放比例
	intscale=1;
	intscaleWidth=imageWidth/screenWidth;
	intscaleHeight=imageHeight/screenHeight;
	if(scaleWidth>=scaleHeight&&scaleWidth>=1){
		scale=scaleWidth;
	}
	elseif(scaleWidth<scaleHeight&&scaleHeight>=1){
		scale=scaleHeight;
	}
	
	//设置缩放比例
	opt.inSampleSize=scale;
	opt.inJustDecodeBounds=false;
	Bitmapbm=BitmapFactory.decodeFile("sdcard/dog.jpg",opt);
	
	ImageViewiv=(ImageView)findViewById(R.id.iv);
	iv.setImageBitmap(bm);
}
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: