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);
}
}
相关文章推荐
- Androidx学习笔记(71)--- 需要支付的应用(调用远程的服务)
- android的进阶findViewById
- 如何解决Android 5.0中出现的警告: Service Intent must be explicit:
- startActivity构成的Activity链中间断掉会如何?
- Android的消息循环机制 Looper Handler类分析
- Android样式的开发:Style篇
- Androidx学习笔记(70)--- 进程优先级
- Androidx学习笔记(69)--- AIDL
- Androidx学习笔记(68)--- 服务分类
- Android技术积累:开发规范
- Androidx学习笔记(67)--- 使用代码注册广播接收者(实现屏幕锁屏和解锁)
- Androidx学习笔记(66)--- 简单音乐播放器原理
- Androidx学习笔记(65)--- 通过绑定服务调用服务里的方法
- Androidx学习笔记(64)---服务两种启动方式
- Androidx学习笔记(63)---电话录(窃)音(听)机(器)
- Android View.post(Runnable )
- Android Init Language
- Android中Broadcast Receiver组件
- Android 快速开发系列 ORMLite 框架最佳实践
- Android ORMLite 框架的入门用法