简体   繁体   中英

Storing drawn canvas components into an array

Is there any possible way to put all the drawn objects into an array? This is just for a drawing game so it is possible to save all objects and load them back from this way. Thanks if this is possible!

Take a look at Picture class - http://developer.android.com/reference/android/graphics/Picture.html

After it's being attached to the canvas, it records all drawing operations and stores it. After you can either draw all of them on another canvas, or store/transfer the Picture to make it on another device or later.

UPD : something like this

    Picture picture = new Picture();
    Canvas canvas = picture.beginRecording(300, 300);
    canvas.drawRect(new Rect(5, 5, 10, 10), new Paint()); // configure paint here
    picture.endRecording();

    picture.writeToStream(new FileOutputStream("stored_drawing.pict"));

Now you'll be able to restore this rectangle from file and draw it on any another canvas. So generally you can wrap you user's drawing through this class and it wil store all user drawing operations.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM