Android获取窗体信息的Util方法

时间:2022-06-05 04:36:05

Android获取窗体信息的Util方法,方法很简单,这里就不多废话了,直接上代码

?
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
package com.wangyi.tools;
 
import android.app.Activity;
import android.util.DisplayMetrics;
 
public class DisplayUtils {
  
  private static DisplayUtils instance;
  private Activity mActivity;
  private DisplayUtils(Activity mActivity){
    this.mActivity=mActivity;
  }
  public static DisplayUtils getInstance(Activity mActivity){
    if(instance==null){
      instance=new DisplayUtils(mActivity);
    }
    return instance;
  }
  public final int[] getScreenSize(){
    int[] size=new int[2];
    DisplayMetrics dm = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    size[0]=dm.widthPixels;
    size[1]=dm.heightPixels;
    return size;
  }
  public final static int getWindowWidth(Activity mActivity) {
    DisplayMetrics dm = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
  }
 
  public final static int getWindowHeight(Activity mActivity) {
    DisplayMetrics dm = new DisplayMetrics();
    mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels;
  }
}

以上所述就是本文的全部内容了,希望大家能够喜欢。