view - Save ViewGroup state : Android -
note: here not talking saving state of activity through onsaveinstancestate(...)
i using https://github.com/eskim/android_drag_sample code drag , drop views in application. user can create multiple scenes in application , each scene, user can add multiple views , drag , drop them wherever want. while switching scenes storing scene information number of children's added, properties etc in model object , working fine. user can save scenes , send through mail pdf. problem prepare pdf need images(am using itext library convert images pdf) how can convert other hidden scenes(which not visible) image? visible scene can convert image , store in sd card. tried saving current scene in model object , converting image while preparing pdf overwriting scenes current scene. dont know whether approach correct or not. so, or clue appreciated.
code convert view bitmap :
public static bitmap getbitmapfromview(final view view) { if(view.getwidth() <= 0 && view.getheight() <= 0) return null; bitmap returnedbitmap = bitmap.createbitmap(view.getwidth(), view.getheight(), bitmap.config.argb_8888); final canvas canvas = new canvas(returnedbitmap); drawable bgdrawable = view.getbackground(); if (bgdrawable != null) bgdrawable.draw(canvas); else canvas.drawcolor(color.white); ((activity) view.getcontext()).runonuithread(new runnable() { @override public void run() { view.draw(canvas); } }); return returnedbitmap; }
after quite long time, got solution problem. idea whatever data there in model object, adding layout , getting image that. below code.
/** convert mystscene(model object) view */ public static view getview(context context, mystscene mystscene) { relativelayout sceneview = new relativelayout(context); relativelayout.layoutparams parms = new relativelayout.layoutparams(constants.play_area_width_tag, layoutparams.wrap_content); parms.setmargins(100, 100, 100, 100); string scenebackground = mystscene.getthemeimagename(); drawable drawable = bitmapconverter.getdrawable(context, scenebackground); sceneview.setbackgrounddrawable(drawable); for(mystcharacter mystcharacter : mystscene.getcharacterlist()) { int scale = mystcharacter.getscale(); imageview image = new imageview(context); string filepath = mystcharacter.getcharactername(); int xposition = (int) mystcharacter.getcharacterpoint().x + 141; if(xposition >= constants.play_area_width_tag) xposition -= 400; else if(xposition > (constants.play_area_width_tag / 2)) xposition -= 300; else xposition -= 200; int yposition = (int) mystcharacter.getcharacterpoint().y; if(!mystcharacter.getisdialog()) sceneview.addview(setimageproperties(image, scale, filepath, xposition, yposition, 0, 0)); else { adddialog(mystcharacter, sceneview, filepath, xposition, yposition); } } // add writing pad if text added while creating/saving scene boolean istrue = mystscene.getstorytext() != null && mystscene.getstorytext().length() != 0; if(istrue) addwritingpad(mystscene, sceneview); sceneview.setlayoutparams(parms); return sceneview; }
save view sd card image
public static void saveviewtosd(getview(context, mystscene)) { file fullsavedir = fileutils.createdirectory(fileutils.pdf_images_directory_tag); file file = new file(fullsavedir, ""+system.currenttimemillis()+".png"); bitmap bitmap = null; try { if(file != null && !file.exists()) file.createnewfile(); fileoutputstream fileoutputstream = new fileoutputstream(file); bitmap = loadbitmapfromview(view); if(bitmap == null) { fileoutputstream.close(); return; } bitmap.compress(bitmap.compressformat.png, 90, fileoutputstream); fileoutputstream.flush(); fileoutputstream.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if(bitmap != null && !bitmap.isrecycled()) { bitmap.recycle(); bitmap = null; } } }
load bitmap view
public static bitmap loadbitmapfromview(view view) throws illegalargumentexception { if (view.getmeasuredheight() <= 0) view.measure(layoutparams.wrap_content, layoutparams.wrap_content); bitmap bitmap = bitmap.createbitmap( constants.play_area_width_tag, constants.play_area_height_tag, bitmap.config.argb_8888); canvas canvas = new canvas(bitmap); drawable bgdrawable = view.getbackground(); bitmap bitmapbg = ((bitmapdrawable)bgdrawable).getbitmap(); canvas.drawbitmap(bitmapbg, view.getleft(), view.gettop(), null); view.layout(view.getleft(), view.gettop(), view.getright(), view.getbottom()); view.draw(canvas); return bitmap; }
Comments
Post a Comment