前言
有一定開發(fā)經(jīng)驗(yàn)的小伙伴肯定會(huì)發(fā)現(xiàn)這樣一個(gè)問題,當(dāng)我們用xml來寫布局的時(shí)候,通常用的是dp、sp碉哑。(相信大家都知道為什么這樣用)。當(dāng)我們用Java代碼來創(chuàng)建View控件時(shí)亮蒋,會(huì)發(fā)現(xiàn)方法接收的參數(shù)都是以px為單位的扣典,當(dāng)然我們不希望直接使用px的(相信大家都知道為什么不希望使用px為單位)。這個(gè)時(shí)候大家很自然的會(huì)想到轉(zhuǎn)換一下就OK啦慎玖。dp贮尖、sp與px之間有一定的轉(zhuǎn)換公式,但每用一次就寫一次這不是程序員的風(fēng)格趁怔。所以這里就總結(jié)了一個(gè)工具類湿硝,希望可以幫助到大家。
GitHub地址
https://github.com/chaohengxing/MyUtils.git
代碼
內(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;
}
}
實(shí)際上,核心內(nèi)容還是Android API里面的內(nèi)容铺浇,這里只不過是對Android API進(jìn)行了一次封裝痢畜,讓自己更容易記憶,在開發(fā)中效率更高鳍侣。
我們點(diǎn)進(jìn)去TypedValue.applyDimension();這個(gè)方法丁稀,源碼如下,源碼很清晰倚聚,相信大家一眼就能看明白线衫。
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;
}
補(bǔ)充
補(bǔ)充三個(gè)方法:
- 獲取狀態(tài)欄的高度
- 獲取當(dāng)前屏幕截圖但不包含狀態(tài)欄
- 獲取當(dāng)前屏幕截圖包含狀態(tài)欄。
這一類代碼惑折,并不需要死記硬背授账,收集好枯跑,用的時(shí)候可以快速找到即可。
/**
* 獲得狀態(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;
}
/**
* 獲取當(dāng)前屏幕截圖矗积,包含狀態(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;
}
/**
* 獲取當(dāng)前屏幕截圖全肮,不包含狀態(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;
}