Android Canvas 保存为图片

-------Activity.java

File f = new File("/sdcard/qianming.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
dv.cacheBitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

--------------DrawView.java

float preX;
float preY;
private Path path;
public Paint paint = null;
final int VIEW_WIDTH = 1024;
final int VIEW_HEIGHT = 1024;
// 定义一个内存中的图片,该图片将作为缓冲区
Bitmap cacheBitmap = null;
// 定义cacheBitmap上的Canvas对象
Canvas cacheCanvas = null;

public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT,
Config.ARGB_8888);
cacheCanvas = new Canvas();
path = new Path();
// 设置cacheCanvas将会绘制到内存中的cacheBitmap上
cacheCanvas.setBitmap(cacheBitmap);
// 设置画笔的颜色
paint = new Paint(Paint.DITHER_FLAG);
paint.setColor(Color.RED);
// 设置画笔风格
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
// 反锯齿
paint.setAntiAlias(true);
paint.setDither(true);
cacheCanvas.save(Canvas.ALL_SAVE_FLAG );
cacheCanvas.restore();

}

发布了1 篇原创文章 · 获赞 79 · 访问量 39万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览