Save view as an image in android -


i working on project in have created custom view transparent background. want create image of view. using below code save , create image bitmap, generated image has black background , not transparent. want transparent background. how can achieve this?

view = suraversesfragment.getmyview();             view.setbackgroundcolor(color.transparent);             bitmap b = bitmap.createbitmap(view.getwidth(), view.getheight(), bitmap.config.argb_8888);             canvas c = new canvas(b);              c.drawcolor(color.transparent, porterduff.mode.clear);             view.draw(c);             bytearrayoutputstream bytes = new bytearrayoutputstream();             b.compress(bitmap.compressformat.jpeg, 100, bytes);             file f = new file(environment.getexternalstoragedirectory() + file.separator + "temporary_file.png");             try {                 f.createnewfile();                 fileoutputstream fo = new fileoutputstream(f);                 fo.write(bytes.tobytearray());             } catch (ioexception e) {                 e.printstacktrace();             } 

for receiving non scaled bitmap of view can call method after enabled drawing cache

view.getdrawingcache() 

the api tells method:

returns bitmap in view drawing cached. returned bitmap null when caching disabled. if caching enabled , cache not ready, method create it. calling draw(android.graphics.canvas) not draw cache when cache enabled. benefit cache, must request drawing cache calling method , draw on screen if returned bitmap not null.

note auto scaling in compatibility mode: when auto scaling not enabled, method create bitmap of same size view. because bitmap drawn scaled parent viewgroup, result on screen might show scaling artifacts. avoid such artifacts, should call method setting auto scaling true. doing so, however, generate bitmap of different size view. implies application must able handle size.

you can set background's color

view.setdrawingcachebackgroundcolor(color.xy) 

you can here: http://developer.android.com/reference/android/view/view.html#getdrawingcache(boolean) http://developer.android.com/reference/android/view/view.html#getdrawingcache() http://developer.android.com/reference/android/view/view.html#setdrawingcachebackgroundcolor(int)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -