2

I am working on a project in which I have created a custom view with transparent background. Now I want to create an image of this view. I am using the below code to save and create an image from bitmap, but the generated image has black background and not transparent. I want transparent background. How can I 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();
            }
Pranit Bankar
  • 453
  • 1
  • 7
  • 21
santosh
  • 21
  • 4
  • You are storing it as a png, try storing it as a jpeg (temporary file.jpg), that is if what you are saying is the file loaded is black. If it is that the view itself is it could be a number of other things. – zgc7009 Jun 19 '15 at 12:20
  • jpeg not support transparent background as i know..right. – santosh Jun 19 '15 at 12:24
  • Sorry I was backwards in my thinking this morning, momentary lapse, change your compression format to `b.compress(Bitmap.CompressFormat.PNG, 100, bytes);` to compress it into PNG instead of JPEG. You are compressing in JEPG format (non-transparent) and then saving it as a PNG (which will take the non-transparent background) – zgc7009 Jun 19 '15 at 17:18

2 Answers2

1

For receiving a non scaled bitmap of a view you can call this method after you enabled the drawing cache

view.getDrawingCache()

the api tells this about the method:

Returns the bitmap in which this view drawing is cached. The returned bitmap is null when caching is disabled. If caching is enabled and the cache is not ready, this method will create it. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling this method and draw it on screen if the returned bitmap is not null.

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view. Because this bitmap will be drawn scaled by the parent ViewGroup, the result on screen might show scaling artifacts. To avoid such artifacts, you should call this method by setting the auto scaling to true. Doing so, however, will generate a bitmap of a different size than the view. This implies that your application must be able to handle this size.

you also can set the background's color

view.setDrawingCacheBackgroundColor(Color.XY)

you can look this also up 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)

dudeldidadum
  • 1,281
  • 17
  • 27
0

Hey Hi Try this for transparent

Bitmap bitmap= Bitmap.createBitmap(255, 255, Bitmap.Config.ARGB_8888);

after that try this line (A is alpha value interval is 0-255 and 0 is fully transparent).

bitmap.eraseColor(Color.argb(AAA,RRR,GGG,BBB));

Check This

Community
  • 1
  • 1
Amar
  • 353
  • 1
  • 2
  • 13