androidandroid-videoviewaspect-ratio

Android VideoView crop_center


I have a RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:foregroundGravity="center"
    android:gravity="center"
    android:orientation="horizontal" >

    <VideoView
        android:id="@+id/videoViewPanel"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" 
        android:layout_centerInParent="true"/>

</RelativeLayout>

And what I need is to show video fullscreen cropped. If I could compare to ImageView, I need to show it as crop_center.

How can I make VideoView not to auto-resize video to fit center, but crop center?


Solution

  • The solution is to use TextureView instead of VideoView(SurfaceView).
    TextureView does not make any manipulations with the content to fit it ti the screen.
    Here is the code sample for the solution:

    //store the SurfaceTexture to set surface for MediaPlayer
    mTextureView.setSurfaceTextureListener(new SurfaceTextureListener() {
    @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface,
                int width, int height) {
            FullScreenActivity.this.mSurface = surface;
    
        }
    

    ....

    Surface s = new Surface(mSurface);
    mPlayer = mp;
    mp.setSurface(s);
    
    scaleVideo(mp);//<-- this function scales video to run cropped
    

    ....

    private void scaleVideo(MediaPlayer mPlayer) {
    
            LayoutParams videoParams = (LayoutParams) mTextureView
                    .getLayoutParams();
            DisplayMetrics dm = new DisplayMetrics();
            FullScreenActivity.this.getWindowManager().getDefaultDisplay()
                    .getMetrics(dm);
    
            final int height = dm.heightPixels;
            final int width = dm.widthPixels;
            int videoHeight = mPlayer.getVideoHeight();
            int videoWidth = mPlayer.getVideoWidth();
            double hRatio = 1;
    
            hRatio = (height * 1.0 / videoHeight) / (width * 1.0 / videoWidth);
            videoParams.x = (int) (hRatio <= 1 ? 0 : Math.round((-(hRatio - 1) / 2)
                    * width));
            videoParams.y = (int) (hRatio >= 1 ? 0 : Math
                    .round((((-1 / hRatio) + 1) / 2) * height));
            videoParams.width = width - videoParams.x - videoParams.x;
            videoParams.height = height - videoParams.y - videoParams.y;
            Log.e(TAG, "x:" + videoParams.x + " y:" + videoParams.y);
            mTextureView.setScaleX(1.00001f);//<-- this line enables smoothing of the picture in TextureView.
            mTextureView.requestLayout();
            mTextureView.invalidate();
    
        }