-
Notifications
You must be signed in to change notification settings - Fork 4
/
ScreenUtils.java
executable file
·193 lines (174 loc) · 6.19 KB
/
ScreenUtils.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.bytesizebit.androidutils;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
/***********
* Android Utils
* Created by Shahar Barsheshet on 17/10/2015.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class ScreenUtils {
private ScreenUtils() {
}
/**
* Get screen width
*
* @param context the context
* @return screen width in pixels
*/
public static int getScreenWidthInPx(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
/**
* Get screen height
*
* @param context the context
* @return screen height in pixels
*/
public static int getScreenHeightInPx(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.heightPixels;
}
/**
* Get the screen size
*
* @param context some context
* @return ScreenSize object
*/
public static ScreenSize getScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return new ScreenSize(metrics.widthPixels, metrics.heightPixels);
}
/**
* Set transparent status bar
* MIN API 19
* should be called when onCreate() starts
*
* @param activity some activity
*/
public static void setTransparentStatusBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
activity.getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* Get status bar height
*
* @param context the context
* @return status bar height in pixels
*/
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
/**
* Check if we have status bar
*
* @param activity some activity
* @return {@code true} if we have status bar
*/
public static boolean hasStatusBar(Activity activity) {
LayoutParams params = activity.getWindow().getAttributes();
return (params.flags & LayoutParams.FLAG_FULLSCREEN) != LayoutParams.FLAG_FULLSCREEN;
}
/**
* Get the action bar height
*
* @param activity some activity
* @return action bar height in pixels
*/
public static int getActionBarHeight(Activity activity) {
TypedValue tv = new TypedValue();
if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
return TypedValue.complexToDimensionPixelSize(tv.data, activity.getResources().getDisplayMetrics());
}
return 0;
}
/**
* Take a screenshot with the status bar
*
* @param activity the activity to capture
* @return a Bitmap
*/
public static Bitmap takeScreenShotWithStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidthInPx(activity);
int height = getScreenHeightInPx(activity);
Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* Take a screenshot without the status bar
*
* @param activity the activity to capture
* @return a Bitmap
*/
public static Bitmap takeScreenShoteWithoutStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int statusBarHeight = getStatusBarHeight(activity);
int width = getScreenWidthInPx(activity);
int height = getScreenHeightInPx(activity);
Bitmap bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bp;
}
/**
* Check if the screen is locked
*
* @param context some context
* @return {@code true} if the screen is lock
*/
public static boolean isScreenLocked(Context context) {
KeyguardManager km = (KeyguardManager) context
.getSystemService(Context.KEYGUARD_SERVICE);
return km.inKeyguardRestrictedInputMode();
}
private static class ScreenSize {
private int screenWidth;
private int screenHeight;
public ScreenSize(int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
}
public int getScreenWidth() {
return screenWidth;
}
public void setScreenWidth(int screenWidth) {
this.screenWidth = screenWidth;
}
public int getScreenHeight() {
return screenHeight;
}
public void setScreenHeight(int screenHeight) {
this.screenHeight = screenHeight;
}
}
}