Android图片合成
来源:互联网 发布:ubuntu 命令行解压tar 编辑:程序博客网 时间:2024/04/28 01:09
项目中遇到了一个图片合成的问题,搞很长时间也没搞定,最后使用Canvas和Bitmap解决了画图的问题。
Canvas是一个画布,你可以建立一个空白的画布,就直接new一个Canvas对象,不需要参数。
也可以先使用BitmapFactory创建一个Bitmap对象,作为新的Canvas对象的参数,也就是说这个画布不是空白的,
如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。
然后使用Canvas画第一张图上去,在画第二张图上去,最后使用Canvas.save(int flag)的方法进行保存,注意save方法里面的参数可以保存单个图层,
如果是保存全部图层的 话使用 save( Canvas.ALL_SAVE_FLAG )。
最后所有的信息都会保存在第一个创建的Bitmap中。代码如下:
Java代码
/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
对图片进行缩小的方法:
Java代码
/**
* lessen the bitmap
*
* @param src bitmap
* @param destWidth the dest bitmap width
* @param destHeigth
* @return new bitmap if successful ,oherwise null
*/
private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )
{
String tag = "lessenBitmap";
if( src == null )
{
return null;
}
int w = src.getWidth();//源文件的大小
int h = src.getHeight();
// calculate the scale - in this case = 0.4f
float scaleWidth = ( ( float ) destWidth ) / w;//宽度缩小比例
float scaleHeight = ( ( float ) destHeigth ) / h;//高度缩小比例
Log.d( tag, "bitmap width is :" + w );
Log.d( tag, "bitmap height is :" + h );
Log.d( tag, "new width is :" + destWidth );
Log.d( tag, "new height is :" + destHeigth );
Log.d( tag, "scale width is :" + scaleWidth );
Log.d( tag, "scale height is :" + scaleHeight );
Matrix m = new Matrix();//矩阵
m.postScale( scaleWidth, scaleHeight );//设置矩阵比例
Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩阵的比例把源文件画入进行
return resizedBitmap;
}
Canvas是一个画布,你可以建立一个空白的画布,就直接new一个Canvas对象,不需要参数。
也可以先使用BitmapFactory创建一个Bitmap对象,作为新的Canvas对象的参数,也就是说这个画布不是空白的,
如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。
然后使用Canvas画第一张图上去,在画第二张图上去,最后使用Canvas.save(int flag)的方法进行保存,注意save方法里面的参数可以保存单个图层,
如果是保存全部图层的 话使用 save( Canvas.ALL_SAVE_FLAG )。
最后所有的信息都会保存在第一个创建的Bitmap中。代码如下:
Java代码
/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
/**
* create the bitmap from a byte array
*
* @param src the bitmap object you want proecss
* @param watermark the water mark above the src
* @return return a bitmap object ,if paramter's length is 0,return null
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark )
{
String tag = "createBitmap";
Log.d( tag, "create a new bitmap" );
if( src == null )
{
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
对图片进行缩小的方法:
Java代码
/**
* lessen the bitmap
*
* @param src bitmap
* @param destWidth the dest bitmap width
* @param destHeigth
* @return new bitmap if successful ,oherwise null
*/
private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )
{
String tag = "lessenBitmap";
if( src == null )
{
return null;
}
int w = src.getWidth();//源文件的大小
int h = src.getHeight();
// calculate the scale - in this case = 0.4f
float scaleWidth = ( ( float ) destWidth ) / w;//宽度缩小比例
float scaleHeight = ( ( float ) destHeigth ) / h;//高度缩小比例
Log.d( tag, "bitmap width is :" + w );
Log.d( tag, "bitmap height is :" + h );
Log.d( tag, "new width is :" + destWidth );
Log.d( tag, "new height is :" + destHeigth );
Log.d( tag, "scale width is :" + scaleWidth );
Log.d( tag, "scale height is :" + scaleHeight );
Matrix m = new Matrix();//矩阵
m.postScale( scaleWidth, scaleHeight );//设置矩阵比例
Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩阵的比例把源文件画入进行
return resizedBitmap;
}
0 0
- Android图片合成
- Android图片的合成
- Android图片合成
- android 图片合成
- android 图片合成
- android图片合成
- android 图片合成
- Android bitmap合成图片
- Android 图片合成Xfermode示例
- Android合成GIF图片JNI版
- Android合成GIF图片JNI版
- Android图片合成/裁剪原理(转)
- Android gif图片的解码与合成
- Android——操作摄像头、图片合成
- Android实现多张图片合成GIF
- Android相框合成图片抠图
- 合成图片
- 合成图片
- 全部开放基金接口调用代码
- 关于Class和Subclass的分类
- C++多重继承之成员变量的偏移量
- 打造象LINUX一样的mac os x 环境
- 以IP地址定义的链接服务器设置别名
- Android图片合成
- 车辆违章查询演示示例
- Request的getParameter和getAttribute方法的区别
- mysql备份和root密码修改
- 网页爬虫的设计与实现(Java版)
- leetcode(2)--String reverse
- 细说linux IPC(一):基于socket的进程间通信(上)
- Ubuntu 10.10下成功编译Linux-2.6.38内核
- Android之Activity