位图

来源:互联网 发布:单片机控制mp3播放器 编辑:程序博客网 时间:2024/06/06 03:28

位图

Drawable:图形图像 动画 颜色

Bitmap:图像

从资源中获取位图

//从资源中获取位图BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.pic2);Bitmap bitmap = bd.getBitmap();
BitmapFactory

解码图像信息(宽高、像素点、裁剪)可以借助裁剪功能处理大图加载的OOM问题(Out Of Memory)

 BitmapFactory.Options opt = new BitmapFactory.Options(); //先取宽高 opt.inJustDecodeBounds = true; //解析宽高(解析出的bitmap为null) BitmapFactory.decodeFile(path, opt); int oWidth = opt.outWidth; int oHeight = opt.outHeight; int scW = (int) Math.ceil(oWidth / maxWidth); int scH = (int) Math.ceil(oHeight / maxHeight); if (scW > 1 || scH > 1) {      if (scH > scW) {          opt.inSampleSize = scH;          //必须要大于1,必须是整数,最终显示的是1/opt.inSampleSize      } else {          opt.inSampleSize = scW;       }   }  //解码图像,要获取像素点 opt.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(path, opt);

图片保存

FileOutputStream out = new FileOutputStream("/mnt/sdcard/pic_small.png");//格式,质量,输出位置mBitmap.compress(Bitmap.CompressFormat.JPEG,80,out);out.flush();out.close();
0 0