dp px 轉換
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
獲取屏幕寬高
public class AppScreenMgr {
public static int getScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
public static int getScreenHeight(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.heightPixels;
}
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int id = (Integer) (clazz.getField("status_bar_height").get(object));
statusHeight = context.getResources().getDimensionPixelSize(id);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
public static int getRealScreenHeight(Context context) {
int dpi = 0;
Display display = ((Activity) context).getWindowManager()
.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
dpi = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
public static int getNavigationAreaHeight(Context context) {
int realScreenHeight = AppScreenMgr.getRealScreenHeight(context);
int screenHeight = AppScreenMgr.getScreenHeight(context);
return realScreenHeight - screenHeight;
}
public static int getNavigationBarrH(Context c) {
Resources resources = c.getResources();
int identifier = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelOffset(identifier);
}
private AppScreenMgr()
{
throw new UnsupportedOperationException("cannot be instantiated");
}
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;
}
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;
}
public static int getTitleBarHeight(Activity context) {
int contentTop = context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
return contentTop - getStatusBarHeight(context);
}
public static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object obj = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int temp = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(temp);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
private static Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i("TAG", "" + statusBarHeight);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
private static boolean savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
return true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private static Bitmap captureWebView(WebView webView) {
Picture snapShot = webView.capturePicture();
Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
snapShot.draw(canvas);
return bmp;
}
private static String getDate(long time, String format) {
Date date = new Date(time);
SimpleDateFormat formatter = new SimpleDateFormat(format);
String daystr = formatter.format(date);
return daystr;
}
private static Boolean isExistsSD() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
private static String getFileName(Context context) {
String fileName = getDate(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".png";
final String localPath;
if (isExistsSD()) {
localPath = context.getExternalCacheDir() + File.separator + fileName;
} else {
localPath = context.getFilesDir() + fileName;
}
return localPath;
}
public static String shoot(Activity a) {
String localPath = getFileName(a);
boolean ret = AppScreenMgr.savePic(AppScreenMgr.takeScreenShot(a), localPath);
if (ret) {
return localPath;
} else {
return "";
}
}
public static String shootWebView(Context context, WebView webView) {
String localPath = getFileName(context);
boolean ret = AppScreenMgr.savePic(AppScreenMgr.captureWebView(webView), localPath);
if (ret) {
return localPath;
} else {
return "";
}
}
}
log日誌的封裝
public class AppLogMessageMgr {
private static boolean isDebug = true;
public static void isEnableDebug(boolean isDebug){
AppLogMessageMgr.isDebug = isDebug;
}
public static void i(String tag,String msg){
if(isDebug){
Log.i(tag, msg != null ? msg : "");
}
}
public static void i(Object object,String msg){
if(isDebug){
Log.i(object.getClass().getSimpleName(), msg != null ? msg : "");
}
}
public static void i(String msg){
if(isDebug){
Log.i(" [INFO] --- ", msg != null ? msg : "");
}
}
public static void d(String tag,String msg){
if(isDebug){
Log.d(tag, msg != null ? msg : "");
}
}
public static void d(Object object,String msg){
if(isDebug){
Log.d(object.getClass().getSimpleName(), msg != null ? msg : "");
}
}
public static void d(String msg){
if(isDebug){
Log.d(" [DEBUG] --- ", msg != null ? msg : "");
}
}
public static void w(String tag,String msg){
if(isDebug){
Log.w(tag, msg != null ? msg : "");
}
}
public static void w(Object object,String msg){
if(isDebug){
Log.w(object.getClass().getSimpleName(), msg != null ? msg : "");
}
}
public static void w(String msg){
if(isDebug){
Log.w(" [WARN] --- ", msg != null ? msg : "");
}
}
public static void e(String tag,String msg){
if(isDebug){
Log.e(tag, msg !=null ? msg : "");
}
}
public static void e(Object object,String msg){
if(isDebug){
Log.e(object.getClass().getSimpleName(), msg !=null ? msg : "");
}
}
public static void e(String msg){
if(isDebug){
Log.e(" [ERROR] --- ", msg !=null ? msg : "");
}
}
public static void v(String tag, String msg){
if(isDebug){
Log.v(tag, msg != null ? msg : "");
}
}
public static void v(Object object, String msg){
if(isDebug){
Log.v(object.getClass().getSimpleName(), msg != null ? msg : "");
}
}
public static void v( String msg){
if(isDebug){
Log.v(" [VERBOSE] --- ", msg != null ? msg : "");
}
}
}