1. 程式人生 > >Android SurfaceView內容居中顯示

Android SurfaceView內容居中顯示

chang relative gcc max-age getheight params private xtend exception

問題緣由:

最近準備封裝一個視頻播放器,我采用了SurfaceView + MediaPlayer的方式。

視頻加載顯示畫面後遇到問題:“視頻畫面扭曲,比例變形。”

問題截圖:

技術分享圖片

解決思路:

視頻尺寸會自動填充surfaceView,所以想要調整視頻尺寸 修改surfaceView就好了。

通過監聽mediaplayer的回調函數“onVideoSizeChanged” 在裏面修改surfaceView的寬高。

changeVideoSize
public void changeVideoSize() {
        int videoWidth = mediaPlayer.getVideoWidth();
        
int videoHeight = mediaPlayer.getVideoHeight(); //根據視頻尺寸去計算->視頻可以在sufaceView中放大的最大倍數。 float max; if (getResources().getConfiguration().orientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { //豎屏模式下按視頻寬度計算放大倍數值 max = Math.max((float) videoWidth / (float
) surfaceWidth,(float) videoHeight / (float) surfaceHeight); } else{ //橫屏模式下按視頻高度計算放大倍數值 max = Math.max(((float) videoWidth/(float) surfaceHeight),(float) videoHeight/(float) surfaceWidth); } //視頻寬高分別/最大倍數值 計算出放大後的視頻尺寸 videoWidth = (int) Math.ceil((float
) videoWidth / max); videoHeight = (int) Math.ceil((float) videoHeight / max); //無法直接設置視頻尺寸,將計算出的視頻尺寸設置到surfaceView 讓視頻自動填充。 surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(videoWidth, videoHeight)); }

註意:surfaceView的尺寸需要記錄保存為固定值,否則在屏幕旋轉後尺寸會變動。

效果截圖:

技術分享圖片技術分享圖片

完整代碼:

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnVideoSizeChangedListener {

    private SurfaceView surfaceView;
    private MediaPlayer mediaPlayer;
    private int surfaceWidth;
    private int surfaceHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化頁面/對象/變量/監聽事件
        setContentView(R.layout.activity_main);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceView.getHolder().addCallback(this);
    }

    private void playVideo(String url) {
        try {
            mediaPlayer.setDataSource(this, Uri.parse(url));
            mediaPlayer.prepare();
            mediaPlayer.start();} catch (IOException e) {e.printStackTrace();}
    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnVideoSizeChangedListener(this);
        mediaPlayer.setDisplay(surfaceHolder);
      
if(getResources().getConfiguration().orientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            surfaceWidth=surfaceView.getWidth();
            surfaceHeight=surfaceView.getHeight();
        }else {
            surfaceWidth=surfaceView.getHeight();
            surfaceHeight=surfaceView.getWidth();
        }

        playVideo("http://pgccdn.v.baidu.com/258500786_2854944371_20171201122626.mp4?authorization=bce-auth-v1%2Fc308a72e7b874edd9115e4614e1d62f6%2F2017-12-01T04%3A26%3A31Z%2F-1%2F%2Fcbe73c8f603a65a23a019236ad32c090ba08f587acae4011d388f153d912724f&responseCacheControl=max-age%3D8640000&responseExpires=Sun%2C+11+Mar+2018+12%3A26%3A31+GMT&xcode=0d70b526aeacd2423ca650defc16c18d96d564747874148f&time=1512191597&_=1512108201312");

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mediaPlayer.release();
        mediaPlayer=null;
    }

    public void changeVideoSize() {
        int videoWidth = mediaPlayer.getVideoWidth();
        int videoHeight = mediaPlayer.getVideoHeight();

        //根據視頻尺寸去計算->視頻可以在sufaceView中放大的最大倍數。
        float max;
        if (getResources().getConfiguration().orientation==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            //豎屏模式下按視頻寬度計算放大倍數值
            max = Math.max((float) videoWidth / (float) surfaceWidth,(float) videoHeight / (float) surfaceHeight);
        } else{
            //橫屏模式下按視頻高度計算放大倍數值
            max = Math.max(((float) videoWidth/(float) surfaceHeight),(float) videoHeight/(float) surfaceWidth);
        }

        //視頻寬高分別/最大倍數值 計算出放大後的視頻尺寸
        videoWidth = (int) Math.ceil((float) videoWidth / max);
        videoHeight = (int) Math.ceil((float) videoHeight / max);

        //無法直接設置視頻尺寸,將計算出的視頻尺寸設置到surfaceView 讓視頻自動填充。
        surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(videoWidth, videoHeight));
    }


    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        changeVideoSize();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        changeVideoSize();
    }
}

最後,在此希望大家能夠提供其它算法,應該會有更好的。

Android SurfaceView內容居中顯示