背景
开发中,有时需要获取当前屏幕的方向,所以单独写了个demo,方便大家参考。
Demo源代码:https://gitee.com/hspbc/screenOrientation.git
主要代码:public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private CameraOrientationListener orientationListener;
private TextView txt;
/**
* 当前屏幕旋转角度
*/
private int mOrientation = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.textView);
orientationListener = new CameraOrientationListener(this);
orientationListener.enable();
startOrientationChangeListener();
}
/**
* 启动屏幕朝向改变监听函数 用于在屏幕横竖屏切换时改变保存的图片的方向
*/
private void startOrientationChangeListener() {
OrientationEventListener mOrEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int rotation) {
Log.i(TAG, "当前屏幕手持角度方法:" + rotation + "°");
if (((rotation >= 0) && (rotation 315)) {
rotation = 0;
} else if ((rotation > 45) && (rotation 135) && (rotation 225) && (rotation 315 || degrees 45 && degrees 135 && degrees 225 && degrees
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?