Android根据感应器设置横竖屏

释放双眼,带上耳机,听听看~!

Android根据感应器设置横竖屏
需求:
1、手机横屏模式时webview展示的内容为竖屏方向
2、手机竖屏模式时webview展示的内容为横屏方向

public class MainActivity extends AppCompatActivity {
private WebView webView;
private String path = "https://www.baidu.com/";
private OrientationEventListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
Log.i("lala","onOrientationChanged===>" + orientation);
boolean isPortrait = PolyvScreenUtils.isPortrait(MainActivity.this);
if ((orientation > -1 && orientation <= 10) || orientation >= 350 || (orientation <= 190 && orientation >= 170)) {
if (isPortrait){
PolyvScreenUtils.setLandscape(MainActivity.this);
}
} else if ((orientation <= 100 && orientation >= 80) || (orientation <= 280 && orientation >= 260)) {
if (!isPortrait){
PolyvScreenUtils.setPortrait(MainActivity.this);
}
}
}
};
listener.enable();
webView = findViewById(R.id.webView_guide);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(path);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (listener != null){
listener.disable();
}
if (webView != null) {
webView.stopLoading();
webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
webView.clearHistory();
((ViewGroup) webView.getParent()).removeView(webView);
webView.destroy();
webView = null;
}
}
}
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
/**
* 与屏幕相关的工具类
*/
public class PolyvScreenUtils {
private static int height16_9;
// 生成竖屏下w:h=16:9的高
public static int generateHeight16_9(Activity activity) {
return height16_9 != 0 ? height16_9 : (height16_9 = getNormalWH(activity)[isPortrait(activity) ? 0 : 1] * 9 / 16);
}
// 获取竖屏下w:h=16:9的高
public static int getHeight16_9() {
return height16_9;
}
// 是否竖屏
public static boolean isPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
// 是否横屏
public static boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
// 设置竖屏
public static void setPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
// 设置横屏
public static void setLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
/**
* 获取包含状态栏的屏幕宽度和高度
*
* @param activity
* @return {宽,高}
*/
public static int[] getNormalWH(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
return new int[]{dm.widthPixels, dm.heightPixels};
} else {
Point point = new Point();
WindowManager wm = activity.getWindowManager();
wm.getDefaultDisplay().getSize(point);
return new int[]{point.x, point.y};
}
}
// 重置状态栏
public static void reSetStatusBar(Activity activity) {
if (isLandscape(activity)) {
hideStatusBar(activity);
hideNavigationBar(activity);
} else {
setDecorVisible(activity);
}
}
// 隐藏状态栏
public static void hideStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = activity.getWindow().getDecorView();
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
// 恢复为不全屏状态
public static void setDecorVisible(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
View decorView = activity.getWindow().getDecorView();
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
}
}
public static void hideNavigationBar(Activity activity) {
int uiOptions = activity.getWindow().getDecorView().getSystemUiVisibility();
activity.getWindow().getDecorView().setSystemUiVisibility(uiOptions | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue / scale + 0.5f);
}
}

人已赞赏
Android文章

Android 控件获取焦点

2020-3-7 14:44:29

Android文章

Android开发使用Spinner实现四级多级联动及自定义Dialog

2020-3-7 16:22:15

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索