簡體   English   中英

Android將VideoView方向從縱向切換為橫向,而無需水平拉伸視頻

[英]Android switch VideoView orientation from Portrait to Landscape without stretching the video horizontally

我有一個從外部存儲播放視頻的應用程序。 視頻在縱向模式下看起來不錯,但在橫向模式下可以拉伸。 有什么簡單的方法可以設置橫向模式的視頻播放寬度以適合視頻?

這是我的視頻播放活動:

public class PlayVideoActivity extends Activity implements OnCompletionListener {

    private VideoView video;
    private MediaController controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_play_video);
        video = (VideoView) findViewById(R.id.vwPlayVideo);
        controller = new MediaController(this);

        video.setOnCompletionListener(this);


        Bundle extras = getIntent().getExtras();
        if(extras != null){
            String videoPath = extras.getString("videoPath");
            video.setVideoPath(videoPath);
            controller.setMediaPlayer(video);
            video.setMediaController(controller);
            video.requestFocus();
            video.start();

            if(savedInstanceState != null){
                video.seekTo(savedInstanceState.getInt("currentPos"));
            }
        }   
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        finish();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if(video.isPlaying()){
            outState.putInt("currentPos", video.getCurrentPosition());
        }   
        super.onSaveInstanceState(outState);
    }

}

編輯:activity_play_video.xml:

<?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"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/vwPlayVideo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

在您的.xml中,嘗試以下操作:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <VideoView
        android:id="@+id/vwPlayVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" />

</LinearLayout>

這不會改變視頻比率。 要使VideoView充滿整個屏幕,請嘗試以下操作:

<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

     <VideoView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:alignParentTop="true"
          android:alignParentLeft="true"
          android:alignParentRight="true"
          android:alignParentBottom="true"/>

</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM