如何在画布上保存绘图以便以后在android中重用?

时间:2014-05-19 15:48:32

标签: android canvas

我正在制作应用程序,从菜单活动中选择颜色,然后在另一个活动中在画布上绘制绘图。如果我去菜单活动改变颜色并返回画布是空白的。我希望它保留以前的绘图。如何保存和重新加载画布图?

 public class DrawingView extends View {

private Paint paint = new Paint();
private Path path = new Path();
private Paint canvasPaint = new Paint(Paint.DITHER_FLAG);
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;



public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
     paint.setAntiAlias(true);
         paint.setStrokeWidth(5f);
         paint.setColor(Color.BLACK);
         paint.setStyle(Paint.Style.STROKE);
         paint.setStrokeJoin(Paint.Join.ROUND);

}



@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    // TODO Auto-generated method stub
    super.onSizeChanged(w, h, oldw, oldh);


     this.setDrawingCacheEnabled(true);  
     buildDrawingCache();
     canvasBitmap = Bitmap.createBitmap(1440,2560, Bitmap.Config.ARGB_8888);
    //canvasBitmap=BitmapFactory.decodeResource(getResources(),R.drawable.images);
    drawCanvas = new Canvas(canvasBitmap);

}

 public Bitmap getBitmap()
    {
        //this.measure(100, 100);
        //this.layout(0, 0, 100, 100);
        this.setDrawingCacheEnabled(true);  
        this.buildDrawingCache();
       Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());   
        this.setDrawingCacheEnabled(false);


    return bmp;
    }




@Override
protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
super.onDraw(canvas);
//canvasBitmap= Bitmap.createBitmap(1440, 2560, Bitmap.Config.ARGB_8888);
    //c = new Canvas(b);
    //canvas.drawBitmap(canvasBitmap, 1440, 2560, canvasPaint);
    canvas.drawBitmap(canvasBitmap, 0, 0, null); 
canvas.drawPath(path, paint);


}
   //function for drawing on touch

@Override
public boolean onTouchEvent(MotionEvent event) {

    // Get the coordinates of the touch event.
                float eventX = event.getX();
                float eventY = event.getY();
                switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                // Set a new starting point
                        path.moveTo(eventX, eventY);
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        // Connect the points
                        path.lineTo(eventX, eventY);
                        break;
                    default:

                return false;

        }

    // Makes our view repaint and call onDraw
                invalidate();

            return true;


}







    //function to change color of paint

     public void setColor(String newcolor) {
    // TODO Auto-generated method stub
    invalidate();
    int paintColor = Color.parseColor(newcolor);
    paint.setColor(paintColor);
}



public void clearScreen() {
    // TODO Auto-generated method stub
//  canvasBitmap.eraseColor(android.graphics.Color.WHITE);

//paint=new Paint();
    path=new Path();
     paint.setAntiAlias(true);
     paint.setStrokeWidth(5f);
     //paint.setColor(Color.BLACK);
//     int paintColor = Color.parseColor(samecolor);
    //  paint.setColor(paintColor);
     paint.setStyle(Paint.Style.STROKE);
     paint.setStrokeJoin(Paint.Join.ROUND);
     invalidate();

}



public void setColorint(int i, int j, int k) {
    // TODO Auto-generated method stub
    invalidate();
    int paintColor = Color.rgb(i,j,k);
    paint.setColor(paintColor);
          }
     }

1 个答案:

答案 0 :(得分:1)

您可以在离开活动之前复制当前位图。在ACTION_UP事件中保存图形缓存将有助于您获得最新信息:

...
switch (event.getAction()) {
    ...
    case MotionEvent.ACTION_UP:
        canvasBitmap = getBitmap();
        path=new Path();  // If you want a new path with each touch
        break;
    ...

因此,当您离开活动时,可以调用getBitmap方法以获取绘图的当前状态,并在返回活动时将其恢复。

由于你的Bitmap太大了,我想你无法使用Activity或View状态来保持它,就像解释here一样。相反,您可以将它保留在Application对象中,解释为here