打開你們手上的項目,基本都會有一大批的輔助類,今天特此整理出10個基本每一個項目中都會使用的工具類,用於快速開發~~html
在此感謝羣裏給我發項目中工具類的兄弟/姐妹~java
一、日誌工具類L.java
- package com.zhy.utils;
- import android.util.Log;
- /**
- * Log統一管理類
- *
- *
- *
- */
- public class L
- {
- private L()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- public static boolean isDebug = true;// 是否須要打印bug,能夠在application的onCreate函數裏面初始化
- private static final String TAG = "way";
- // 下面四個是默認tag的函數
- public static void i(String msg)
- {
- if (isDebug)
- Log.i(TAG, msg);
- }
- public static void d(String msg)
- {
- if (isDebug)
- Log.d(TAG, msg);
- }
- public static void e(String msg)
- {
- if (isDebug)
- Log.e(TAG, msg);
- }
- public static void v(String msg)
- {
- if (isDebug)
- Log.v(TAG, msg);
- }
- // 下面是傳入自定義tag的函數
- public static void i(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- public static void d(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- public static void e(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- public static void v(String tag, String msg)
- {
- if (isDebug)
- Log.i(tag, msg);
- }
- }
package com.zhy.utils; import android.util.Log; /** * Log統一管理類 * * * */ public class L { private L() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } public static boolean isDebug = true;// 是否須要打印bug,能夠在application的onCreate函數裏面初始化 private static final String TAG = "way"; // 下面四個是默認tag的函數 public static void i(String msg) { if (isDebug) Log.i(TAG, msg); } public static void d(String msg) { if (isDebug) Log.d(TAG, msg); } public static void e(String msg) { if (isDebug) Log.e(TAG, msg); } public static void v(String msg) { if (isDebug) Log.v(TAG, msg); } // 下面是傳入自定義tag的函數 public static void i(String tag, String msg) { if (isDebug) Log.i(tag, msg); } public static void d(String tag, String msg) { if (isDebug) Log.i(tag, msg); } public static void e(String tag, String msg) { if (isDebug) Log.i(tag, msg); } public static void v(String tag, String msg) { if (isDebug) Log.i(tag, msg); } }
網上看到的類,註釋上應該原創做者的名字,很簡單的一個類;網上也有不少提供把日誌記錄到SDCard上的,不過我是歷來沒記錄過,因此引入個最簡單的,你們能夠進行評價是否須要擴充~~android
二、Toast統一管理類
- package com.zhy.utils;
- import android.content.Context;
- import android.widget.Toast;
- /**
- * Toast統一管理類
- *
- */
- public class T
- {
- private T()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- public static boolean isShow = true;
- /**
- * 短期顯示Toast
- *
- * @param context
- * @param message
- */
- public static void showShort(Context context, CharSequence message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
- }
- /**
- * 短期顯示Toast
- *
- * @param context
- * @param message
- */
- public static void showShort(Context context, int message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
- }
- /**
- * 長時間顯示Toast
- *
- * @param context
- * @param message
- */
- public static void showLong(Context context, CharSequence message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_LONG).show();
- }
- /**
- * 長時間顯示Toast
- *
- * @param context
- * @param message
- */
- public static void showLong(Context context, int message)
- {
- if (isShow)
- Toast.makeText(context, message, Toast.LENGTH_LONG).show();
- }
- /**
- * 自定義顯示Toast時間
- *
- * @param context
- * @param message
- * @param duration
- */
- public static void show(Context context, CharSequence message, int duration)
- {
- if (isShow)
- Toast.makeText(context, message, duration).show();
- }
- /**
- * 自定義顯示Toast時間
- *
- * @param context
- * @param message
- * @param duration
- */
- public static void show(Context context, int message, int duration)
- {
- if (isShow)
- Toast.makeText(context, message, duration).show();
- }
- }
package com.zhy.utils; import android.content.Context; import android.widget.Toast; /** * Toast統一管理類 * */ public class T { private T() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } public static boolean isShow = true; /** * 短期顯示Toast * * @param context * @param message */ public static void showShort(Context context, CharSequence message) { if (isShow) Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } /** * 短期顯示Toast * * @param context * @param message */ public static void showShort(Context context, int message) { if (isShow) Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } /** * 長時間顯示Toast * * @param context * @param message */ public static void showLong(Context context, CharSequence message) { if (isShow) Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } /** * 長時間顯示Toast * * @param context * @param message */ public static void showLong(Context context, int message) { if (isShow) Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } /** * 自定義顯示Toast時間 * * @param context * @param message * @param duration */ public static void show(Context context, CharSequence message, int duration) { if (isShow) Toast.makeText(context, message, duration).show(); } /** * 自定義顯示Toast時間 * * @param context * @param message * @param duration */ public static void show(Context context, int message, int duration) { if (isShow) Toast.makeText(context, message, duration).show(); } }
也是很是簡單的一個封裝,能省則省了~~web
三、SharedPreferences封裝類SPUtils
- package com.zhy.utils;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Map;
- import android.content.Context;
- import android.content.SharedPreferences;
- public class SPUtils
- {
- /**
- * 保存在手機裏面的文件名
- */
- public static final String FILE_NAME = "share_data";
- /**
- * 保存數據的方法,咱們須要拿到保存數據的具體類型,而後根據類型調用不一樣的保存方法
- *
- * @param context
- * @param key
- * @param object
- */
- public static void put(Context context, String key, Object object)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- if (object instanceof String)
- {
- editor.putString(key, (String) object);
- } else if (object instanceof Integer)
- {
- editor.putInt(key, (Integer) object);
- } else if (object instanceof Boolean)
- {
- editor.putBoolean(key, (Boolean) object);
- } else if (object instanceof Float)
- {
- editor.putFloat(key, (Float) object);
- } else if (object instanceof Long)
- {
- editor.putLong(key, (Long) object);
- } else
- {
- editor.putString(key, object.toString());
- }
- SharedPreferencesCompat.apply(editor);
- }
- /**
- * 獲得保存數據的方法,咱們根據默認值獲得保存的數據的具體類型,而後調用相對於的方法獲取值
- *
- * @param context
- * @param key
- * @param defaultObject
- * @return
- */
- public static Object get(Context context, String key, Object defaultObject)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- if (defaultObject instanceof String)
- {
- return sp.getString(key, (String) defaultObject);
- } else if (defaultObject instanceof Integer)
- {
- return sp.getInt(key, (Integer) defaultObject);
- } else if (defaultObject instanceof Boolean)
- {
- return sp.getBoolean(key, (Boolean) defaultObject);
- } else if (defaultObject instanceof Float)
- {
- return sp.getFloat(key, (Float) defaultObject);
- } else if (defaultObject instanceof Long)
- {
- return sp.getLong(key, (Long) defaultObject);
- }
- return null;
- }
- /**
- * 移除某個key值已經對應的值
- * @param context
- * @param key
- */
- public static void remove(Context context, String key)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.remove(key);
- SharedPreferencesCompat.apply(editor);
- }
- /**
- * 清除全部數據
- * @param context
- */
- public static void clear(Context context)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.clear();
- SharedPreferencesCompat.apply(editor);
- }
- /**
- * 查詢某個key是否已經存在
- * @param context
- * @param key
- * @return
- */
- public static boolean contains(Context context, String key)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- return sp.contains(key);
- }
- /**
- * 返回全部的鍵值對
- *
- * @param context
- * @return
- */
- public static Map<String, ?> getAll(Context context)
- {
- SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
- Context.MODE_PRIVATE);
- return sp.getAll();
- }
- /**
- * 建立一個解決SharedPreferencesCompat.apply方法的一個兼容類
- *
- * @author zhy
- *
- */
- private static class SharedPreferencesCompat
- {
- private static final Method sApplyMethod = findApplyMethod();
- /**
- * 反射查找apply的方法
- *
- * @return
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- private static Method findApplyMethod()
- {
- try
- {
- Class clz = SharedPreferences.Editor.class;
- return clz.getMethod("apply");
- } catch (NoSuchMethodException e)
- {
- }
- return null;
- }
- /**
- * 若是找到則使用apply執行,不然使用commit
- *
- * @param editor
- */
- public static void apply(SharedPreferences.Editor editor)
- {
- try
- {
- if (sApplyMethod != null)
- {
- sApplyMethod.invoke(editor);
- return;
- }
- } catch (IllegalArgumentException e)
- {
- } catch (IllegalAccessException e)
- {
- } catch (InvocationTargetException e)
- {
- }
- editor.commit();
- }
- }
- }
package com.zhy.utils; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class SPUtils { /** * 保存在手機裏面的文件名 */ public static final String FILE_NAME = "share_data"; /** * 保存數據的方法,咱們須要拿到保存數據的具體類型,而後根據類型調用不一樣的保存方法 * * @param context * @param key * @param object */ public static void put(Context context, String key, Object object) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); if (object instanceof String) { editor.putString(key, (String) object); } else if (object instanceof Integer) { editor.putInt(key, (Integer) object); } else if (object instanceof Boolean) { editor.putBoolean(key, (Boolean) object); } else if (object instanceof Float) { editor.putFloat(key, (Float) object); } else if (object instanceof Long) { editor.putLong(key, (Long) object); } else { editor.putString(key, object.toString()); } SharedPreferencesCompat.apply(editor); } /** * 獲得保存數據的方法,咱們根據默認值獲得保存的數據的具體類型,而後調用相對於的方法獲取值 * * @param context * @param key * @param defaultObject * @return */ public static Object get(Context context, String key, Object defaultObject) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); if (defaultObject instanceof String) { return sp.getString(key, (String) defaultObject); } else if (defaultObject instanceof Integer) { return sp.getInt(key, (Integer) defaultObject); } else if (defaultObject instanceof Boolean) { return sp.getBoolean(key, (Boolean) defaultObject); } else if (defaultObject instanceof Float) { return sp.getFloat(key, (Float) defaultObject); } else if (defaultObject instanceof Long) { return sp.getLong(key, (Long) defaultObject); } return null; } /** * 移除某個key值已經對應的值 * @param context * @param key */ public static void remove(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.remove(key); SharedPreferencesCompat.apply(editor); } /** * 清除全部數據 * @param context */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); SharedPreferencesCompat.apply(editor); } /** * 查詢某個key是否已經存在 * @param context * @param key * @return */ public static boolean contains(Context context, String key) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.contains(key); } /** * 返回全部的鍵值對 * * @param context * @return */ public static Map<String, ?> getAll(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); return sp.getAll(); } /** * 建立一個解決SharedPreferencesCompat.apply方法的一個兼容類 * * @author zhy * */ private static class SharedPreferencesCompat { private static final Method sApplyMethod = findApplyMethod(); /** * 反射查找apply的方法 * * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Method findApplyMethod() { try { Class clz = SharedPreferences.Editor.class; return clz.getMethod("apply"); } catch (NoSuchMethodException e) { } return null; } /** * 若是找到則使用apply執行,不然使用commit * * @param editor */ public static void apply(SharedPreferences.Editor editor) { try { if (sApplyMethod != null) { sApplyMethod.invoke(editor); return; } } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } editor.commit(); } } }對SharedPreference的使用作了建議的封裝,對外公佈出put,get,remove,clear等等方法;
注意一點,裏面全部的commit操做使用了SharedPreferencesCompat.apply進行了替代,目的是儘量的使用apply代替commitapi
首先說下爲何,由於commit方法是同步的,而且咱們不少時候的commit操做都是UI線程中,畢竟是IO操做,儘量異步;網絡
因此咱們使用apply進行替代,apply異步的進行寫入;app
可是apply至關於commit來講是new API呢,爲了更好的兼容,咱們作了適配;less
SharedPreferencesCompat也能夠給你們建立兼容類提供了必定的參考~~iphone
四、單位轉換類 DensityUtils
- package com.zhy.utils;
- import android.content.Context;
- import android.util.TypedValue;
- /**
- * 經常使用單位轉換的輔助類
- *
- *
- *
- */
- public class DensityUtils
- {
- private DensityUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * dp轉px
- *
- * @param context
- * @param val
- * @return
- */
- public static int dp2px(Context context, float dpVal)
- {
- return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
- dpVal, context.getResources().getDisplayMetrics());
- }
- /**
- * sp轉px
- *
- * @param context
- * @param val
- * @return
- */
- public static int sp2px(Context context, float spVal)
- {
- return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
- spVal, context.getResources().getDisplayMetrics());
- }
- /**
- * px轉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轉sp
- *
- * @param fontScale
- * @param pxVal
- * @return
- */
- public static float px2sp(Context context, float pxVal)
- {
- return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
- }
- }
package com.zhy.utils; import android.content.Context; import android.util.TypedValue; /** * 經常使用單位轉換的輔助類 * * * */ public class DensityUtils { private DensityUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * dp轉px * * @param context * @param val * @return */ public static int dp2px(Context context, float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics()); } /** * sp轉px * * @param context * @param val * @return */ public static int sp2px(Context context, float spVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources().getDisplayMetrics()); } /** * px轉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轉sp * * @param fontScale * @param pxVal * @return */ public static float px2sp(Context context, float pxVal) { return (pxVal / context.getResources().getDisplayMetrics().scaledDensity); } }
五、SD卡相關輔助類 SDCardUtils
- package com.zhy.utils;
- import java.io.File;
- import android.os.Environment;
- import android.os.StatFs;
- /**
- * SD卡相關的輔助類
- *
- *
- *
- */
- public class SDCardUtils
- {
- private SDCardUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 判斷SDCard是否可用
- *
- * @return
- */
- public static boolean isSDCardEnable()
- {
- return Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED);
- }
- /**
- * 獲取SD卡路徑
- *
- * @return
- */
- public static String getSDCardPath()
- {
- return Environment.getExternalStorageDirectory().getAbsolutePath()
- + File.separator;
- }
- /**
- * 獲取SD卡的剩餘容量 單位byte
- *
- * @return
- */
- public static long getSDCardAllSize()
- {
- if (isSDCardEnable())
- {
- StatFs stat = new StatFs(getSDCardPath());
- // 獲取空閒的數據塊的數量
- long availableBlocks = (long) stat.getAvailableBlocks() - 4;
- // 獲取單個數據塊的大小(byte)
- long freeBlocks = stat.getAvailableBlocks();
- return freeBlocks * availableBlocks;
- }
- return 0;
- }
- /**
- * 獲取指定路徑所在空間的剩餘可用容量字節數,單位byte
- *
- * @param filePath
- * @return 容量字節 SDCard可用空間,內部存儲可用空間
- */
- public static long getFreeBytes(String filePath)
- {
- // 若是是sd卡的下的路徑,則獲取sd卡可用容量
- if (filePath.startsWith(getSDCardPath()))
- {
- filePath = getSDCardPath();
- } else
- {// 若是是內部存儲的路徑,則獲取內存存儲的可用容量
- filePath = Environment.getDataDirectory().getAbsolutePath();
- }
- StatFs stat = new StatFs(filePath);
- long availableBlocks = (long) stat.getAvailableBlocks() - 4;
- return stat.getBlockSize() * availableBlocks;
- }
- /**
- * 獲取系統存儲路徑
- *
- * @return
- */
- public static String getRootDirectoryPath()
- {
- return Environment.getRootDirectory().getAbsolutePath();
- }
- }
package com.zhy.utils; import java.io.File; import android.os.Environment; import android.os.StatFs; /** * SD卡相關的輔助類 * * * */ public class SDCardUtils { private SDCardUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 判斷SDCard是否可用 * * @return */ public static boolean isSDCardEnable() { return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); } /** * 獲取SD卡路徑 * * @return */ public static String getSDCardPath() { return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator; } /** * 獲取SD卡的剩餘容量 單位byte * * @return */ public static long getSDCardAllSize() { if (isSDCardEnable()) { StatFs stat = new StatFs(getSDCardPath()); // 獲取空閒的數據塊的數量 long availableBlocks = (long) stat.getAvailableBlocks() - 4; // 獲取單個數據塊的大小(byte) long freeBlocks = stat.getAvailableBlocks(); return freeBlocks * availableBlocks; } return 0; } /** * 獲取指定路徑所在空間的剩餘可用容量字節數,單位byte * * @param filePath * @return 容量字節 SDCard可用空間,內部存儲可用空間 */ public static long getFreeBytes(String filePath) { // 若是是sd卡的下的路徑,則獲取sd卡可用容量 if (filePath.startsWith(getSDCardPath())) { filePath = getSDCardPath(); } else {// 若是是內部存儲的路徑,則獲取內存存儲的可用容量 filePath = Environment.getDataDirectory().getAbsolutePath(); } StatFs stat = new StatFs(filePath); long availableBlocks = (long) stat.getAvailableBlocks() - 4; return stat.getBlockSize() * availableBlocks; } /** * 獲取系統存儲路徑 * * @return */ public static String getRootDirectoryPath() { return Environment.getRootDirectory().getAbsolutePath(); } }
六、屏幕相關輔助類 ScreenUtils
- package com.zhy.utils;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Rect;
- import android.util.DisplayMetrics;
- import android.view.View;
- import android.view.WindowManager;
- /**
- * 得到屏幕相關的輔助類
- *
- *
- *
- */
- public class ScreenUtils
- {
- private ScreenUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 得到屏幕高度
- *
- * @param context
- * @return
- */
- 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
- */
- 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
- */
- 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
- */
- 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
- */
- 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;
- }
- }
package com.zhy.utils; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Rect; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; /** * 得到屏幕相關的輔助類 * * * */ public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 得到屏幕高度 * * @param context * @return */ 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 */ 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 */ 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 */ 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 */ 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; } }
七、App相關輔助類
- package com.zhy.utils;
- import android.content.Context;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- /**
- * 跟App相關的輔助類
- *
- *
- *
- */
- public class AppUtils
- {
- private AppUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 獲取應用程序名稱
- */
- public static String getAppName(Context context)
- {
- try
- {
- PackageManager packageManager = context.getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(
- context.getPackageName(), 0);
- int labelRes = packageInfo.applicationInfo.labelRes;
- return context.getResources().getString(labelRes);
- } catch (NameNotFoundException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * [獲取應用程序版本名稱信息]
- *
- * @param context
- * @return 當前應用的版本名稱
- */
- public static String getVersionName(Context context)
- {
- try
- {
- PackageManager packageManager = context.getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(
- context.getPackageName(), 0);
- return packageInfo.versionName;
- } catch (NameNotFoundException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- }
package com.zhy.utils; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; /** * 跟App相關的輔助類 * * * */ public class AppUtils { private AppUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 獲取應用程序名稱 */ public static String getAppName(Context context) { try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); int labelRes = packageInfo.applicationInfo.labelRes; return context.getResources().getString(labelRes); } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } /** * [獲取應用程序版本名稱信息] * * @param context * @return 當前應用的版本名稱 */ public static String getVersionName(Context context) { try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); return packageInfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } }
八、軟鍵盤相關輔助類KeyBoardUtils
- package com.zhy.utils;
- import android.content.Context;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.EditText;
- /**
- * 打開或關閉軟鍵盤
- *
- * @author zhy
- *
- */
- public class KeyBoardUtils
- {
- /**
- * 打卡軟鍵盤
- *
- * @param mEditText
- * 輸入框
- * @param mContext
- * 上下文
- */
- public static void openKeybord(EditText mEditText, Context mContext)
- {
- InputMethodManager imm = (InputMethodManager) mContext
- .getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
- imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
- InputMethodManager.HIDE_IMPLICIT_ONLY);
- }
- /**
- * 關閉軟鍵盤
- *
- * @param mEditText
- * 輸入框
- * @param mContext
- * 上下文
- */
- public static void closeKeybord(EditText mEditText, Context mContext)
- {
- InputMethodManager imm = (InputMethodManager) mContext
- .getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
- }
- }
package com.zhy.utils; import android.content.Context; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; /** * 打開或關閉軟鍵盤 * * @author zhy * */ public class KeyBoardUtils { /** * 打卡軟鍵盤 * * @param mEditText * 輸入框 * @param mContext * 上下文 */ public static void openKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } /** * 關閉軟鍵盤 * * @param mEditText * 輸入框 * @param mContext * 上下文 */ public static void closeKeybord(EditText mEditText, Context mContext) { InputMethodManager imm = (InputMethodManager) mContext .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); } }
九、網絡相關輔助類 NetUtils
- package com.zhy.utils;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- /**
- * 跟網絡相關的工具類
- *
- *
- *
- */
- public class NetUtils
- {
- private NetUtils()
- {
- /* cannot be instantiated */
- throw new UnsupportedOperationException("cannot be instantiated");
- }
- /**
- * 判斷網絡是否鏈接
- *
- * @param context
- * @return
- */
- public static boolean isConnected(Context context)
- {
- ConnectivityManager connectivity = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (null != connectivity)
- {
- NetworkInfo info = connectivity.getActiveNetworkInfo();
- if (null != info && info.isConnected())
- {
- if (info.getState() == NetworkInfo.State.CONNECTED)
- {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 判斷是不是wifi鏈接
- */
- public static boolean isWifi(Context context)
- {
- ConnectivityManager cm = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cm == null)
- return false;
- return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
- }
- /**
- * 打開網絡設置界面
- */
- public static void openSetting(Activity activity)
- {
- Intent intent = new Intent("/");
- ComponentName cm = new ComponentName("com.android.settings",
- "com.android.settings.WirelessSettings");
- intent.setComponent(cm);
- intent.setAction("android.intent.action.VIEW");
- activity.startActivityForResult(intent, 0);
- }
- }
package com.zhy.utils; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; /** * 跟網絡相關的工具類 * * * */ public class NetUtils { private NetUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 判斷網絡是否鏈接 * * @param context * @return */ public static boolean isConnected(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (null != connectivity) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (null != info && info.isConnected()) { if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } return false; } /** * 判斷是不是wifi鏈接 */ public static boolean isWifi(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) return false; return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; } /** * 打開網絡設置界面 */ public static void openSetting(Activity activity) { Intent intent = new Intent("/"); ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"); intent.setComponent(cm); intent.setAction("android.intent.action.VIEW"); activity.startActivityForResult(intent, 0); } }
十、Http相關輔助類 HttpUtils
- package com.zhy.utils;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Http請求的工具類
- *
- * @author zhy
- *
- */
- public class HttpUtils
- {
- private static final int TIMEOUT_IN_MILLIONS = 5000;
- public interface CallBack
- {
- void onRequestComplete(String result);
- }
- /**
- * 異步的Get請求
- *
- * @param urlStr
- * @param callBack
- */
- public static void doGetAsyn(final String urlStr, final CallBack callBack)
- {
- new Thread()
- {
- public void run()
- {
- try
- {
- String result = doGet(urlStr);
- if (callBack != null)
- {
- callBack.onRequestComplete(result);
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- };
- }.start();
- }
- /**
- * 異步的Post請求
- * @param urlStr
- * @param params
- * @param callBack
- * @throws Exception
- */
- public static void doPostAsyn(final String urlStr, final String params,
- final CallBack callBack) throws Exception
- {
- new Thread()
- {
- public void run()
- {
- try
- {
- String result = doPost(urlStr, params);
- if (callBack != null)
- {
- callBack.onRequestComplete(result);
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- };
- }.start();
- }
- /**
- * Get請求,得到返回數據
- *
- * @param urlStr
- * @return
- * @throws Exception
- */
- public static String doGet(String urlStr)
- {
- URL url = null;
- HttpURLConnection conn = null;
- InputStream is = null;
- ByteArrayOutputStream baos = null;
- try
- {
- url = new URL(urlStr);
- conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
- conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
- conn.setRequestMethod("GET");
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- if (conn.getResponseCode() == 200)
- {
- is = conn.getInputStream();
- baos = new ByteArrayOutputStream();
- int len = -1;
- byte[] buf = new byte[128];
- while ((len = is.read(buf)) != -1)
- {
- baos.write(buf, 0, len);
- }
- baos.flush();
- return baos.toString();
- } else
- {
- throw new RuntimeException(" responseCode is not 200 ... ");
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- } finally
- {
- try
- {
- if (is != null)
- is.close();
- } catch (IOException e)
- {
- }
- try
- {
- if (baos != null)
- baos.close();
- } catch (IOException e)
- {
- }
- conn.disconnect();
- }
- return null ;
- }
- /**
- * 向指定 URL 發送POST方法的請求
- *
- * @param url
- * 發送請求的 URL
- * @param param
- * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
- * @return 所表明遠程資源的響應結果
- * @throws Exception
- */
- public static String doPost(String url, String param)
- {
- PrintWriter out = null;
- BufferedReader in = null;
- String result = "";
- try
- {
- URL realUrl = new URL(url);
- // 打開和URL之間的鏈接
- HttpURLConnection conn = (HttpURLConnection) realUrl
- .openConnection();
- // 設置通用的請求屬性
- conn.setRequestProperty("accept", "*/*");
- conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- conn.setRequestProperty("charset", "utf-8");
- conn.setUseCaches(false);
- // 發送POST請求必須設置以下兩行
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
- conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
- if (param != null && !param.trim().equals(""))
- {
- // 獲取URLConnection對象對應的輸出流
- out = new PrintWriter(conn.getOutputStream());
- // 發送請求參數
- out.print(param);
- // flush輸出流的緩衝
- out.flush();
- }
- // 定義BufferedReader輸入流來讀取URL的響應
- in = new BufferedReader(
- new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
- {
- result += line;
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- // 使用finally塊來關閉輸出流、輸入流
- finally
- {
- try
- {
- if (out != null)
- {
- out.close();
- }
- if (in != null)
- {
- in.close();
- }
- } catch (IOException ex)
- {
- ex.printStackTrace();
- }
- }
- return result;
- }
- }
package com.zhy.utils; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; /** * Http請求的工具類 * * @author zhy * */ public class HttpUtils { private static final int TIMEOUT_IN_MILLIONS = 5000; public interface CallBack { void onRequestComplete(String result); } /** * 異步的Get請求 * * @param urlStr * @param callBack */ public static void doGetAsyn(final String urlStr, final CallBack callBack) { new Thread() { public void run() { try { String result = doGet(urlStr); if (callBack != null) { callBack.onRequestComplete(result); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } /** * 異步的Post請求 * @param urlStr * @param params * @param callBack * @throws Exception */ public static void doPostAsyn(final String urlStr, final String params, final CallBack callBack) throws Exception { new Thread() { public void run() { try { String result = doPost(urlStr, params); if (callBack != null) { callBack.onRequestComplete(result); } } catch (Exception e) { e.printStackTrace(); } }; }.start(); } /** * Get請求,得到返回數據 * * @param urlStr * @return * @throws Exception */ public static String doGet(String urlStr) { URL url = null; HttpURLConnection conn = null; InputStream is = null; ByteArrayOutputStream baos = null; try { url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(TIMEOUT_IN_MILLIONS); conn.setConnectTimeout(TIMEOUT_IN_MILLIONS); conn.setRequestMethod("GET"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); if (conn.getResponseCode() == 200) { is = conn.getInputStream(); baos = new ByteArrayOutputStream(); int len = -1; byte[] buf = new byte[128]; while ((len = is.read(buf)) != -1) { baos.write(buf, 0, len); } baos.flush(); return baos.toString(); } else { throw new RuntimeException(" responseCode is not 200 ... "); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { } try { if (baos != null) baos.close(); } catch (IOException e) { } conn.disconnect(); } return null ; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所表明遠程資源的響應結果 * @throws Exception */ public static String doPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打開和URL之間的鏈接 HttpURLConnection conn = (HttpURLConnection) realUrl .openConnection(); // 設置通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("charset", "utf-8"); conn.setUseCaches(false); // 發送POST請求必須設置以下兩行 conn.setDoOutput(true); conn.setDoInput(true); conn.setReadTimeout(TIMEOUT_IN_MILLIONS); conn.setConnectTimeout(TIMEOUT_IN_MILLIONS); if (param != null && !param.trim().equals("")) { // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩衝 out.flush(); } // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printStackTrace(); } // 使用finally塊來關閉輸出流、輸入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }若是你們在使用過程當中出現什麼錯誤,或者有更好的建議,歡迎你們留言提出~~能夠不斷的改進這些類~