Android获取屏幕高宽

四种方式:三种方式通过WindowManager来获取,另一种通过Resources来获取。获取屏幕高宽方式大同小异,主要是获取到屏幕的DisplayMetrics。测试环境Android studio

The first:通过WindowManager来获取,个人建议使用

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class ScreenUtil {

    private static ScreenUtil mInstance = null;
    private final int mWidth, mHeight;

    public static ScreenUtil getInstance(Context context){
        if (null == mInstance){
            synchronized (ScreenUtil.class){
                if (null == mInstance){
                    mInstance = new ScreenUtil(context);
                }
            }
        }
        return mInstance;
    }

    private ScreenUtil(Context context){
        //获取系统window服务
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //获取屏幕参数
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        mWidth = metrics.widthPixels;
        mHeight = metrics.heightPixels;
    }

    public int getWidth(){
        return mWidth;
    }

    public int getHeight(){
        return mHeight;
    }
}

The second:也是通过WindowManager来获取,与第一种方式不同的是获取WindowManager的方式

import android.app.Activity;
import android.util.DisplayMetrics;

public class ScreenUtil {

    private static ScreenUtil mInstance = null;
    private final int mWidth, mHeight;

    public static ScreenUtil getInstance(Activity activity){
        if (null == mInstance){
            synchronized (ScreenUtil.class){
                if (null == mInstance){
                    mInstance = new ScreenUtil(activity);
                }
            }
        }
        return mInstance;
    }

    private ScreenUtil(Activity activity){
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        mWidth = metrics.widthPixels;
        mHeight = metrics.heightPixels;
    }

    public int getWidth(){
        return mWidth;
    }

    public int getHeight(){
        return mHeight;
    }
}

The third:获取屏幕的默认分辨率,方法已被弃用,不建议使用

import android.app.Activity;
import android.view.Display;

public class ScreenUtil {

    private static ScreenUtil mInstance = null;
    private final int mWidth, mHeight;

    public static ScreenUtil getInstance(Activity activity){
        if (null == mInstance){
            synchronized (ScreenUtil.class){
                if (null == mInstance){
                    mInstance = new ScreenUtil(activity);
                }
            }
        }
        return mInstance;
    }

    private ScreenUtil(Activity activity){
        Display display = activity.getWindowManager().getDefaultDisplay();
        mWidth = display.getWidth();
        mHeight = display.getHeight();
    }

    public int getWidth(){
        return mWidth;
    }

    public int getHeight(){
        return mHeight;
    }
}

The fourth:通过Resources获取


import android.content.Context;
import android.util.DisplayMetrics;

public class ScreenUtil {

    private static ScreenUtil mInstance = null;
    private final int mWidth, mHeight;

    public static ScreenUtil getInstance(Context context){
        if (null == mInstance){
            synchronized (ScreenUtil.class){
                if (null == mInstance){
                    mInstance = new ScreenUtil(context);
                }
            }
        }
        return mInstance;
    }

    private ScreenUtil(Context context){
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        mWidth = metrics.widthPixels;
        mHeight = metrics.heightPixels;
    }

    public int getWidth(){
        return mWidth;
    }

    public int getHeight(){
        return mHeight;
    }
}

个人笔记,多多指点。。。

猜你喜欢

转载自blog.csdn.net/qq_54087555/article/details/126884811