android手机全面屏适配
程序员文章站
2022-03-27 11:42:10
package jp.co.cybird.android.magicaldays;import android.app.Activity;import android.app.Application;import android.content.Context;import android.graphics.Canvas;import android.graphics.Point;import android.os.Build;import android.os.Bundle;import...
package jp.co.cybird.android.magicaldays;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.opd2c.magicaldays.bs.R;
public class GameActivityNew implements Application.ActivityLifecycleCallbacks {
//标准屏宽比
private float mStandardScreenAspectRatio = 1.68f;
//屏幕分辨率
private int screenWidth;
private int screenHeight;
private boolean mHasCheckAllScreen;
private boolean mIsAllScreenDevice;
//判断是否为游戏的主activity
private Boolean IsMainActivity(final Activity activity) {
String className = activity.getLocalClassName();
if (className.endsWith("GameActivity")){
return true;
}
return false;
}
//判断是否是全面屏
private boolean IsAllScreenDevice(Activity context) {
if (mHasCheckAllScreen) {
return mIsAllScreenDevice;
}
mHasCheckAllScreen = true;
mIsAllScreenDevice = false;
// 低于 API 21的,都不会是全面屏。。。
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
Display display = windowManager.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
float width, height;
if (point.x < point.y) {
width = point.x;
height = point.y;
}
else{
width = point.y;
height = point.x;
}
if (height / width >= 1.97f) {
mIsAllScreenDevice = true;
}
}
return mIsAllScreenDevice;
}
//隐藏下方手机的返回按钮界面
private void HideBottomKey(Activity activity){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.d("GameActivityNew","KITKAT");
View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
//允许应用显示全面屏
private void AllowFullScreenDisplay(Activity activity){
// 隐藏标题
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = activity.getWindow();
// 如果需要设置全屏
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
Log.d("GameActivityNew","JELLY_BEAN");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams lp = window.getAttributes();
/**
* 官方Android 9 支持最新的全面屏, https://developer.android.com/about/versions/pie/android-9.0
* LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT 仅仅当系统提供的bar完全包含了刘海区时,才允许window扩展到刘海区,否则window不会和刘海区重叠
* LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES 允许window扩展到刘海区
* LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER 不允许window扩展到刘海区
*/
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(lp);
Log.d("GameActivityNew","P");
}
}
@Override
public void onActivityCreated(final Activity activity, Bundle savedInstanceState){
if(IsMainActivity(activity)){
HideBottomKey(activity);
AllowFullScreenDisplay(activity);
if(IsAllScreenDevice(activity)){
//获取手机屏幕分辨率(宽:dm.widthPixels,高:dm.heightPixels)
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
Log.d("GameActivityNew", "widthPixels=" + screenWidth);
Log.d("GameActivityNew", "heightPixels=" + screenHeight);
//获取游戏主activity的根layout对象,方便在游戏在屏幕上下方绘制图片,做全面屏适配
final ViewGroup vg=(ViewGroup)activity.getWindow().getDecorView();
// activity.getWindow().getDecorView().post(new Runnable() {
// @Override
// public void run() {
// Log.d("GameActivityNewh", Integer.toString( vg.getChildAt(1).getHeight()));
// }
// });
//顶部图片
ImageView img = new ImageView(activity);
//设置图片的长宽
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(screenWidth, 196);
img.setLayoutParams(layoutParams);
//设置图片的icon
img.setImageResource(R.drawable.base);
//创建MarginLayoutParams,参数就填imgView的LayoutParams就行(设置图片的偏移坐标)
ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(img.getLayoutParams());
/*
* 给这个MarginLayoutParams添加偏移参数 四个参数(一般只需要顶部和左边就能确定View的位置,后两个参数添0即可):
* left:第一个参数,是View距离屏幕左边的距离
* top:第二个参数,是Veiw距离屏幕顶部的距离
* right:第三个参数,是View距离屏幕右边的距离
* bottom: 第三个参数,是View距离屏幕底部的距离
* */
margin.setMargins(0, 0, 0, 0);
RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(margin);
img.setLayoutParams(layoutParams3);
vg.addView(img);
//底部图片
ImageView img2 = new ImageView(activity);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(screenWidth, 196);
img2.setLayoutParams(layoutParams2);
img2.setImageResource(R.drawable.base1);
ViewGroup.MarginLayoutParams margin2 = new ViewGroup.MarginLayoutParams(img2.getLayoutParams());
margin2.setMargins(0, screenHeight - 94 , 0, 0);
RelativeLayout.LayoutParams layoutParams4 = new RelativeLayout.LayoutParams(margin2);
img2.setLayoutParams(layoutParams4);
vg.addView(img2);
}
}
}
@Override
public void onActivityStarted(Activity activity){
}
@Override
public void onActivityResumed(Activity activity){
}
@Override
public void onActivityPaused(Activity activity){
}
@Override
public void onActivityStopped(Activity activity){
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState){
}
@Override
public void onActivityDestroyed(Activity activity){
}
}
本文地址:https://blog.csdn.net/qq_41165567/article/details/108128581
上一篇: 在Flutter中给widget添加动画
下一篇: 周娥皇是什么人?她为什么会被气死