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
Captured Image
Also tried to capture parent layout but same result.
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.
After creating bitmap call this
tv.setDrawingCacheEnabled(false);
after this
bitmap = tv.getDrawingCache();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With