下面列出了android.view.WindowManager#getDefaultDisplay ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!(getActivity() instanceof Controller)) {
throw new IllegalStateException("Parent activity must implement the Controller interface.");
}
WindowManager windowManager = ServiceUtil.getWindowManager(getActivity());
Display display = windowManager.getDefaultDisplay();
Point displaySize = new Point();
display.getSize(displaySize);
controller = (Controller) getActivity();
camera = new Camera1Controller(TextSecurePreferences.getDirectCaptureCameraId(getContext()), displaySize.x, displaySize.y, this);
orderEnforcer = new OrderEnforcer<>(Stage.SURFACE_AVAILABLE, Stage.CAMERA_PROPERTIES_AVAILABLE);
viewModel = ViewModelProviders.of(requireActivity(), new MediaSendViewModel.Factory(requireActivity().getApplication(), new MediaRepository())).get(MediaSendViewModel.class);
}
/**
* Reads, one time, values from the camera that are needed by the app.
*/
void initFromCameraParameters(Camera camera) {
Camera.Parameters parameters = camera.getParameters();
previewFormat = parameters.getPreviewFormat();
previewFormatString = parameters.get("preview-format");
Log.d(TAG, "Default preview format: " + previewFormat + '/' + previewFormatString);
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
screenResolution = new Point(display.getWidth(), display.getHeight());
Log.d(TAG, "Screen resolution: " + screenResolution);
//图片拉伸
Point screenResolutionForCamera = new Point();
screenResolutionForCamera.x = screenResolution.x;
screenResolutionForCamera.y = screenResolution.y;
// preview size is always something like 480*320, other 320*480
if (screenResolution.x < screenResolution.y) {
screenResolutionForCamera.x = screenResolution.y;
screenResolutionForCamera.y = screenResolution.x;
}
cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
Log.d(TAG, "Camera resolution: " + screenResolution);
}
/**
* Reads, one time, values from the camera that are needed by the app.
*/
void initFromCameraParameters(Camera camera) {
Camera.Parameters parameters = camera.getParameters();
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point theScreenResolution = new Point();
display.getSize(theScreenResolution);
screenResolution = theScreenResolution;
if (rotation == Surface.ROTATION_0){
int temp = screenResolution.x;
screenResolution.x = screenResolution.y;
screenResolution.y = temp;
}
Log.i(TAG, "Screen resolution: " + screenResolution);
cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, screenResolution);
Log.i(TAG, "Camera resolution: " + cameraResolution);
}
private void setCustomDialog() {
View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_make_collections, null);
mImageView = (ImageView) mView.findViewById(R.id.make_collections_code);
go_weixin_friend = mView.findViewById(R.id.go_weixin_friend);
go_weixin_circle = mView.findViewById(R.id.go_weixin_circle);
go_qq_friend = mView.findViewById(R.id.go_qq_friend);
go_qzone = mView.findViewById(R.id.go_qzone);
iconNUmber = (TextView) mView.findViewById(R.id.icon_number);
iconType = (TextView) mView.findViewById(R.id.icon_type);
share_view = (LinearLayout) mView.findViewById(R.id.share_view);
go_weixin_friend.setOnClickListener(this);
go_weixin_circle.setOnClickListener(this);
go_qq_friend.setOnClickListener(this);
go_qzone.setOnClickListener(this);
super.setContentView(mView);
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.width = (int) (display.getWidth() * 0.78); //设置宽度
this.getWindow().setAttributes(lp);
}
public void windowDeploy() {
window = getWindow();
window.setWindowAnimations(R.style.dialogWindow);
window.setBackgroundDrawableResource(R.drawable.bg_dialog); //设置对话框背景为透明
WindowManager.LayoutParams wl = window.getAttributes();
//根据x,y坐标设置窗口需要显示的位置
wl.x = 0; //x小于0左移,大于0右移
wl.y = 0; //y小于0上移,大于0下移
// wl.alpha = 0.6f; //设置透明度
// wl.gravity = Gravity.BOTTOM; //设置重力
window.setAttributes(wl);
WindowManager m = window.getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams params = window.getAttributes();
params.width = (int) (d.getWidth() * this.widthProportion);
params.height = (int) (d.getHeight() * this.heightProportion);
window.setAttributes(params);
}
public void setReactionOrientation() {
int orientation = getResources().getConfiguration().orientation;
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
WIDTH_REACTIONS = Math.round((size.x) / 5);
HEIGHT_REACTIONS = Math.round((size.x) / 5);
REACTIONS_PADDING = Math.round(7 * (WIDTH_REACTIONS / 100));
} else {
WIDTH_REACTIONS = Math.round(((size.x) / 2) / 5);
HEIGHT_REACTIONS = Math.round(((size.x) / 2) / 5);
REACTIONS_PADDING = Math.round(7 * (WIDTH_REACTIONS / 100));
}
}
@SuppressWarnings("deprecation")
public int getSmallestScreenDimension() {
WindowManager wm = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
return Math.min(display.getHeight(), display.getWidth());
}
/**
* 获取屏幕的宽高信息
*
* @param context Context
* @return 宽高信息
*/
public static int[] getDeviceSize(Context context) {
int[] size = new int[2];
try {
int screenWidth, screenHeight;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
int rotation = display.getRotation();
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealSize(point);
screenWidth = point.x;
screenHeight = point.y;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(point);
screenWidth = point.x;
screenHeight = point.y;
} else {
screenWidth = display.getWidth();
screenHeight = display.getHeight();
}
size[0] = getNaturalWidth(rotation, screenWidth, screenHeight);
size[1] = getNaturalHeight(rotation, screenWidth, screenHeight);
} catch (Exception e) {
//context.getResources().getDisplayMetrics()这种方式获取屏幕高度不包括底部虚拟导航栏
if (context.getResources() != null) {
final DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
size[0] = displayMetrics.widthPixels;
size[1] = displayMetrics.heightPixels;
}
}
return size;
}
/**
* Returns the screen/display size.
*/
public static Point getDisplaySize(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// You can get the height & width like such:
// int width = size.x;
// int height = size.y;
return size;
}
private void initEnv(Context context) {
// 获得屏幕大小
WindowManager windowManager = ((Activity)context).getWindowManager();
Display display = windowManager.getDefaultDisplay();
DISPLAY_WINDOW_WIDTH = display.getWidth();
DISPLAY_WINDOW_HEIGHT = display.getHeight();
SLIDE_MIN_DISTANCE_X = (int)(DISPLAY_WINDOW_WIDTH * FACTOR_PORTRAIT);
SLIDE_MAX_DISTANCE_Y = (int)(DISPLAY_WINDOW_HEIGHT * FACTOR_LANDSCAPE);
//orientation = context.getResources().getConfiguration().orientation;
}
int getWindowWidth() {
WindowManager windowManager = ServiceUtil.getWindowManager(getContext());
Display display = windowManager.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
return metrics.widthPixels;
}
public static int px2dp(Context context, int px) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics displaymetrics = new DisplayMetrics();
display.getMetrics(displaymetrics);
return (int) (px / displaymetrics.density + 0.5f);
}
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size.x;
}
/**
* Gets real resolution via the new getRealMetrics API.
* */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Pair<Integer,Integer> getRealResolution(Context ctx) {
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
return new Pair<Integer, Integer>(metrics.widthPixels, metrics.heightPixels);
}
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
public static int getScreenWidth(Context c) {
if (screenWidth == 0) {
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
}
return screenWidth;
}
public static int getScreenWidth(Context c) {
if (screenWidth == 0) {
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
}
return screenWidth;
}
/**
* Get the usable screen size (everything but the navigation bar)
* http://stackoverflow.com/questions/20264268/how-to-get-height-and-width-of-navigation-bar-programmatically/29938139#29938139
* @return the size of the usable area of the screen
*/
Point getAppUsableScreenSize() {
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
/**
* Determines whether the current device is a natural portrait-oriented device
*
* <p>
* Using the current app's window to determine whether the device is a natural
* portrait-oriented device doesn't work in all scenarios, one example of this is multi-window
* mode.
* Taking a natural portrait-oriented device in multi-window mode, rotating it 90 degrees (so
* that it's in landscape), with the app open, and its window's width being smaller than its
* height. Using the app's width and height would determine that the device isn't
* naturally portrait-oriented, where in fact it is, which is why it is important to use the
* size of the device instead.
* </p>
*
* @param context Current context. Can be an {@link android.app.Application} context
* or an {@link android.app.Activity} context.
* @param rotationDegrees The device's rotation in degrees from its natural orientation.
* @return Whether the device is naturally portrait-oriented.
*/
private static boolean isNaturalPortrait(@NonNull final Context context,
final int rotationDegrees) {
final WindowManager windowManager = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
if (windowManager == null) {
return true;
}
final Display display = windowManager.getDefaultDisplay();
final Point deviceSize = new Point();
display.getRealSize(deviceSize);
final int width = deviceSize.x;
final int height = deviceSize.y;
return ((rotationDegrees == 0 || rotationDegrees == 180) && width < height) || (
(rotationDegrees == 90 || rotationDegrees == 270) && width >= height);
}
@SuppressLint("WrongConstant")
public static void lockOrientation(Activity activity) {
if (activity == null || prevOrientation != -10) {
return;
}
try {
prevOrientation = activity.getRequestedOrientation();
WindowManager manager = (WindowManager) activity.getSystemService(Activity.WINDOW_SERVICE);
if (manager != null && manager.getDefaultDisplay() != null) {
int rotation = manager.getDefaultDisplay().getRotation();
int orientation = activity.getResources().getConfiguration().orientation;
if (rotation == Surface.ROTATION_270) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
} else if (rotation == Surface.ROTATION_90) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else if (rotation == Surface.ROTATION_0) {
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
} else {
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
}
}
} catch (Exception e) {
FileLog.e(e);
}
}