2

I'm trying to take a screenshot of a CardView that has a shadow (elevation). However, the screenshot comes out without the shadow.
Any ideas?

This is my code:

View v = mView.RootView;
v.DrawingCacheEnabled = true;
Bitmap b = v.DrawingCache;
amitairos
  • 2,907
  • 11
  • 50
  • 84

1 Answers1

2

Shadows (Elevation In API25+) are hardware accelerated and are not available for caching at the view level.

Also if you turn off the hardware acceleration for a View (actually its parent) than the elevation effect is also disable thus not available for caching...

(aView.Parent as View).SetLayerType(LayerType.Software, null);

A View Cache Capture Example:

Bitmap CaptureView(View view)
{
    if (view.IsHardwareAccelerated)
        Toast.MakeText(ApplicationContext, "View Is Hardware Accelerated, Effects will not be captured", ToastLength.Long).Show();
    view.BuildDrawingCache();
    Bitmap bitmap = view.GetDrawingCache(false);
    Bitmap bitmapCopy = bitmap.Copy(Bitmap.Config.Argb8888, false);
    view.DestroyDrawingCache();
    return bitmapCopy;
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • 1
    So that means that I can't capture the shadow? Is there a workaround? – amitairos Feb 14 '17 at 06:41
  • @amitairos You could use the Projection APIs in 5.0+ and obtain a `Surface` of the main screen/default display and then clip that Surface to the View's parent coordinates that you wish to capture (remember the "shadow" is not in the View that defines it, it is in the parent of that View....). It really depends upon your use-case... – SushiHangover Feb 14 '17 at 07:14
  • Can you explain how to obtain a Surface of the main screen? I'm not familiar with this. – amitairos Feb 14 '17 at 08:02
  • @amitairos Xamarin has samples of the 5.0 APIs, you should review the Media Projection APIs https://developer.android.com/reference/android/media/projection/MediaProjection.html & https://github.com/xamarin/monodroid-samples/tree/master/android5.0/ScreenCapture – SushiHangover Feb 14 '17 at 08:05
  • Thanks. Can you point me out the important part of how to capture the screenshot in here :https://github.com/xamarin/monodroid-samples/blob/master/android5.0/ScreenCapture/ScreenCapture/ScreenCaptureFragment.cs I'm having trouble finding it. Thanks! – amitairos Feb 14 '17 at 08:25