Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView shadow destroyed while capture image

Shadow gets destroyed while capturing image of textview.

I added text shadow programmatically using setShadowLayer.

tv.setShadowLayer(20, 0, 0, Color.BLACK);

when I capture image using

tv.buildDrawingCache(true);
tv.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
tv.setDrawingCacheEnabled(true);

and getting bitmap using

bitmap = tv.getDrawingCache();

Original Image on Screen

enter image description here

Captured Image

enter image description here

Also tried to capture parent layout but same result.

like image 996
Rahul Mandaliya Avatar asked Aug 30 '16 12:08

Rahul Mandaliya


3 Answers

The difference between the two images is that the original image is produced using a LAYER_TYPE_HARDWARE layer. When you set a shadow layer on text using this layer type, the blur is created by the graphics processor on a texture.

See this article on View layers for details.

The captured image on the other hand is created by rendering into a bitmap, which is essentially the same as using LAYER_TYPE_SOFTWARE. Since the blur is no longer created by the graphics processor, there is no guarantee that it will look the same.

You can experiment with it. Add code to force software rendering:

tv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
tv.setShadowLayer(20, 0, 0, Color.BLACK);

After you do this, the two images will look the same.

If you would like both images to look more like your original image with hardware blur, you can adjust the blur radius:

tv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
tv.setShadowLayer(8, 0, 0, Color.BLACK);

If you would like to get results exactly like the original, by capturing a bitmap of the hardware-rendered image, it must be possible but I don't know an easy way. You might have to rewrite the app using OpenGL to get access to the underlying graphics buffers.

like image 78
x-code Avatar answered Nov 19 '22 22:11

x-code


After creating bitmap call this

tv.setDrawingCacheEnabled(false);

after this

bitmap = tv.getDrawingCache();
like image 1
Piyush Avatar answered Nov 19 '22 21:11

Piyush


try this code it save what actually appears on the screen.

Bitmap bitmap = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            textView.draw(canvas);
            FileOutputStream var5;
            try {
                var5 = new FileOutputStream(new File(Environment.getExternalStorageDirectory(),"temp.png"));
                bitmap.compress(CompressFormat.PNG, 100, var5);
                var5.flush();
                var5.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

I have successfully tested.

like image 1
prakash ubhadiya Avatar answered Nov 19 '22 22:11

prakash ubhadiya