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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 单位转换工具,Android单位转换----常用单位转换工具类

發(fā)布時間:2024/4/11 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 单位转换工具,Android单位转换----常用单位转换工具类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

有一定開發(fā)經(jīng)驗的小伙伴肯定會發(fā)現(xiàn)這樣一個問題,當我們用xml來寫布局的時候,通常用的是dp、sp。(相信大家都知道為什么這樣用)。當我們用Java代碼來創(chuàng)建View控件時,會發(fā)現(xiàn)方法接收的參數(shù)都是以px為單位的,當然我們不希望直接使用px的(相信大家都知道為什么不希望使用px為單位)。這個時候大家很自然的會想到轉(zhuǎn)換一下就OK啦。dp、sp與px之間有一定的轉(zhuǎn)換公式,但每用一次就寫一次這不是程序員的風格。所以這里就總結(jié)了一個工具類,希望可以幫助到大家。

GitHub地址

代碼

內(nèi)容比較簡單,話不多說,直接上代碼

/**

* 常用單位轉(zhuǎn)換的工具類

*/

public class DensityUtils {

private DensityUtils() {

}

/**

* dp轉(zhuǎn)px

*

* @param context

* @return

*/

public static int dp2px(Context context, float dpVal) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources()

.getDisplayMetrics());

}

/**

* sp轉(zhuǎn)px

*

* @param context

* @return

*/

public static int sp2px(Context context, float spVal) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources()

.getDisplayMetrics());

}

/**

* px轉(zhuǎn)dp

*

* @param context

* @param pxVal

* @return

*/

public static float px2dp(Context context, float pxVal) {

final float scale = context.getResources().getDisplayMetrics().density;

return (pxVal / scale);

}

/**

* px轉(zhuǎn)sp

*

* @param pxVal

* @return

*/

public static float px2sp(Context context, float pxVal) {

return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);

}

/**

* 得到屏幕寬度

*

* @param context

* @return

*/

public static int getDisplayWidth(Context context) {

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

}

/**

* 得到屏幕高度

*

* @param context

* @return

*/

public static int getDisplayHeight(Context context) {

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

}

}

實際上,核心內(nèi)容還是Android API里面的內(nèi)容,這里只不過是對Android API進行了一次封裝,讓自己更容易記憶,在開發(fā)中效率更高。

我們點進去TypedValue.applyDimension();這個方法,源碼如下,源碼很清晰,相信大家一眼就能看明白。

public static float applyDimension(int unit, float value,

DisplayMetrics metrics)

{

switch (unit) {

case COMPLEX_UNIT_PX:

return value;

case COMPLEX_UNIT_DIP:

return value * metrics.density;

case COMPLEX_UNIT_SP:

return value * metrics.scaledDensity;

case COMPLEX_UNIT_PT:

return value * metrics.xdpi * (1.0f/72);

case COMPLEX_UNIT_IN:

return value * metrics.xdpi;

case COMPLEX_UNIT_MM:

return value * metrics.xdpi * (1.0f/25.4f);

}

return 0;

}

補充

補充三個方法:

獲取狀態(tài)欄的高度

獲取當前屏幕截圖但不包含狀態(tài)欄

獲取當前屏幕截圖包含狀態(tài)欄。

這一類代碼,并不需要死記硬背,收集好,用的時候可以快速找到即可。

/**

* 獲得狀態(tài)欄的高度

*

* @param context

* @return

*/

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;

}

/**

* 獲取當前屏幕截圖,包含狀態(tài)欄

*

* @param activity

* @return

*/

public static Bitmap snapShotWithStatusBar(Activity activity) {

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

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bmp = view.getDrawingCache();

int width = getDisplayWidth(activity);

int height = getDisplayHeight(activity);

Bitmap bp = null;

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

view.destroyDrawingCache();

return bp;

}

/**

* 獲取當前屏幕截圖,不包含狀態(tài)欄

*

* @param activity

* @return

*/

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 = getDisplayWidth(activity);

int height = getDisplayHeight(activity);

Bitmap bp = null;

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

view.destroyDrawingCache();

return bp;

}

總結(jié)

以上是生活随笔為你收集整理的android 单位转换工具,Android单位转换----常用单位转换工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。