Screen Capture and Output with Transformations
In a recent Android project I am working on, I try to implement a method that captures the content on screen and project it onto a medium with certain transformations.
There have been a lot of tutorials on the screen capturing part out there. The typical way is using MediaProjectionManager
to create a screen capture intent, and then projecting the content onto a surface upon user’s approval using the createVirtualDisplay
method. Google provides a nice demo about the usage of media projection, which can be found here. In this example, the surface for projection is created through a SurfaceView
, but it can also be created from other instances like a MediaRecorder
or a ImageReader
, which can be used for video recording or taking screenshots.
In order to achieve the goal of live output with transformations, TextureView
seems to be the best choice here, as it renders contents with SurfaceTexture
, which can be used to construct a surface object, and unlike the SurfaceView
, TextureView
can be easily scaled and transformed using the setTransform
method. This approach does the trick when you need to display only a part of the screen in a sized window. There might be other better ways to do it since TextureView
is quite memory consuming and getting the proper transformation matrix can be tricky in some occasions.