canvas save and restore 方法

class CavasView extends View{
            public CavasView(Context context) {
                 super(context);
           }
           
            @Override
            protected void onDraw(Canvas canvas) {
                 super.onDraw(canvas);
                 int px = getMeasuredWidth();
                 int py = getMeasuredHeight();
                Paint bgPaint = new Paint();
                bgPaint.setColor(Color. BLUE);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1 );
                canvas.drawRect(0, 0, px, py, bgPaint);
                canvas.drawBitmap(bitmap, 0, 0, bgPaint);
                
                canvas.save();
                canvas.rotate(90, px/2, py/2);
                canvas.drawBitmap(bitmap, 0, 0, bgPaint);
                Paint linePaint = new Paint();
                linePaint.setColor(Color. RED);
                canvas.drawLine(px/2, 0, 0, py/2, linePaint);
                canvas.drawLine(px/2, 0, px, py/2, linePaint);
                canvas.drawLine(px/2, 0, px/2, py, linePaint);
                Paint cirlePaint = new Paint();
                canvas.drawCircle(px/2 + 100, py/2 + 100, 40, cirlePaint);
                canvas.restore();
                
                canvas.drawCircle(px/2 + 100, py/2 + 100, 40, linePaint);
           }
           @Override
            protected void onDraw(Canvas canvas) {
                 super.onDraw(canvas);
                canvas.drawColor(Color. BLUE);
                Paint paint = new Paint();
                paint.setColor(Color. GRAY);
                paint.setStrokeWidth(2.0f);
                paint.setStyle(Paint.Style. STROKE);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1 );
                canvas.drawLine(0, 600, 480, 600, paint);
                 for( int i=0;50*i<600;i++) 
                     canvas.drawLine(0, 50*i, 480, 50*i, paint); 
                 for( int i=0;50*i<480;i++) 
                     canvas.drawLine(50*i, 0, 50*i, 600, paint);
            
             Matrix matrix = new Matrix();
             Camera camera = new Camera();
             camera.save();
             camera.rotateX(45);
             camera.translate(100, 0, 300);
             camera.getMatrix(matrix);
             camera.restore();
             matrix.setRotate(30);
             matrix.preTranslate(-240, -300);
             matrix.postTranslate(240, 300);
            
             canvas.concat(matrix);
             canvas.drawBitmap(bitmap, matrix, null);
            
             //保存canvas到图片
             Bitmap xBitmap = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888 );
             Canvas xCanvas = new Canvas(xBitmap);
             xCanvas.drawBitmap(bitmap, matrix, null);
             xCanvas.drawLine(0, 600, 480, 600, paint);
                 for( int i=0;50*i<600;i++) 
                      xCanvas.drawLine(0, 50*i, 480, 50*i, paint); 
                 for( int i=0;50*i<480;i++) 
                     xCanvas.drawLine(50*i, 0, 50*i, 600, paint);
             xCanvas.save(Canvas. ALL_SAVE_FLAG);
             xCanvas.restore();
             Log. d("nicholas" , "onDraw" );
             File file;
             FileOutputStream os = null;
                 try {
                     file = new File("/sdcard/1.png" );
                      if(!file.exists()){
                           file.createNewFile();
                     }
                     os = new FileOutputStream(file);
                     xBitmap.compress(CompressFormat. PNG, 50, os);
                     os.flush();
                     os.close();
                } catch (FileNotFoundException e) {
                     e.printStackTrace();
                     
                } catch (IOException e) {
                     e.printStackTrace();
                } finally{
                      try {
                            if(os != null)
                           os.close();
                     } catch (IOException e) {
                            // TODO Auto-generated catch block
                           e.printStackTrace();
                     }
                }
           
     }



❑ save:用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作。 画布被修改,如果不这样,画布

被旋转,变形了,画上去点线可能不是我们自己想要的,参看本例,旋转后划线

❑ restore:用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。



canvas save and restore 方法

canvas save and restore 方法





你可能感兴趣的:(canvas,bitmap,restore,save)