android - Creating a bitmap of a View that never gets drawn -


i programmatically creating view never gets drawn screen (the view isn't inflated xml or set on screen during oncreate). instead, want create bitmap of view use image. however, cannot bitmap/a correct bitmap.

i have looked on site , have used various questions such here: converting view bitmap without displaying in android?. initially, used following code:

public bitmap loadbitmapfromview(view v) {     bitmap bitmap = bitmap.createbitmap(v.getlayoutparams().width, v.getlayoutparams().height, bitmap.config.argb_8888); //v.getwidth() , v.getheight() 0 since view isn't drawn     canvas canvas = new canvas(bitmap);     v.measure(measurespec.makemeasurespec(0, measurespec.unspecified), measurespec.makemeasurespec(0, measurespec.unspecified);     v.layout(0, 0, v.getlayoutparams().width, v.getlayoutparams().height); //v.getmeasuredheight() , v.getmeasuredwidth() 0 --- not sure why?     v.draw(canvas);     return bitmap; } 

this code created bitmap empty , didn't have view image. tried third answer down (by @dwivedi ji), posted convenience:

private bitmap getviewbitmap(view v) {     v.clearfocus();     v.setpressed(false);     v.setdrawingcacheenabled(true);      boolean willnotcache = v.willnotcachedrawing();     v.setwillnotcachedrawing(false);      int color = v.getdrawingcachebackgroundcolor();     v.setdrawingcachebackgroundcolor(0);      if (color != 0) {         v.destroydrawingcache();     }     v.builddrawingcache();     bitmap cachebitmap = v.getdrawingcache(); //this null     if (cachebitmap == null) {          log.e(tag, "failed getviewbitmap(" + v + ")", new runtimeexception());         return null;     }      bitmap bitmap = bitmap.createbitmap(cachebitmap);      // restore view     v.destroydrawingcache();     v.setwillnotcachedrawing(willnotcache);     v.setdrawingcachebackgroundcolor(color);     v.setdrawingcacheenabled(false);      return bitmap; } 

the v.getdrawingcache() call returns null left me without bitmap.

how bitmap view image? apologize lengthy question (kinda new posting here, please let me know if need additional info/clarification).

thanks in advance!

edit

the class try make bitmap follows:

public class circleview extends view {     ... //constructors - nothing special here      @override protected void ondraw(canvas canvas)     {         canvas.drawcolor(0x00000000);          if(m_linewidthpx == 0) //previously set             return;          float centerx = getmeasuredheight() / 2.f;         float centery = getmeasuredheight() / 2.f;         float radius = getmeasuredwidth() / 2.f - (m_linewidthpx) / 2.f;         m_paint.setstrokewidth(m_linewidthpx); //m_paint instantiated in constructors         m_paint.setstyle(paint.style.stroke);         m_paint.setantialias(true);         canvas.drawcircle(centerx, centery, radius, m_paint);     } } 

there isn't class, draws circle. omitted constructors since nothing special occurs there. set m_linewidthpx variable , instantiate m_paint variable.

check size has bitmap, isn't 0x0 pt?

i use approach this:

public static bitmap getbitmapfromview(view view){      view.measure(linearlayout.layoutparams.wrap_content,                  linearlayout.layoutparams.wrap_content);      view.layout(0, 0, view.getmeasuredwidth(), view.getmeasuredheight());      bitmap bitmap = bitmap.createbitmap(view.getwidth(),                                         view.getheight(),                                         bitmap.config.argb_8888);     canvas c = new canvas(bitmap);     view.draw(c);     return bitmap; } 

Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -