2014年8月7日 星期四

android 两张图片覆盖 合成为一张 + 处理(图片合成、图片圆角、图片翻转、图片缩放)

android 两张图片覆盖 合成为一张

infor from - http://devdd.sinaapp.com/post-282.html
把一张图片覆盖到另一种图片上,合并保存为一张图片
思路:创建这两种图片的 bitmap 对象,得到底图的高度和宽度,根据此尺寸创建新的 bitmap 对象,及 canvas 画布,先画入底图,然后再在目标坐标画另一张图片,保存
//创建两个bitmap,一个做为底层,一个做为上层
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.a);
Bitmap front = BitmapFactory.decodeResource(getResources(), R.drawable.t1);

//取得底层的宽高
int bgWidth = background.getWidth();   
int bgHeight = background.getHeight();

//创建新bitmap
Bitmap newbmp = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888);  
Canvas cv = new Canvas(newbmp);   
cv.drawBitmap(background, 0, 0, null);//在 0,0坐标开始画入bg   
cv.drawBitmap(front, 0, 0, null);//在 0,0坐标开始画入fg ,可以从任意位置画入
cv.save(Canvas.ALL_SAVE_FLAG);//保存
cv.restore();//存储   

//在图像视图中显示合成后的图片
ImageView img = (ImageView)findViewById(R.id.img);
img.setImageBitmap(newbmp);

//保存到SD卡中
FileOutputStream fos = null;
String filepath = Environment.getExternalStorageDirectory()
  + File.separator + "newimg.png";
FileOutputStream m_fileOutPutStream = null;
try {
  m_fileOutPutStream = new FileOutputStream(filepath);
  newbmp.compress(CompressFormat.PNG, 100, m_fileOutPutStream);
  m_fileOutPutStream.flush();
  m_fileOutPutStream.close();
} catch (Exception e) {
} finally {
    if (fos != null) {
  try {
      fos.flush();
      fos.close();
  } catch (IOException e) {
  }
    }
}





Android之图片处理(图片合成、图片圆角、图片翻转、图片缩放)

infor from - http://www.cnblogs.com/olvo/archive/2012/04/14/2447097.html
图片合成 /** * 图片合成 * @param bitmap * @return */ private Bitmap createBitmap( Bitmap src, Bitmap watermark ) { 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; } 图片圆角 /** * 图片圆角 * @param bitmap * @return */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = 12; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint);  return output; } 图片缩放、翻转和旋转 /** * 缩放、翻转和旋转图片 * @param bmpOrg * @param rotate * @return */ private android.graphics.Bitmap gerZoomRotateBitmap( android.graphics.Bitmap bmpOrg, int rotate) { // 获取图片的原始的大小 int width = bmpOrg.getWidth(); int height = bmpOrg.getHeight(); int newWidth = 300; int newheight = 300; // 定义缩放的高和宽的比例 float sw = ((float) newWidth) / width; float sh = ((float) newheight) / height; // 创建操作图片的用的Matrix对象 android.graphics.Matrix matrix = new android.graphics.Matrix(); // 缩放翻转图片的动作 // sw sh的绝对值为绽放宽高的比例,sw为负数表示X方向翻转,sh为负数表示Y方向翻转 matrix.postScale(sw, sh); // 旋转30* matrix.postRotate(rotate); //创建一个新的图片  android.graphics.Bitmap resizeBitmap = android.graphics.Bitmap .createBitmap(bmpOrg, 0, 0, width, height, matrix, true); return resizeBitmap; }

沒有留言:

張貼留言