日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

android屏幕密度高度,Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)...

發布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android屏幕密度高度,Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們需要獲取Android手機或Pad的屏幕的物理尺寸,以便于界面的設計或是其他功能的實現。下面就分享一下Android中常用的一些輔助方法:

獲取屏幕高度:

/**

* 獲得屏幕高度

* @param context

* @return

* by Hankkin at:2015-10-07 21:15:59

*/

public static int getScreenWidth(Context context) {

WindowManager wm = (WindowManager) context

.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics outMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(outMetrics);

return outMetrics.widthPixels;

}

獲取屏幕寬度:

/**

* 獲得屏幕寬度

* @param context

* @return

* by Hankkin at:2015-10-07 21:16:13

*/

public static int getScreenHeight(Context context) {

WindowManager wm = (WindowManager) context

.getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics outMetrics = new DisplayMetrics();

wm.getDefaultDisplay().getMetrics(outMetrics);

return outMetrics.heightPixels;

}

獲取屏幕密度:

/**

* 獲取屏幕密度

* @param context

* @return

* by Hankkin at:2015-10-07 21:16:29

*/

public static float getScreenDensity(Context context) {

return context.getResources().getDisplayMetrics().density;

}

dip轉px:

/**

* dip轉px像素

* @param context

* @param px

* @return

* by Hankkin at:2015-10-07 21:16:43

*/

public static int dip2px(Context context, float px) {

final float scale = getScreenDensity(context);

return (int) (px * scale + 0.5);

}

獲取狀態欄高度:

/**

* 獲得狀態欄的高度

* @param context

* @return

* by Hankkin at:2015-10-07 21:16:43

*/

public static int getStatusHeight(Context context) {

int statusHeight = -1;

try {

Class> clazz = Class.forName("com.android.internal.R$dimen");

Object object = clazz.newInstance();

int height = Integer.parseInt(clazz.getField("status_bar_height")

.get(object).toString());

statusHeight = context.getResources().getDimensionPixelSize(height);

} catch (Exception e) {

e.printStackTrace();

}

return statusHeight;

}

獲取屏幕當前截圖:

/**

* 獲取當前屏幕截圖,包含狀態欄

* @param activity

* @return

* by Hankkin at:2015-10-07 21:16:43

*/

public static Bitmap snapShotWithStatusBar(Activity activity) {

View view = activity.getWindow().getDecorView();

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bmp = view.getDrawingCache();

int width = getScreenWidth(activity);

int height = getScreenHeight(activity);

Bitmap bp = null;

bp = Bitmap.createBitmap(bmp, 0, 0, width, height);

view.destroyDrawingCache();

return bp;

}

/**

* 獲取當前屏幕截圖,不包含狀態欄

* @param activity

* @return

* by Hankkin at:2015-10-07 21:16:43

*/

public static Bitmap snapShotWithoutStatusBar(Activity activity) {

View view = activity.getWindow().getDecorView();

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bmp = view.getDrawingCache();

Rect frame = new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;

int width = getScreenWidth(activity);

int height = getScreenHeight(activity);

Bitmap bp = null;

bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height

- statusBarHeight);

view.destroyDrawingCache();

return bp;

}

以上所述是本文給大家介紹的Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖),希望對大家也是幫助,更多信息登錄腳本之家網站了解更多信息。

總結

以上是生活随笔為你收集整理的android屏幕密度高度,Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。