Search code examples
androiddrawingandroid-canvas

How to redraw the drawing in canvas when orientation changed


My code is working fine not until the table or the device is rotated. When rotated the drawing got cleared and I learned it happen because when the device gets rotated, the activity is restarted. My problem is how redraw and save the data to onSaveInstanceState. Many said that I have to use onSaveInstanceState to save the drawing, but i don't know how to do it when the datatype I have to save is the Path.

here is the snippet of my code

public class MyDrawView extends View implements OnTouchListener {
    MyDrawView drawView;
    float prevX = 0;
    float prevY = 0;

    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;
    private Paint mPaint;
    private int w,h;
    private ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<Paint> bitPaints = new ArrayList<Paint>();
    private ArrayList<Paint> pathPaints = new ArrayList<Paint>();
    Paint tempPaint;
    private String filename="";

    public MyDrawView(Context c,String filename) {
        super(c);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);


        this.filename=filename;
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);
        tempPaint=new Paint(mPaint);
        mCanvas = new Canvas();
        mPath = new Path();
        if (rotated){
            setDefaultDrawing();
        }

    }
    public void setDefaultDrawing()
    {

    }
    public Bitmap getBitmap(){
        return this.mBitmap;
    }
    public void setStrokeWidth(float width){
        mPaint.setStrokeWidth(width);
    }
    public void setColor(int color){
        mPaint.setColor(color);
    }
    public String getFileName(){
        return this.filename;
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w=w;
        this.h=h;
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        Path tempPath=new Path(mPath);
        paths.add(tempPath);
        Paint tempPaint=new Paint(mPaint);
        pathPaints.add(tempPaint);
        Paint tempBitPaint=new Paint(mBitmapPaint);
        bitPaints.add(tempBitPaint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

Solution

  • I figured it out, since Path is a custom object, I created a class object that will hold the Path, in that way, I can just save the class object in my onSaveInstanceState and retrieve it using the same class object.