效果如图所示 播放器随着屏幕自动旋转而变化 这个效果不难 遇到的问题是 在播放器右下角还有一个按钮 按钮点击就会改变播放器的大小 也要发生变化
所以在监听自动旋转的时候 按钮就失效了
我第一个想法就是点击的时候把监听给取消(屏幕旋转与否肯定是有监听的)
在适合的位置给开启
比如 一开始是 竖屏
我点击按钮 监听取消(不取消 你就算切换了横屏 也马上会切换回来)
然后你的屏幕横屏了 你在吧监听打开
问题来了 当你的监听都被取消 你怎么知道你的屏幕横屏了?
我当时就把这个想法给否决了(后面也是重新接收这个想法才实现)
后面在下班回家路上 突然想起来 我为什么不能注册两个监听呢 一个来控制屏幕旋转
一个来控制 控制那个屏幕旋转是否开启
有了这个想法 就在今天给实现了
下面上代码 代码来看
XML
VideoView1是一个和VideoView一样的空间只不过里面是
public class VideoView1 extends TextureView implements MediaPlayerControl
这样的 SurfaceView被换了 至于区别 自行百度
package com.daemon.viewlp;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private int screenWidth;
private int screenHeight;
private com.daemon.viewlp.VideoView1 sf_play;
private LinearLayout ll_playcontroller;
private ImageView iv_stretch;
private ImageView iv_recordcourse_start;
private boolean sensor_flag=true;
private boolean stretch_flag=true;
private SensorManager sm;
private OrientationSensorListener listener;
private Sensor sensor;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 888:
int orientation = msg.arg1;
if (orientation>45&&orientation<135) {
}else if (orientation>135&&orientation<225){
}else if (orientation>225&&orientation<315){
System.out.println("切换成横屏");
MainActivity.this.setRequestedOrientation(0);
sensor_flag = false;
stretch_flag=false;
}else if ((orientation>315&&orientation<360)||(orientation>0&&orientation<45)){
System.out.println("切换成竖屏");
MainActivity.this.setRequestedOrientation(1);
sensor_flag = true;
stretch_flag=true;
}
break;
default:
break;
}
};
};
private SensorManager sm1;
private Sensor sensor1;
private OrientationSensorListener2 listener1;
public boolean record_flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
setContentView(R.layout.activity_main);
initView();
//注册重力感应器 屏幕旋转
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener = new OrientationSensorListener(handler);
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
//根据 旋转之后 点击 符合之后 激活sm
sm1 = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensor1 = sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener1 = new OrientationSensorListener2();
sm1.registerListener(listener1, sensor1, SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onResume() {
//sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
super.onResume();
}
@SuppressLint("NewApi")
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("screenHeight "+screenHeight);
System.out.println("screenWidth "+screenWidth);
if (stretch_flag) {
//切换成竖屏
LayoutParams params1 = new RelativeLayout.LayoutParams(screenHeight,DensityUtil.dip2px(this, 160));
sf_play.setLayoutParams(params1);
Toast.makeText(getApplicationContext(), "竖屏", 0).show();
}
else {
//切换成横屏
LayoutParams params1 = new RelativeLayout.LayoutParams(screenHeight,screenWidth);
sf_play.setLayoutParams(params1);
Toast.makeText(getApplicationContext(), "横屏", 0).show();
}
}
private void initView() {
sf_play = (VideoView1) findViewById(R.id.sf_play);
ll_playcontroller=(LinearLayout)findViewById(R.id.ll_playcontroller);
ll_playcontroller.getBackground().setAlpha(150);
iv_stretch = (ImageView) findViewById(R.id.iv_stretch);
iv_recordcourse_start = (ImageView) findViewById(R.id.iv_recordcourse_start);
iv_stretch.setOnClickListener(this);
iv_recordcourse_start.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.iv_stretch:
sm.unregisterListener(listener);
Toast.makeText(getApplicationContext(), "点击切换屏幕", 0).show();
if (stretch_flag) {
stretch_flag = false;
//切换成横屏
MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
stretch_flag = true;
//切换成竖屏
MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
}
}
/**
* 重力感应监听者
*/
public class OrientationSensorListener implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public static final int ORIENTATION_UNKNOWN = -1;
private Handler rotateHandler;
public OrientationSensorListener(Handler handler) {
rotateHandler = handler;
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
if(sensor_flag!=stretch_flag) //只有两个不相同才开始监听行为
{
float[] values = event.values;
int orientation = ORIENTATION_UNKNOWN;
float X = -values[_DATA_X];
float Y = -values[_DATA_Y];
float Z = -values[_DATA_Z];
float magnitude = X*X + Y*Y;
// Don't trust the angle if the magnitude is small compared to the y value
if (magnitude * 4 >= Z*Z) {
//屏幕旋转时
float OneEightyOverPi = 57.29577957855f;
float angle = (float)Math.atan2(-Y, X) * OneEightyOverPi;
orientation = 90 - (int)Math.round(angle);
// normalize to 0 - 359 range
while (orientation >= 360) {
orientation -= 360;
}
while (orientation < 0) {
orientation += 360;
}
}
if (rotateHandler!=null) {
rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();
}
}
}
}
public class OrientationSensorListener2 implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public static final int ORIENTATION_UNKNOWN = -1;
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int orientation = ORIENTATION_UNKNOWN;
float X = -values[_DATA_X];
float Y = -values[_DATA_Y];
float Z = -values[_DATA_Z];
/**
* 这一段据说是 android源码里面拿出来的计算 屏幕旋转的 不懂 先留着 万一以后懂了呢
*/
float magnitude = X*X + Y*Y;
// Don't trust the angle if the magnitude is small compared to the y value
if (magnitude * 4 >= Z*Z) {
//屏幕旋转时
float OneEightyOverPi = 57.29577957855f;
float angle = (float)Math.atan2(-Y, X) * OneEightyOverPi;
orientation = 90 - (int)Math.round(angle);
// normalize to 0 - 359 range
while (orientation >= 360) {
orientation -= 360;
}
while (orientation < 0) {
orientation += 360;
}
}
if (orientation>225&&orientation<315){ //横屏
sensor_flag = false;
}else if ((orientation>315&&orientation<360)||(orientation>0&&orientation<45)){ //竖屏
sensor_flag = true;
}
if(stretch_flag== sensor_flag){ //点击变成横屏 屏幕 也转横屏 激活
System.out.println("激活");
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
}
}
}
}
代码不复杂该有的注释也有
至于那段 判断屏幕旋转的 据说是源码里面搞出来
我也看了官方的这一块的内容 现在还不是很懂 什么加速传感器 三个参数 x y z 也试图改成自己理解的 总是有点问题 没完全实现 这段代码不长 可以先用着(已经在的轮子)
要demo源码的可以 附上地址 (劳动成果 分数也是要的)
http://download.csdn.net/detail/liubo080852/8446445
最后来张 金木-----