0

I'm an iOS developer porting an iOS app to Android, so bear with me.I have a VideoView that is acting as a "video background". Everything about it is working correctly - except for in portrait view and on orientation change (to portrait view). In the AndroidManifest file, I have my activity setup so that screen orientations do not trigger a layout refresh:

android:configChanges="orientation|screenSize"

This is working correctly; what isn't working correctly is the aspect ratio of the video being played in the VideoView. The video is skewed to fit within the portrait bounds instead of cropping out the left and right sides to maintain aspect ratio. How can I force the VideoView to "aspect-fill" the video in it's container? I should also mention that this VideoView is a subview of a ConstraintView. Here is the applicable XML:

<VideoView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/videoView2"
    app:layout_constraintBottom_toBottomOf="@+id/imageView3"
    app:layout_constraintLeft_toLeftOf="@+id/imageView3"
    app:layout_constraintRight_toRightOf="@+id/imageView3"
    app:layout_constraintTop_toTopOf="@+id/imageView4"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"/>
Solsma Dev
  • 481
  • 6
  • 22

1 Answers1

0

You can check for the orientation programatically after the orientation change (through onConfigurationChanged()) and set your videoView scale type accordingly to what you want.

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        // set video view configurations to what you want on landscape
    }else{
        // set video view configurations to what you want on portrait
    }
}

About scaling the video view, that's another issue. Check this answers for more info about it: https://stackoverflow.com/a/7225469/4244598

https://stackoverflow.com/a/12335916/4244598

http://blog.kasenlam.com/2012/02/android-how-to-stretch-video-to-fill.html

Community
  • 1
  • 1
Natan
  • 1,867
  • 13
  • 24
  • I have a listener for `onConfigurationChanged()` - what code would change the scaling of the VideoView? – Solsma Dev Feb 03 '17 at 15:30
  • That third link is exactly the approach I tried, but my base layout is a ConstraintLayout and I think that is affecting the RelativeLayout recommended in that approach. – Solsma Dev Feb 03 '17 at 15:45
  • Do you really need the constraint layout? I know it's more familiar for you because of you iOS background, but maybe you can achieve the same behaviour with another type of container. – Natan Feb 03 '17 at 15:47
  • Unfortunately I believe I do - to mimic the look of our iOS app accurately. We have a 6 button grid that scales in a unique manner that I don't think is accomplishable using another container. – Solsma Dev Feb 03 '17 at 15:49
  • Have you tried linear layout with weights? or maybe percent relative layout? https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html – Natan Feb 03 '17 at 15:50
  • I have not - that could possibly be able to accomplish the same thing. Another approach I considered is a RelativeLayout base layout to hold the VideoView and then a nested ConstraintLayout over the video view to hold the existing UI. – Solsma Dev Feb 03 '17 at 15:52