How to dynamically draw bitmaps to a Canvas in Android? -


here scenario of issue . custom view canvas on need draw . 2 buttons beneath ,lets call them , b when clicked -> image drawn above canvas . when b clicked -> image b drawn above canvas .

the issue demands drawn image on canvas must preserved . if click button followed button b canvas must contain 2 images . previous images need preserved.

problem : how achieve ? possible solution 1 : create arraylist , keep adding images arraylist . pass updated arraylist canvas ondraw method , redraw every single image every button click .

possible solution 2 : there has method preserve state of canvas on each button click draw on canvas's last preserved state , draw new image .

further requirements : images drawn canvas dragged need keep track of updated positions .

i @ impasse , couldn't find tutorial or book tackling such requirement , appreciated .

you can create bitmap, , draw want on it.

bitmap mbitmap;  protected void onlayout(boolean changed, int left, int top, int right, int bottom) {     super.onlayout(changed, left, top, right, bottom);     if (changed) {         if (mbitmap != null) {             mbitmap.recycle();             mbitmap = null;         }         mbitmap = bitmap.createbitmap(right - left, bottom - top, bitmap.config.argb_8888);         redrawallyourstuffto(mbitmap);     } } 

when button pressed, can draw directly bitmap, this:

canvas canvas = new canvas(mbitmap); canvas. ... // draw operations.  // after bitmap canvas drawing finished, call invalidate(); 

in ondraw paint bitmap

protected void ondraw(canvas canvas) {    canvas.drawbitmap(mbitmap, 0, 0, null); } 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -