一。布局
应用界面:
向模拟器里推入一张图片:
二。简单加载图片
一般手机的应用独占内存为16MB,一张bmp图片所占内存KB=宽x高x色位/8bit/1024kb
Win里画图软件制作的PNG格式图片,有时源数据里标示为24位,但实际使用中,可能是按照32位操作的。经由PhotoShop制作的带有透明通道的png图片是32位的;无透明通道的是24位,但被使用中同样按照32位。
直接根据图片的路径加载。
1 2 3 4 5 6 7 |
public void showImg(View v){ //相当消耗内存资源 根据图片的分辨率而定 Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/img30.jpg"); iv_show.setImageBitmap(bitmap); } |
三。压缩加载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
public void showImg(View v) { /** 1.得到屏幕的宽高信息 **/ WindowManager wm = getWindowManager(); int screenWidth = wm.getDefaultDisplay().getWidth(); int screenHeight = wm.getDefaultDisplay().getHeight(); System.out.println("屏幕宽高:" + screenWidth + "-" + screenHeight); /** 2.得到图片的宽高 **/ // 1)图片源数据对象, // which if left unchanged will give the same result from the decoder as if null were passed. BitmapFactory.Options opts = new Options();// 解析位图的源数据(属性,详细信息),不对图片做任何改变 opts.inJustDecodeBounds = true; // 不去解析真实的位图,仅获取位图源数据 // 2)解析图片 Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/img30.jpg", opts); int bitmapWidth = opts.outWidth; int bitmapHeight = opts.outHeight; System.out.println("图片宽高: " + bitmapWidth + "-" + bitmapHeight); /** 3.计算缩放比例 **/ int dx = bitmapWidth / screenWidth; // 得到整型倍数的横向缩放值 int dy = bitmapHeight / screenHeight; // 得到整型倍数的纵向缩放值 int scale = 1; // 1)如果图片的宽大于高,那么就按照横向的比例进行压缩 if (dx > dy && dy > 1) { System.out.println("按照水平方法缩放,缩放比例:" + dx); scale = dx; } // 1)如果图片的宽小于高,那么就按照纵向的比例进行压缩 if (dy > dx && dx > 1) { System.out.println("按照垂直方法缩放,缩放比例:" + dy); scale = dy; } /** 4.缩放加载图片到内存 **/ opts.inSampleSize = scale; // 1)真正的去解析这个位图。 opts.inJustDecodeBounds = false; // 2)解析图片 bitmap = BitmapFactory.decodeFile("/mnt/sdcard/img30.jpg", opts); /** 5.更新控件 **/ iv_show.setImageBitmap(bitmap); } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/331.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设