讀取手機長寬解析度與密度Density DPI

1.第一種讀取手機長寬解析度、手機密度、手機DPI的寫法為:
//手機的寬度(像素)
int mWidth = getResources().getDisplayMetrics().widthPixels;
//手機的高度(像素)
int mHeight = getResources().getDisplayMetrics().heightPixels;
//手機的密度
float mDensity = getResources().getDisplayMetrics().density;
//手機的DPI
float mDpi = getResources().getDisplayMetrics().densityDpi;

 

2.第二種讀取手機長寬解析度、手機密度、手機DPI的寫法為:
//先取得DisplayMetrics資訊
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
//手機的寬度(像素)
int mWidth = mDisplayMetrics.widthPixels;
//手機的高度(像素)
int mHeight = mDisplayMetrics.heightPixels;
//手機的密度
float mDensity = mDisplayMetrics.density;
//手機的DPI
int mDpi = mDisplayMetrics.densityDpi;

 

3.第三種讀取手機長寬解析度的寫法為:(與第二種類似,但是第三種為直接讀取寬高)
//手機的寬度(像素)
int mWidth = getWindowManager().getDefaultDisplay().getWidth();
//手機的高度(像素)
int mHeight = getWindowManager().getDefaultDisplay().getHeight();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s