Bitmap是Android系统中的图像处理的最重要的类之一。
用Bitmap可以获取图像文件信息,对图像进行旋转,剪切,放大,缩小等操作。
在Android SDK中可以支持的图片格式如下:png , jpg , gif和bmp。


一 创建
1  从资源中获取位图
1.1 使用BitmapDrawable获取位图
a 使用BitmapDrawable (InputStream is)构造一个BitmapDrawable;
b 使用BitmapDrawable类的getBitmap()获取得到位图;

// 读取InputStream并得到位图
InputStream is = res.openRawResource(R.drawable.pic180);
BitmapDrawable bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();
或者
BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);
Bitmap bmp=bmpDraw.getBitmap();



1.2 使用BitmapFactory获取位图


BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。


decodeByteArray(byte[] data, int offset,int length)从指定字节数组的offset位置开始,将长度为length的字节数据解析成Bitmap对象。


decodeFIle(String pathName)从pathName指定的文件中解析、创建Bitmap对象。


decodeFileDescriptor(FileDescriptor fd)用于从FileDescriptor对应的文件中解析、创建Bitmap对象。


decodeResource(Resource res,int id)用于根据给定的资源ID从指定的资源文件中解析、创建Bitmap对象。


decodeStream(InputStream is)用于从指定输入流中介解析、创建Bitmap对象。



例如:


Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pic180);




在系统不断的解析、创建Bitmap的过程中,可能会由于内存小或其他原因,导致程序运行时发生OutOfMemory错误。


为此,Android为Bitmap提供了内存回收方法:


void recycle();强制回收Bitmap对象。


boolean isRecycle();判断Bitmap 对象是否被回收的方法:




二 获取位图的信息


public final int getWidth()获取位图的宽度 


public final int getHeight()获取位图的高度 


public final boolean isMutable()图片是否可修改 


public int getScaledWidth(Canvas canvas)获取指定密度转换后的图像的宽度 


public int getScaledHeight(Canvas canvas)获取指定密度转换后的图像的高度 


public boolean compress(CompressFormat format, int quality, OutputStream stream)——按指定的图片格式以及画质,将图片转换为输出流。 


format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG 


quality:画质,0-100.0表示最低画质压缩,100以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置。 




另外补充两点:


在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题;


Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充了。




三 显示位图


显示位图可以使用核心类Canvas,通过Canvas类的drawBirmap()显示位图。


或者借助于BitmapDrawable来将Bitmap绘制到Canvas。


当然,也可以通过BitmapDrawable将位图显示到View中。




1 转换为BitmapDrawable对象显示位图


// 获取位图
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
// 显示位图
ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
iv2.setImageDrawable(bmpDraw);



2 使用Canvas类显示位图


public void onDraw(Canvas canvas){  
	Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
	canvas.drawColor(Color.BLACK);
	canvas.drawBitmap(bmp, 10, 10, null);
}




四 位图缩放


1 将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。


2 在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)


3 借助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不过要注意此时整个画布都缩放了。


4 借助Matrix:


Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  
Matrix matrix=new Matrix();
matrix.postScale(0.2f, 0.2f);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);  
canvas.drawBitmap(dstbmp, 10, 10, null);



五 位图旋转


同样,位图的旋转也可以借助Matrix或者Canvas来实现。


Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  
Matrix matrix=new Matrix();
matrix.postScale(0.8f, 0.8f);
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK); 
canvas.drawBitmap(dstbmp, 10, 10, null);

参考文章:


http://coollast.blog.51cto.com/6319475/1120760