本文主要是介绍Android获取屏幕分辨率的三种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、在activity中
1、DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
2、DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
二、在service中
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
三、在非activity中,传一个Context进来调用
1、public ImageGalleryAdapter(Context c) {
myContext = c;
DisplayMetrics dm = c.getResources().getDisplayMetrics();
int screenHeight = dm.heightPixels;
}
2、public class BaseTools {
public static int getWindowWidth(Context context){
// 获取屏幕分辨率
WindowManager wm = (WindowManager) (context.getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int mScreenWidth = dm.widthPixels;
return mScreenWidth;
}
public static int getWindowHeigh(Context context){
// 获取屏幕分辨率
WindowManager wm = (WindowManager) (context.getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int mScreenHeigh = dm.heightPixels;
return mScreenHeigh;
}
}
这篇关于Android获取屏幕分辨率的三种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!