Android切换从纵向到横向的VideoView方向,无需水平拉伸视频

时间:2014-10-27 15:04:19

标签: android video

我有一个从外部存储播放视频的应用程序。这些视频在纵向模式下看起来很好,但它们在横向模式下拉伸。有没有简单的方法来设置横向模式视频播放宽度以适应视频?

这是我的视频播放活动:

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>

1 个答案:

答案 0 :(得分:2)

在.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>