cstriker1407的笔记本

好记性不如铅笔头

android, 编程

Android应用开发笔记:bitmap的常见处理

最近在做android项目时,遇到了一些bitmap合并和写文件的需求。就在这里简单备份下代码。

public class Bitmaps {
	/*
	 * 先计算出新生成的bitmap的大小,然后根据不同的坐标使用canvas来绘制。
	 */
	public static Bitmap joinTopBottomBitmap(Bitmap top, Bitmap bottom) {
		if (null == top || null == bottom) {
			return null;
		}

		int topW = top.getWidth();
		int topH = top.getHeight();

		int bottomW = top.getWidth();
		int bottomH = top.getHeight();
		int resultW = topW > bottomW ? topW : bottomW;
		int resultH = topH + bottomH;
		Bitmap result = Bitmap.createBitmap(resultW, resultH,
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawBitmap(top, 0, 0, null);
		canvas.drawBitmap(bottom, 0, topH, null);

		return result;
	}

	public static boolean writeBitmapToFile(Bitmap map, String filePath) {
		if (null == map || null == filePath) {
			return false;
		}
		FileOutputStream fileOutputStream = null;

		try {
			fileOutputStream = new FileOutputStream(filePath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		map.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

		boolean result = true;

		try {
			fileOutputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
		try {
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}

		return result;
	}

}

 

Leave a Reply

2 × 5 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Theme by Anders Norén

苏ICP备16032087号