|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 xuexiangjys([email protected]) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.xuexiang.module_test; |
| 18 | + |
| 19 | +import android.app.Activity; |
| 20 | +import android.content.Context; |
| 21 | +import android.graphics.Point; |
| 22 | +import android.view.Display; |
| 23 | +import android.view.WindowManager; |
| 24 | + |
| 25 | +import com.xuexiang.xaop.annotation.MemoryCache; |
| 26 | + |
| 27 | +/** |
| 28 | + * 这里是演示的工具类 |
| 29 | + * |
| 30 | + * @author xuexiang |
| 31 | + * @since 2021/10/6 10:51 AM |
| 32 | + */ |
| 33 | +public final class CommonUtils { |
| 34 | + |
| 35 | + private CommonUtils() { |
| 36 | + throw new UnsupportedOperationException("u can't instantiate me..."); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * 获取上下文所在的宽度 |
| 41 | + * |
| 42 | + * @param context 上下文 |
| 43 | + * @param isReal 是否是真实的尺寸 |
| 44 | + * @return 上下文所在的宽度 |
| 45 | + */ |
| 46 | + public static int getDisplayWidth(Context context, boolean isReal) { |
| 47 | + Point point = getDisplaySize(context, isReal); |
| 48 | + return point != null ? point.x : 0; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 获取上下文所在的尺寸 |
| 53 | + * |
| 54 | + * @param context 上下文 |
| 55 | + * @param isReal 是否是真实的尺寸 |
| 56 | + * @return 上下文所在的尺寸 |
| 57 | + */ |
| 58 | + @MemoryCache |
| 59 | + private static Point getDisplaySize(Context context, boolean isReal) { |
| 60 | + WindowManager windowManager; |
| 61 | + if (context instanceof Activity) { |
| 62 | + windowManager = ((Activity) context).getWindowManager(); |
| 63 | + } else { |
| 64 | + windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 65 | + } |
| 66 | + if (windowManager == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + Display display = windowManager.getDefaultDisplay(); |
| 70 | + Point point = new Point(); |
| 71 | + if (isReal) { |
| 72 | + display.getRealSize(point); |
| 73 | + } else { |
| 74 | + display.getSize(point); |
| 75 | + } |
| 76 | + return point; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +} |
0 commit comments