2

In my fragment I put a video. I didn't want to restart the activity when the screen is rotated, so I added android:configChanges="orientation|screenSize". But now, when I rotate the screen, the video size is very strange. See these images:

This when I rotate to landscape: enter image description here This when I rotate to portrait: enter image description here This happens only when in my manifest there is android:configChanges="orientation|screenSize", if I remove it, it doesn't happen.

Here is my video layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
</LinearLayout>
Curio
  • 1,331
  • 2
  • 14
  • 34

1 Answers1

1

When you add android:configChanges="orientation|screenSize" and avoid the restart the system will call onConfigurationChanged() on your activity.

In here you can handle anything you want to that might be important for your application, for example resizing the video view for the new orientation.

If you don't do this the system will use the old view dimensions which no longer match the display.

See this answer for an example of how to resize a video view: https://stackoverflow.com/a/14113271/334402

Google's Camerea2Vdieo example (https://github.com/googlesamples/android-Camera2Video) shows one approach to resize a video when a surface has changed - they do not stop the activity restarting but the principles are the same, in that you detect the 'window' has changed and then measure the new size and reset accordingly:

Detecting the change using a SurfaceTextureListener:

/**
     * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a
     * {@link TextureView}.
     */
    private TextureView.SurfaceTextureListener mSurfaceTextureListener
            = new TextureView.SurfaceTextureListener() {

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
                                              int width, int height) {
            openCamera(width, height);
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
                                                int width, int height) {
            configureTransform(width, height);
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
            return true;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        }

    };

The transformation of the texture is done in configureTransform which you can see at the link above.

Community
  • 1
  • 1
Mick
  • 24,231
  • 1
  • 54
  • 120
  • I wrote that code but if I rotate the screen more times, it happens the same thing. How can I solve it? – Curio Apr 27 '17 at 10:32
  • Can you show your code and it will be easier to say - as an example for the preview texture with the Google Camera2Basic code, the code looks for changes to the surface texture preview and then adjusts the size. I'll add a link and exact to the answer. – Mick Apr 27 '17 at 18:14
  • The system didn't send my comment yesterday. I solved it with a thread. – Curio Apr 28 '17 at 21:06