直接上代码吧
View view = findViewById(R.id.testview);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
//设置背景色
view.setDrawingCacheBackgroundColor(Color.WHITE);
//从Drawing Cache里取得图像内容
Bitmap bitmap = view.getDrawingCache();
//下面是将bitmap保存为JPEG图片
File file = new File(path);
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//关闭Drawing Cache
view.destroyDrawingCache();
方法2 使用Canvas
还是上代码好了.
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
//启用canvas抗锯齿
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
//接下来可以通过canvas api将文字/图片等画上去,比如:
canvas.drawText(...);
canvas.drawBitmap(...);
//保存为图片
FileOutputStream out = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
作者:VivianLab