android SurfaceView中播放视频 按视频的原始比例播放

    OnPreparedListener mediaPlayerOnPreparedListener = new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer arg0) {
            // 首先取得video的宽和高
            int vWidth = mediaPlayer.getVideoWidth();
            int vHeight = mediaPlayer.getVideoHeight();
            
            // 该LinearLayout的父容器 android:orientation="vertical" 必须
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutPlay);
            int lw = linearLayout.getWidth();
            int lh = linearLayout.getHeight();

            if (vWidth > lw || vHeight > lh) {
                // 如果video的宽或者高超出了当前屏幕的大小,则要进行缩放
                float wRatio = (float) vWidth / (float) lw;
                float hRatio = (float) vHeight / (float) lh;

                // 选择大的一个进行缩放
                float ratio = Math.max(wRatio, hRatio);
                vWidth = (int) Math.ceil((float) vWidth / ratio);
                vHeight = (int) Math.ceil((float) vHeight / ratio);

                // 设置surfaceView的布局参数
                LinearLayout.LayoutParams lp= new LinearLayout.LayoutParams(vWidth, vHeight);
                lp.gravity = Gravity.CENTER;
                surfaceView.setLayoutParams(lp);
            }
            // 然后开始播放视频
            mediaPlayer.start();
        }
    };
原文地址:https://www.cnblogs.com/bloodofhero/p/4920659.html