[Android] How to make subtitles overing the video when the player view dimensions are not 16/9

Most devices don’t have an aspect ratio of 16/9, and in a lot of use-cases such as portrait mode, the app allows for PlayerView dimensions that are very far from 16/9. In this case, the player has to use only a part of the screen to display the video, while the PlayerView and SubtitleView remain adapted to the space they have in the app.

This may cause subtitles not to be hovering the video properly, which might result in weird results especially when the subtitle positioning varies along the stream.

A way to avoid this is to add some padding to the SubtitleView, to make sure the subtitle will be overing the video at all time. The horizontal or vertical padding will need to be recalculated at each layout change.

Example :

        subtitleView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
            // This block will be called every time layout changes for subtitleView.
            float aspectRatio = (float) subtitleView.getWidth() / subtitleView.getHeight();
            if (aspectRatio < (16f / 9f)) {
                float padding = subtitleView.getHeight() - 0.5f * aspectRatio * subtitleView.getHeight();
                int roundedPadding = Math.round(padding/2);
                subtitleView.setPadding(0, roundedPadding, 0, roundedPadding); // Top padding only
            } else if (aspectRatio > (16f / 9f)) {
                float padding = subtitleView.getWidth() - 0.5f * aspectRatio * subtitleView.getWidth();
                int roundedPadding = Math.round(padding);
                subtitleView.setPadding(roundedPadding, 0, roundedPadding, 0); // Horizontal padding only

(based on https://github.com/bitmovin/bitmovin-player-android-samples/tree/main/CustomUiSubtitleView)

Before (no padding)

After (with SubtitleView padding)