import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
public class DensityUtils {
private static final float DOT_FIVE = 0.5f;
/**
* dip to px
*
* @param context
* @param dip
* @return
*/
public static int dip2px(Context context, float dip) {
float density = getDensity(context);
return (int) (dip * density + DensityUtils.DOT_FIVE);
}
/**
* px to dip
*
* @param context
* @param px
* @return
*/
public static int px2dip(Context context, float px) {
float density = getDensity(context);
return (int) (px / density + DOT_FIVE);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @param pxValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param spValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
private static DisplayMetrics sDisplayMetrics;
/**
* get screen width
*
* @param context
* @return
*/
public static int getDisplayWidth(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.widthPixels;
}
/**
* get screen height
*
* @param context
* @return
*/
public static int getDisplayHeight(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.heightPixels;
}
/**
* get screen density
*
* @param context
* @return
*/
public static float getDensity(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.density;
}
/**
* get screen density dpi
*
* @param context
* @return
*/
public static int getDensityDpi(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.densityDpi;
}
/**
* init display metrics
*
* @param context
*/
private static synchronized void initDisplayMetrics(Context context) {
sDisplayMetrics = context.getResources().getDisplayMetrics();
}
/**
* is landscape
*
* @param context
* @return
*/
public static boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
/**
* is portrait
*
* @param context
* @return
*/
public static boolean isPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
public static int getActvieHeight(Activity activity){
Dimension dimen = new Dimension();
// 用户绘制区域
Rect outRect = new Rect();
activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
dimen.mWidth = outRect.width() ;
dimen.mHeight = outRect.height();
// end
return dimen.mHeight;
}
/**
* 获得状态栏的高度
*
* @param activity
* @return
*/
public static int getStatusHeight(Activity activity)
{
// Rect frame = new Rect();
// activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
// int statusBarHeight = frame.top;
// Log.d("111","statusBarHeight"+statusBarHeight);
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 = activity.getResources().getDimensionPixelSize(height);
} catch (Exception e)
{
e.printStackTrace();
}
return statusHeight;
}
public int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
int height = resources.getDimensionPixelSize(resourceId);
Log.v("dbw", "Navi height:" + height);
return height;
}
public static int getActionBarHeight(Context context) {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
return actionBarHeight;
}else{
return 0;
}
}
/**
* get toolbar height
* @param context
* @return
*/
public static int getToolbarHeight(Context context) {
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
return toolbarHeight;
}
private int getTitleHeight(Activity activity) {
int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面状态栏的高度
int titleBarHeight = contentTop - getStatusHeight(activity);
return titleBarHeight;
}
private int getActionBarSizeHeight(Activity activity) {
return activity.getActionBar().getHeight();
}
public static class Dimension {
public int mWidth ;
public int mHeight ;
public Dimension(){}
}
/**
* 获取当前屏幕截图,包含状态栏
*
* @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;
}
/**
* 获取当前屏幕截图,不包含状态栏
*
* @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;
}
}