2

I am making app which select color from menu activity and then draw drawing on canvas in another activity. If I go to menu activity for changing color and come back canvas is blank.I want it to retain previous drawing. how to save and reload canvas drawing?

 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 Answers1

1

You can make a copy of the current bitmap before leaving the activity. Saving the drawing cache on ACTION_UP event will help you being up to date:

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

So when you are leaving your activity, you can call your getBitmap method in order to get the current state of the drawing and restore it when you are back to the activity.

As your Bitmap is so big, I guess you cannot keep it using Activity or View states, like explained here. Instead you can keep it in the Application object, explained here.

Community
  • 1
  • 1
Juan Sánchez
  • 980
  • 2
  • 10
  • 26