本文的目的是介绍android的bitmap问题的详细情况,特别关注android中bitmap的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解android的bit
本文的目的是介绍android 的 bitmap 问题的详细情况,特别关注android中bitmap的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解android 的 bitmap 问题的机会,同时也不会遗漏关于Android - 避免出现 bitmap 内存限制 OUT OF MEMORY 的一种方法、Android Bitmap、android Bitmap getByteCount和getRowBytes、Android Bitmap 与 DrawAble 与 byte [] 与 InputStream 之间的转换工具的知识。
本文目录一览:- android 的 bitmap 问题(android中bitmap)
- Android - 避免出现 bitmap 内存限制 OUT OF MEMORY 的一种方法
- Android Bitmap
- android Bitmap getByteCount和getRowBytes
- Android Bitmap 与 DrawAble 与 byte [] 与 InputStream 之间的转换工具
android 的 bitmap 问题(android中bitmap)
我穿入的 srcPath 是有效的图片地址 但是为什么最后返回的 bitmap 是 null 但是我 debug 的模式下 bitmap 确是有值的 在线急等 求好人帮忙
Android - 避免出现 bitmap 内存限制 OUT OF MEMORY 的一种方法
这里,我使用 Gallery 来举例,在模拟器中,不会出现 OOM 错误,但是,一旦把程序运行到真机里,图片文件一多,必然会出现 OOM, 我们通过做一些额外的处理来避免。
1. 创建一个图片缓存对象 HashMap dataCache,integer 对应 Adapter 中的位置 position,我们只用缓存处在显示中的图片,对于之外的位置,如果 dataCache 中有对应的图片,我们需要进行回收内存。在这个例子中,Adapter 对象的 getView 方法首先判断该位置是否有缓存的 bitmap,如果没有,则解码图片(bitmapDecoder.getPhotoItem,BitmapDecoder 类见后面)并返回 bitmap 对象,设置 dataCache 在该位置上的 bitmap 缓存以便之后使用;若是该位置存在缓存,则直接取出来使用,避免了再一次调用底层的解码图像需要的内存开销。有时为了提高 Gallery 的更新速度,我们还可以预存储一些位置上的 bitmap,比如存储显示区域位置外向上 3 个向下 3 个位置的 bitmap,这样上或下滚动 Gallery 时可以加快 getView 的获取。
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.photo_item, null);
holder = new ViewHolder();
holder.photo = (ImageView) convertView.findViewById(R.id.photo_item_image);
holder.photoTitle = (TextView) convertView.findViewById(R.id.photo_item_title);
holder.photoDate = (TextView) convertView.findViewById(R.id.photo_item_date);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
cursor.moveToPosition(position);
Bitmap current = dateCache.get(position);
if(current != null){
//如果缓存中已解码该图片,则直接返回缓存中的图片
holder.photo.setImageBitmap(current);
}else {
current = bitmapDecoder.getPhotoItem(cursor.getString(1), 2) ;
holder.photo.setImageBitmap(current);
dateCache.put(position, current);
}
holder.photoTitle.setText(cursor.getString(2));
holder.photoDate.setText(cursor.getString(4));
return convertView;
}
}
BitmapDecoder.class
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
public class BitmapDecoder {
private static final String TAG = "BitmapDecoder";
private Context context;
public BitmapDecoder(Context context) {
this.context = context;
}
public Bitmap getPhotoItem(String filepath,int size) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=size;
Bitmap bitmap = BitmapFactory.decodeFile(filepath,options);
//预先缩放,避免实时缩放,可以提高更新率
bitmap=Bitmap.createScaledBitmap(bitmap, 180, 251, true);
return bitmap;
}
}
2. 由于 Gallery 控件的特点,总有一个 item 处于当前选择状态,我们利用此时进行 dataCache 中额外不用的 bitmap 的清理,来释放内存。
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
releaseBitmap();
Log.v(TAG, "select id:"+ id);
}
private void releaseBitmap(){
//在这,我们分别预存储了第一个和最后一个可见位置之外的3个位置的bitmap
//即dataCache中始终只缓存了(M=6+Gallery当前可见view的个数)M个bitmap
int start = mGallery.getFirstVisiblePosition()-3;
int end = mGallery.getLastVisiblePosition()+3;
Log.v(TAG, "start:"+ start);
Log.v(TAG, "end:"+ end);
//释放position<start之外的bitmap资源
Bitmap delBitmap;
for(int del=0;del<start;del++){
delBitmap = dateCache.get(del);
if(delBitmap != null){
//如果非空则表示有缓存的bitmap,需要清理
Log.v(TAG, "release position:"+ del);
//从缓存中移除该del->bitmap的映射
dateCache.remove(del);
delBitmap.recycle();
}
}
freeBitmapFromIndex(end);
}
/**
* 从某一位置开始释放bitmap资源
* @param index
*/
private void freeBitmapFromIndex(int end) {
//释放之外的bitmap资源
Bitmap delBitmap;
for(int del =end+1;del<dateCache.size();del++){
delBitmap = dateCache.get(del);
if(delBitmap != null){
dateCache.remove(del);
delBitmap.recycle();
Log.v(TAG, "release position:"+ del);
}
}
}
经过这些额外的操作,有效的避免了 OOM 的问题。
Android Bitmap
一 图片表示原理
图片是由每个像素点来组成 像素点就是小方块
图片的大小等于 宽 * 高 * 每个像素点的大小
二 加载图片 OOM 异常
解决办法
其中 big.jpg 是一张 21.2MB 的高清图
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.load).setOnClickListener(this);
mImageView = findViewById(R.id.image);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.load:
load();
break;
}
}
private void load() {
try {
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true; //只会解析图片的大小 不会加载图片的内容
BitmapFactory.decodeStream(getAssets().open("big.jpg"), null, option);
// 获取图片的宽高
int width = option.outWidth;
int height = option.outHeight;
// 获取屏幕的宽高
int screenWidth = getScreenWidth();
int screenHeight = getScreenHeight();
// 把图片的宽高和屏幕的宽高进行对比
int scaleX = width / screenWidth;
int scaleY = height / screenHeight;
int scale = scaleX > scaleY ? scaleX : scaleY;
option.inJustDecodeBounds = false; //加载图片的内容
// 如果设置为>1 请求解码器对原始数据进行子采样 例如inSampleSize==4返回图像的宽度/高度是原始图像的1/4
// 任何值<=1都与1相同
option.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("big.jpg"), null, option);
int byteCount = bitmap.getByteCount();
Log.i("HUANG", "byteCount=" + byteCount);
mImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
/** 得到设备屏幕的宽度 (像素) **/
private int getScreenWidth() {
return getResources().getDisplayMetrics().widthPixels;
}
/** 得到设备屏幕的高度 (像素) **/
private int getScreenHeight() {
return getResources().getDisplayMetrics().heightPixels;
}
}
三 图片处理原理
Android 里面所有的显示效果都是绘制出来的
用 Android 封装好的绘图类去绘制图片
Canvas: 画布
Paint: 画笔
Matrix: 图形矩阵 3*3
Bitmap: 要绘制的图片
四 图片的旋转 平移 缩放
其中 mm.jpg 是一张 57KB 的图 属于正常范围 不需要额外处理
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView mImageView, mCopyView;
Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.change).setOnClickListener(this);
mImageView = findViewById(R.id.image);
mCopyView = findViewById(R.id.copy);
try {
mBitmap = BitmapFactory.decodeStream(getAssets().open("mm.jpg"));
mImageView.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.change:
change();
break;
}
}
// 图片的旋转 平移 缩放
// 注意: 旋转 平移 缩放 这三种效果在本案例中只能同时存在一种 分别打开注释看效果
private void change() {
if (null == mBitmap) return;
// 新建空白的图片 要和原图的大小一样
Bitmap bitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());
Canvas canvas = new Canvas(bitmap); //画布 传参必须是一个空白的图片 否则报错
Paint paint = new Paint(); //画笔
Matrix matrix = new Matrix(); //矩阵
// 旋转30度 以图片的中心为圆心
matrix.setRotate(30, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
// X轴平移80
//matrix.setTranslate(80, 0);
// Y轴缩为原来的0.5
//matrix.setScale(1F, 0.5F, mBitmap.getWidth() / 2, mBitmap.getHeight() / 2);
canvas.drawColor(Color.WHITE); //绘制背景为白色
canvas.drawBitmap(mBitmap, matrix, paint); //绘制图片
mCopyView.setImageBitmap(bitmap);
}
}
五 图片的涂鸦操作
其中 mm.jpg 是一张 57KB 的图 属于正常范围 不需要额外处理
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
ImageView mImageView;
Bitmap mNewBitmap;
Canvas mCanvas;
Paint mPaint;
Matrix mMatrix;
int mStartX, mStartY; //按下点的坐标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image);
try {
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("mm.jpg"));
// 不能直接在原图上进行绘制 必须新建空白的图片
mNewBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
mCanvas = new Canvas(mNewBitmap);
mPaint = new Paint();
mPaint.setColor(Color.YELLOW);
mMatrix = new Matrix();
// 把原图绘制在空白的图片上
mCanvas.drawBitmap(bitmap, mMatrix, mPaint);
mImageView.setImageBitmap(mNewBitmap);
mImageView.setOnTouchListener(this); //设置触摸监听
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //按下
mStartX = (int) event.getX();
mStartY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE: //移动
// 获取移动点的坐标
int moveX = (int) event.getX();
int moveY = (int) event.getY();
// 画线
mCanvas.drawLine(mStartX, mStartY, moveX, moveY, mPaint);
// 把新图设置给ImageView
mImageView.setImageBitmap(mNewBitmap);
// 把移动点置为开始点
mStartX = moveX;
mStartY = moveY;
break;
case MotionEvent.ACTION_UP: //弹起
break;
}
return true; //事件自己来处理
}
}
六 图片的颜色处理
图片是有颜色
核心原理就是重绘图片
改变图片的颜色就是对画笔进行操
其中 mm.jpg 是一张 57KB 的图 属于正常范围 不需要额外处理
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView mImageView;
Bitmap mBitmap, mNewBitmap;
Canvas mCanvas;
Paint mPaint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.image);
try {
mBitmap = BitmapFactory.decodeStream(getAssets().open("mm.jpg"));
mImageView.setImageBitmap(mBitmap);
mNewBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());
mCanvas = new Canvas(mNewBitmap);
mPaint = new Paint();
findViewById(R.id.change).setOnClickListener(this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.change:
int randomR = (int) (Math.random() * 256); //0-255 随机数
int randomG = (int) (Math.random() * 256); //0-255 随机数
int randomB = (int) (Math.random() * 256); //0-255 随机数
int randomA = (int) (Math.random() * 256); //0-255 随机数
float colorR = (255 - randomR) / (float) 255;
float colorG = (255 - randomG) / (float) 255;
float colorB = (255 - randomB) / (float) 255;
float colorA = (255 - randomA) / (float) 255;
Log.i("HUANG", "randomR=" + randomR);
Log.i("HUANG", "randomG=" + randomG);
Log.i("HUANG", "randomB=" + randomB);
Log.i("HUANG", "randomA=" + randomA);
Log.i("HUANG", "colorR=" + colorR);
Log.i("HUANG", "colorG=" + colorG);
Log.i("HUANG", "colorB=" + colorB);
Log.i("HUANG", "colorA=" + colorA);
ColorMatrix matrix = new ColorMatrix(); //颜色矩阵 5*4
matrix.set(new float[]{
colorR, 0, 0, 0, 0, //red
0, colorG, 0, 0, 0, //green
0, 0, colorB, 0, 0, //blue
0, 0, 0, colorA, 0 //alpha
});
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
mPaint.setColorFilter(filter);
mCanvas.drawBitmap(mBitmap, new Matrix(), mPaint);
mImageView.setImageBitmap(mNewBitmap);
break;
}
}
}
七 内存泄漏和内存溢出
内存泄漏 (MemoryLeak)
有些对象只有有限的生命周期 当它们的任务完成之后 它们将被回收 如果在对象的生命周期本该结束的时候 这个对象还被一系列的引用 这就会导致内存泄漏
随着泄漏的累积 App 将消耗完内存 内存泄漏最终会导致内存溢出
内存泄漏的原因
1. 资源对象没关闭 (Cursor File...)
2. 没有及时调用 recycle () 释放不再使用的 Bitmap
3. 广播注册没取消
4. ...
神器: LeakCanary 内存泄露检测工具 (https://github.com/square/leakcanary)
内存溢出 (OutOfMemoryError OOM)
内存溢出是指当对象的内存占用已经超出分配内存的空间大小
内存溢出的原因
1. Bitmap 过大
2. 内存泄露导致
3. ...
八 ImageView 中 scaleType 属性值含义
android Bitmap getByteCount和getRowBytes
今天做图像缓存需要计算Bitmap的所占的内存空间,于是研究了下Bitmap关于内存占用的API
1、getRowBytes:Since API Level 1,用于计算位图每一行所占用的内存字节数。
2、getByteCount:Since API Level 12,用于计算位图所占用的内存字节数。
经实测发现:getByteCount() = getRowBytes() * getHeight(),也就是说位图所占用的内存空间数等于位图的每一行所占用的空间数乘以位图的行数。
因为getByteCount要求的API版本较高,因此对于使用较低版本的开发者,在计算位图所占空间时上面的方法或许有帮助。
Android Bitmap 与 DrawAble 与 byte [] 与 InputStream 之间的转换工具
[java] view plaincopyprint?
package com.soai.imdemo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
/**
* Bitmap 与 DrawAble 与 byte [] 与 InputStream 之间的转换工具类
* @author Administrator
*
*/
public class FormatTools {
private static FormatTools tools = new FormatTools();
public static FormatTools getInstance() {
if (tools == null) {
tools = new FormatTools();
return tools;
}
return tools;
}
// 将 byte [] 转换成 InputStream
public InputStream Byte2InputStream(byte[] b) {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
return bais;
}
// 将 InputStream 转换成 byte []
public byte[] InputStream2Bytes(InputStream is) {
String str = "";
byte[] readByte = new byte[1024];
int readCount = -1;
try {
while ((readCount = is.read(readByte, 0, 1024)) != -1) {
str += new String(readByte).trim();
}
return str.getBytes();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 将 Bitmap 转换成 InputStream
public InputStream Bitmap2InputStream(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
return is;
}
// 将 Bitmap 转换成 InputStream
public InputStream Bitmap2InputStream(Bitmap bm, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
return is;
}
// 将 InputStream 转换成 Bitmap
public Bitmap InputStream2Bitmap(InputStream is) {
return BitmapFactory.decodeStream(is);
}
// Drawable 转换成 InputStream
public InputStream Drawable2InputStream(Drawable d) {
Bitmap bitmap = this.drawable2Bitmap(d);
return this.Bitmap2InputStream(bitmap);
}
// InputStream 转换成 Drawable
public Drawable InputStream2Drawable(InputStream is) {
Bitmap bitmap = this.InputStream2Bitmap(is);
return this.bitmap2Drawable(bitmap);
}
// Drawable 转换成 byte []
public byte[] Drawable2Bytes(Drawable d) {
Bitmap bitmap = this.drawable2Bitmap(d);
return this.Bitmap2Bytes(bitmap);
}
// byte [] 转换成 Drawable
public Drawable Bytes2Drawable(byte[] b) {
Bitmap bitmap = this.Bytes2Bitmap(b);
return this.bitmap2Drawable(bitmap);
}
// Bitmap 转换成 byte []
public byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
// byte [] 转换成 Bitmap
public Bitmap Bytes2Bitmap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
return null;
}
// Drawable 转换成 Bitmap
public Bitmap drawable2Bitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
// Bitmap 转换成 Drawable
public Drawable bitmap2Drawable(Bitmap bitmap) {
BitmapDrawable bd = new BitmapDrawable(bitmap);
Drawable d = (Drawable) bd;
return d;
}
}
我们今天的关于android 的 bitmap 问题和android中bitmap的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android - 避免出现 bitmap 内存限制 OUT OF MEMORY 的一种方法、Android Bitmap、android Bitmap getByteCount和getRowBytes、Android Bitmap 与 DrawAble 与 byte [] 与 InputStream 之间的转换工具的相关信息,请在本站查询。
本文标签: