編輯:關於Android編程
分享一下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像素 * @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軟件的過程需要不斷地進行測試。使用Junint測試框架,是
百度手機助手已經與中國電信達成深度合作協議,在全國范圍內推出免費WIFI服務。只要有中國電信Chinanet wifi熱點地方,百度手機助手用戶都可以免費連
1.2.//使用回調接口,首先初始化pintuview並綁定,實現回調接口的方法 mPintuLayout = (PintuLayout) findViewById
組件有Public和Private的概念,是否能被其他方調用。通過android:exported字段來確定,android:exported=true表示能,反之不行。