XScreen使用笔记

一.代码位置

二.引用

  1. 将JitPack存储库添加到您的构建文件中
    将其添加到存储库末尾的root build.gradle中:

    1
    2
    3
    4
    5
    6
    allprojects {
    repositories {
    ...
    maven { url 'https://jitpack.io' }
    }
    }
  2. 添加依赖项

    1
    2
    3
    4
    dependencies {
    ...
    implementation 'com.github.itemuse:XLib:Tag'
    }
  3. 初始化 Application中init

    1
    2
    3
    4
    5
    6
    7
    import cn.xy.library.XApp;
    ...
    @Override
    public void onCreate() {
    super.onCreate();
    XApp.init(this);
    }
  4. AndroidManifest.xml中添加弹出界面的activity

    1
    2
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.WRITE_SETTINGS"

三.几个API示例

  • 获取屏幕的宽度/高度

XScreen.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Return the width of screen, in pixel.
*
* @return the width of screen, in pixel
*/
public static int getScreenWidth() {
WindowManager wm = (WindowManager) getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wm.getDefaultDisplay().getRealSize(point);
} else {
wm.getDefaultDisplay().getSize(point);
}
return point.x;
}

/**
* Return the height of screen, in pixel.
*
* @return the height of screen, in pixel
*/
public static int getScreenHeight() {
WindowManager wm = (WindowManager) getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
wm.getDefaultDisplay().getRealSize(point);
} else {
wm.getDefaultDisplay().getSize(point);
}
return point.y;
}
  • 获取应用屏幕的宽度/高度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Return the application's width of screen, in pixel.
*
* @return the application's width of screen, in pixel
*/
public static int getAppScreenWidth() {
WindowManager wm = (WindowManager) getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.x;
}

/**
* Return the application's height of screen, in pixel.
*
* @return the application's height of screen, in pixel
*/
public static int getAppScreenHeight() {
WindowManager wm = (WindowManager) getApp().getSystemService(Context.WINDOW_SERVICE);
if (wm == null) return -1;
Point point = new Point();
wm.getDefaultDisplay().getSize(point);
return point.y;
}
  • 获取屏幕密度/DPI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Return the density of screen.
*
* @return the density of screen
*/
public static float getScreenDensity() {
return Resources.getSystem().getDisplayMetrics().density;
}

/**
* Return the screen density expressed as dots-per-inch.
*
* @return the screen density expressed as dots-per-inch
*/
public static int getScreenDensityDpi() {
return Resources.getSystem().getDisplayMetrics().densityDpi;
}
  • 设置屏幕为全屏/非全屏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

/**
* Set non full screen.
*
* @param activity The activity.
*/
public static void setNonFullScreen(@NonNull final Activity activity) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

/**
* Toggle full screen.
*
* @param activity The activity.
*/
public static void toggleFullScreen(@NonNull final Activity activity) {
boolean isFullScreen = isFullScreen(activity);
Window window = activity.getWindow();
if (isFullScreen) {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}

/**
* Return whether screen is full.
*
* @param activity The activity.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isFullScreen(@NonNull final Activity activity) {
int fullScreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
return (activity.getWindow().getAttributes().flags & fullScreenFlag) == fullScreenFlag;
}
  • 设置屏幕为横屏/竖屏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Set the screen to landscape.
*
* @param activity The activity.
*/
@SuppressLint("SourceLockedOrientationActivity")
public static void setLandscape(@NonNull final Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

/**
* Set the screen to portrait.
*
* @param activity The activity.
*/
@SuppressLint("SourceLockedOrientationActivity")
public static void setPortrait(@NonNull final Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

/**
* Return whether screen is landscape.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLandscape() {
return getApp().getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
}

/**
* Return whether screen is portrait.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isPortrait() {
return getApp().getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT;
}

/**
* Return the rotation of screen.
*
* @param activity The activity.
* @return the rotation of screen
*/
public static int getScreenRotation(@NonNull final Activity activity) {
switch (activity.getWindowManager().getDefaultDisplay().getRotation()) {
case Surface.ROTATION_0:
return 0;
case Surface.ROTATION_90:
return 90;
case Surface.ROTATION_180:
return 180;
case Surface.ROTATION_270:
return 270;
default:
return 0;
}
}


  • 截屏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Return the bitmap of screen.
*
* @param activity The activity.
* @return the bitmap of screen
*/
public static Bitmap screenShot(@NonNull final Activity activity) {
return screenShot(activity, false);
}

/**
* Return the bitmap of screen.
*
* @param activity The activity.
* @param isDeleteStatusBar True to delete status bar, false otherwise.
* @return the bitmap of screen
*/
public static Bitmap screenShot(@NonNull final Activity activity, boolean isDeleteStatusBar) {
View decorView = activity.getWindow().getDecorView();
Bitmap bmp = view2Bitmap(decorView);
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
if (isDeleteStatusBar) {
int statusBarHeight = XSystemUI.getStatusBarHeight();
return Bitmap.createBitmap(
bmp,
0,
statusBarHeight,
dm.widthPixels,
dm.heightPixels - statusBarHeight
);
} else {
return Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
}
}
  • 休眠

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Set the duration of sleep.
* <p>Must hold {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
*
* @param duration The duration.
*/
@RequiresPermission(WRITE_SETTINGS)
public static void setSleepDuration(final int duration) {
Settings.System.putInt(
getApp().getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT,
duration
);
}

/**
* Return the duration of sleep.
*
* @return the duration of sleep.
*/
public static int getSleepDuration() {
try {
return Settings.System.getInt(
getApp().getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT
);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return -123;
}
}

4. 测试效果