Android 之常用工具类(后续)
2016-03-17 12:07
609 查看
SharedPreferencesUtils 共享首选项
2.App相关辅助类 APPUtils.java
3.屏幕相关辅助类 ScreenUtils.java
4.软键盘相关辅助类KeyBoardUtils.java
5.网络相关辅助类 NetUtils.java
6.单例工具类 SingletonUtils.java
7. 关于图片的BitmapUtils
8.有关身份证验证CardUtils
/** * 保存在手机里面的文件名 */ private static SharedPreferencesUtils mSpUtils; public static final String FILE_NAME = "share_data"; /** * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法 * * @param context 上下文 * @param key 键 * @param object 值 */ //提供单例 懒汉模式 线程安全 private SharedPreferencesUtils (){} public static synchronized SharedPreferencesUtils getInstance(){ if(mSpUtils==null){ mSpUtils=new SharedPreferencesUtils(); } return mSpUtils; } 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()); } // 一定要提交 editor.commit(); } /** * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值 * * @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); editor.commit(); } /** * 清除所有数据 * * @param context */ public static void clear(Context context) { SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.clear(); editor.commit(); } /** * 查询某个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(); }
2.App相关辅助类 APPUtils.java
public class APPUtils { private APPUtils() { } /** * 获取应用程序名称 */ 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; } }
3.屏幕相关辅助类 ScreenUtils.java
public class ScreenUtils { private ScreenUtils() { } /** * 获得屏幕高度 * * @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; } }
4.软键盘相关辅助类KeyBoardUtils.java
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); } }
5.网络相关辅助类 NetUtils.java
public class NetUtils{ private NetUtils() { /* cannot be instantiated */ } /** * 判断网络是否连接 */ 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); } }
6.单例工具类 SingletonUtils.java
public abstract class SingletonUtils<T> { private T instance; protected abstract T newInstance(); public final T getInstance() { if (instance == null) { synchronized (SingletonUtils.class) { if (instance == null) { instance = newInstance(); } } } return instance; } }
7. 关于图片的BitmapUtils
private static Bitmap output; /** * 转换图片转换成圆角. * * @param bitmap * 传入Bitmap对象 * @return the bitmap */ public static Bitmap toRoundBitmap(Bitmap bitmap) { if (bitmap == null) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; } /** * 将图片圆形化 * * @param bitmap * @return */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = null; if (bitmap != null) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } output = Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(output); canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG)); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); bitmap = output; return bitmap; } output = null; return null; }
8.有关身份证验证CardUtils
private static String strYear; private static String strMonth; private static String strDay; private static String sex; /** * * 正则验证身份证是否有效 * * @param idCard 身份证号码 * @return */ public static boolean ifIdCard(String idCard){ String idCardReg="^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$"; if(idCard.matches(idCardReg)){ return true; }else { return false; } } /** * * @param mesg 根据身份份证号获取生日 * @return */ public static String SetbirthDay(String mesg){ if(mesg!=null&&mesg!=""&& mesg.length()==18){ strYear = mesg.substring(6, 10); strMonth = mesg.substring(10, 12); strDay = mesg.substring(12, 14); } return strYear+"-"+strMonth+"-"+strDay; } /** * * @param mesg 根据身份证号获取性别 * @return */ public static String setSex(String mesg){ if(mesg!=null &&mesg!="" &&mesg.length()==18){ sex = mesg.substring(16, 17); } if(mesg!=null &&mesg!="" &&mesg.length()==18){ if(Integer.parseInt(sex)%2==0){ sex = "女"; }else{ sex ="男"; } } return sex; }
相关文章推荐
- Android之framework修改底部导航栏NavigationBar动态显示和隐藏
- android 过场动画如何加载
- android 开发技巧
- 《Android框架揭秘》读书笔记——JNI
- android开发 装饰者模式
- Android Studio 插件推荐(合集)
- Android 常用小代码
- Android项目EditText焦点问题
- android圆角矩形图片的实现进而讨论view自定义的要点
- android studio快捷键大全
- android sdk manager 代理设置
- Android悬浮窗进阶版-上篇(思路篇)
- Android四大基本组件介绍与生命周期
- android GridView android:stretchMode="columnWidth"
- Android Activity内镶嵌Activity
- Android菜鸟的成长笔记——PreferenceFragment 与 自定义Preference
- Android studio 导入github工程
- Android USB Host
- Android Touch事件传递机制解析
- Android NDK 如何缩减库的大小