标签:android style blog http io ar color os sp
canvas绘图后保存图片,分两种情况,一种:自己new一个canvas对象。 第二种:在onDraw函数作为参数引入Canvas
建一个空的bitmap对象,然后在初始化画布时,把它作为参数带入,之后canvas所做的绘图操作,都是在该bitmap上,只要保存该bitmap对象即可。如下代码:画一个蓝色的圆环:
//这边直接用canvas画,然后保存 Bitmap bitmap = Bitmap.createBitmap(700,700, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setAntiAlias(true);//设置抗锯齿 paint.setStrokeWidth(7);//设置笔触宽度 paint.setStyle(Paint.Style.STROKE); //画奥运五环 paint.setColor(Color.BLUE); canvas.drawCircle(150,150,100,paint); //绘制蓝色圆环 canvas.save(Canvas.ALL_SAVE_FLAG); //保存 canvas.restore(); // 存储 File sdRoot = Environment.getExternalStorageDirectory(); String save_path = sdRoot.getAbsolutePath() + "/DCIM/100MEDIA/114.jpg"; try { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(save_path))); } catch (FileNotFoundException e) { e.printStackTrace(); }
重点就是setDrawingCacheEnabled(true); 和 getDrawingCache()这两个函数
public MyView(Context context) { super(context); //这函数是要打开图形缓存,这样才能getDrawingCache setDrawingCacheEnabled(true); }
protected void onDraw(Canvas canvas){ canvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setAntiAlias(true);//设置抗锯齿 paint.setStrokeWidth(7);//设置笔触宽度 paint.setStyle(Paint.Style.STROKE); //画一个环 paint.setColor(Color.BLUE); canvas.drawCircle(150,150,100,paint); //绘制蓝色圆环 canvas.save(Canvas.ALL_SAVE_FLAG); //保存 canvas.restore(); // 存储 //将canvas里的draw保存下来 File sdRoot = Environment.getExternalStorageDirectory(); String save_path = sdRoot.getAbsolutePath() + "/DCIM/100MEDIA/113.jpg"; try { getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(save_path))); } catch (Exception e) { Log.e("Error--------->", e.toString()); } super.onDraw(canvas); }
标签:android style blog http io ar color os sp
原文地址:http://www.cnblogs.com/zhongyuan/p/4107883.html