说一下视频播放跟随屏幕旋转,以及activity涉及到的周期问题

来源:互联网 发布:大数据时代联系的特点 编辑:程序博客网 时间:2024/06/06 01:05

最近好忙,唉,废话不多说,上代码

简单来说就是判断屏幕状态的值

private int orientation = ActivityInfo.SCREEN_ORIENTATION_USER;

然后设置一下监听:

MyOrientationEventListener myOrientationEventListener;

class MyOrientationEventListener extends OrientationEventListener {        public MyOrientationEventListener(Context context, int rate) {            super(context, rate);            // TODO Auto-generated constructor stub        }        @Override        public void onOrientationChanged(int arg0) {            // TODO Auto-generated method stub            L.d("onOrientationChanged生命周期实现");            L.d(orientation);            if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {                return;  //手机平放时,检测不到有效的角度            }            orientation = ActivityInfo.SCREEN_ORIENTATION_USER;            VideoViewPlayingActivity.this.setRequestedOrientation(orientation);            Display display = getWindowManager().getDefaultDisplay();            int width = display.getWidth();            int height = display.getHeight();            if (width > height) {                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;            } else {                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;            }        }    }

这个初始化和消除所应该放的周期

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_video_view_playing);        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);        mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, POWER_LOCK);        laughSQLiteOpenHelper = new LaughSQLiteOpenHelper(this);        laughSQLiteOpenHelper.getWritableDatabase();        gestureDetector = new GestureDetector(this, this);        myOrientationEventListener = new MyOrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL);        if (myOrientationEventListener.canDetectOrientation()) {            myOrientationEventListener.enable();        } else {            Toast.makeText(VideoViewPlayingActivity.this, "Can't Detect Orientation!", Toast.LENGTH_LONG).show();        }        Configuration configuration = this.getResources().getConfiguration();        int ori = configuration.orientation;        initRock();        Intent intent = this.getIntent();        //获取的数据        url_info = intent.getStringExtra("url_info");        id_info = intent.getStringExtra("id_info");        image_info = intent.getStringExtra("image_info");        title_info = intent.getStringExtra("title_info");        good_info = intent.getStringExtra("good_info");        collect_info = intent.getStringExtra("collect_info");        if (intent.getStringExtra("type_info") != null) {            type = intent.getStringExtra("type_info");        }        L.d("得到的图片的网址", image_info);        mIsHwDecode = getIntent().getBooleanExtra("isHW", false);        Thread loadThread = new Thread(new LoadMore());        loadThread.start();        initTextView();        initUI();        //获取的数据        video_palying_title.setText(intent.getStringExtra("title_info"));        initViewPager();        /**         * 开启后台事件处理线程         */        mHandlerThread = new HandlerThread("event handler thread", Process.THREAD_PRIORITY_BACKGROUND);        mHandlerThread.start();//        mEventHandler = new EventHandler(mHandlerThread.getLooper());    }
销毁的:

@Override    protected void onDestroy() {        super.onDestroy();        /**         * 退出后台事件处理线程         */        mHandlerThread.quit();        myOrientationEventListener.disable();    }
然后需要根据这个数值去处理是否横屏,怎么横屏

@Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);        L.d("onConfigurationChanged生命周期");        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 设置当前activity为竖屏            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);            //显示其他组件            video_playing_back.bringToFront();            video_down_linearlayout.setVisibility(View.VISIBLE);            video_playing_back_relativelayout.setVisibility(View.GONE);            video_view_title.setVisibility(View.GONE);            video_playing_back.setVisibility(View.VISIBLE);            video_playing_back_normal.setVisibility(View.VISIBLE);        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 设置当前activity为横屏            // 当横屏时 把除了视频以外的都隐藏            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);            //隐藏其他组件的代码            video_playing_back.bringToFront();            video_down_linearlayout.setVisibility(View.GONE);            video_playing_back_relativelayout.setVisibility(View.VISIBLE);            video_playing_back_relativelayout.bringToFront();            video_view_title.setVisibility(View.VISIBLE);            video_view_title.setText(title_info);            video_view_title.bringToFront();            video_playing_back.setVisibility(View.VISIBLE);            video_playing_back_normal.setVisibility(View.GONE);        }    }
这里注意的是全屏去除标题栏,非全屏再恢复,监听的话一定要destory







0 0
原创粉丝点击