旋转视频时如何避免屏幕大小崩溃

时间:2016-03-30 06:56:54

标签: android orientation android-videoview screen-orientation

我在播放视频时遇到问题。我在清单

中有以下属性的活动

android:configChanges="orientation|keyboard|keyboardHidden|screenSize"

但每次我将方向从纵向更改为横向时,屏幕尺寸都会显示为纵向。和横向相同的事情发生在屏幕上,屏幕尺寸显示的风景就像链接中显示的图形一样。 enter image description here

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bdb"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"

     android:configChanges="orientation|keyboardHidden|keyboard|screenSize" 
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   </application>

  </manifest>

MainActivity.java

package com.example.bdb;

import android.app.Activity;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams .FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    VideoView myVideoView = (VideoView)findViewById(R.id.birthdayview);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(myVideoView);
    try{
    String vidpath = "android.resource://" + getPackageName() + "/" + R.raw.birthday;
    myVideoView.setVideoURI(Uri.parse(vidpath));
    myVideoView.setMediaController(new MediaController(this));
    myVideoView.requestFocus();
    myVideoView.start();

    }
    catch (Exception e){
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT);
    }
}

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context="com.example.bdb.MainActivity" >

    <VideoView
        android:id="@+id/birthdayview"
        android:layout_width="fill_parent"
         android:layout_alignParentRight="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true"
         android:layout_height="fill_parent">

    </VideoView>

解决这个问题的任何解决方案/建议?非常感谢你:))

1 个答案:

答案 0 :(得分:1)

通过在清单中声明android:configChanges="orientation|keyboard|keyboardHidden|screenSize",您负责处理所有这些更改。但是,您尚未实现onConfigurationChanged来处理布局的配置更改。您应该手动更改VideoView

的大小
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    doLayout();
}

private void doLayout() {
    if (getResources().getConfiguration().orientation
        == Configuration.ORIENTATION_PORTRAIT) {
        // do PORTRAIT stuff
    } else {
        // do LANDSCAPE stuff
    }
}