bitmap 内存溢出
http://dyh7077063.iteye.com/blog/970672? ? bitmap
?
?
?
Bitmap类getPixels()方法中参数stride理解? ?转载
?
?
picture在android.graphics.Picture包中,相对于Drawable和Bitmap而言,Picture对象就小巧的多,它并不存储实际的像素,仅仅记录了每个绘制的过程。
?
Rect: ? ?RectF:
1、精度不一样,Rect是使用int类型作为数值,RectF是使用float类型作为数值
2、两个类型提供的方法也不是完全一致
?
Matrix m = new Matrix(); m.postRotate(30); m.postTranslate(100, 100); Matrix m = new Matrix(); m.postRotate(30); m.postTranslate(100, 100);?
?
?
Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在
?????????Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
???????? set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
???????? post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。例如,要将一个图片旋
???????? 转30度,然后平移到(100,100)的地方,那么可以这样做:
?
?
最近在做Android上的图像处理,在Android上直接对像素操作,居然出现了意想不到的事情。Bitmap类getPixel方法获取的像素值全部是负的,本来应该是黑色的,也就是0的,全部变成了-16777216,很是奇怪。但是仔细研究研究这个16777216又比较特殊,因为16777216=256*256*256,刚好是RGB三种颜色分量最大值的乘积。其实这个值的不精确表示,我们很熟悉,手机广告中宣传屏幕的时候经常会说支持1600万色,诺基亚最喜欢这样宣传了。-16777216的补码十六进制表示就是#FF000000,刚好是加了alpha通道的不透明黑色。查了Android 的文档才知道,Android中颜色由四个分量组成,而我想当然的YY成了RGB三个分量,忽略了A这个分量,默认的A值是255。所以无A通道的图像素最高位总是1,而JAVA中又没有无符号整型,返回一个32位的int型变量,就这样出现了我遇到的各种负数。
?
?
?
?
首先我们会基于手机屏幕大小使用相应尺寸的图片;第二我们会使用一些缓存技术,例如有些图片一旦被用户所下载,就无需下载第二次,主要通过这两种方式来提升用户的体验;另外还有很多其他技术值得尝试,比如在Wifi的网络下,去做一些图片的预加载,甚至还可以改变图片的压缩率,让用户能够在手机上能够更快速的,下的更小,这也是一种方法。这方面的尝试是不断的根据业务来调整。
?
?
有时会发现由于内存不够而导致错误,大都来源于Image太大造成的。
BitmapFactory.Options opts = new BitmapFactory.Options();?
opts.inSampleSize = 3;?
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);?
设置恰当的inSampleSize是解决该问题的关键之一。BitmapFactory.Options提供了另一个成员inJustDecodeBounds。?
BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;?
Bitmap bitmap = BitmapFactory.decodeFile(imageFile, opts);?
设置inJustDecodeBounds为true后,decodeFile并不分配空间,opts.width和opts.height。?
?
?
ava.lang.OutOfMemoryError: bitmap size exceeds VM budget?
基本上要注意几个地方:
1 bitmap如果不用了,回收掉
protected void onDestroy() {??
? ?? ???super.onDestroy();??
? ?? ???if(bmp1 != null){??
? ?? ?? ?? ?bmp1.recycle();??
? ?? ?? ?? ?bmp1 = null;??
? ?? ???}??
? ?? ???if(bmp2 != null){??
? ?? ?? ?? ?bmp2.recycle();??
? ?? ?? ?? ?bmp2 = null;??
? ?? ???}??
? ? }??
2 先算出该bitmap的大小,然后通过调节采样率的方式来规避
BitmapFactory.Options opts = new BitmapFactory.Options();??
? ?? ???opts.inJustDecodeBounds = true;??
? ?? ???BitmapFactory.decodeFile(imageFile, opts);??
? ?? ???opts.inSampleSize = computeSampleSize(opts, minSideLength, maxNumOfPixels);??
? ?? ???opts.inJustDecodeBounds = false;??
? ?? ???try {??
? ?? ?? ?? ?return BitmapFactory.decodeFile(imageFile, opts);??
? ?? ???} catch (OutOfMemoryError err) {??
? ?? ???}??
? ?? ???return null;??
3 在进行文件传输时,最好采用压缩的方式变成byte[]再传输
public static byte[] bitmap2Bytes(Bitmap bm) {??
? ?? ???ByteArrayOutputStream baos = new ByteArrayOutputStream();??
? ?? ???bm.compress(Bitmap.CompressFormat.JPEG, 90, baos);??
? ?? ???return baos.toByteArray();??
? ? } ?
?
?
?
?
?