diff --git a/README.md b/README.md index 254ef55..369f913 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,48 @@ - 代码库: - 1. 给当前界面添加一个透明度 - 2. 一键添加qq群 - 3. 得到当前版本号 - 4. 判断当前网络是否可用 - 5. 当前是否是移动网络 - 6. 当前是否是wifi - 7. ListView根据item计算出实际的高度 - 8. 拨打电话(记得在清单文件添加权限) - 9. 字符串是否包含汉字 - 10. 从assets 文件夹中读取图片 - 11. 判断字符串是否为空 - 12. 递归删除文件夹及里边的文件 - 13.非常全面的一个图片库,个人开发者必看 - 14.各种常用单位转换以及屏幕尺寸获取:获得屏幕宽度(像素),获得屏幕高度(像素),px2dip,dip2px,px2sp,sp2px - 15. 压缩图片,防止Bitmap过大导致OOM - 16.请求打开蓝牙 - 17.读取Excel文件(http://www.jianshu.com/p/2b747c74c961) - 18.应用程序启动短暂白屏或者黑屏解决办法(启动页设置背景图片) - 19.判断是否短时间内重复操作 - 20.得到sd卡路径 - 21.从网络路径下载图片保存到本地,并返回本地保存路径 - 22.改变系统语言为英文环境 - 23.异或运算和校验 - 24.Image,Bitmap工具类:ImageUtils.java - ......未完待续...... +前言 + +> 本项目目的在于收集开发中常用的工具类以及工具方法等,主要想作为个人代码仓库,不间断更新,持续完善,也欢迎看到的朋友一起交流学习和补充不足。 + + + +更新日志 + +``` +2018/06/28 +1.增加分类不同的文件夹,以便后期维护。 +2.新增HttpURLConnection基本使用工具类 +``` + +```java +eight months ago +初始化项目 +1. 给当前界面添加一个透明度 +2. 一键添加qq群 +3. 得到当前版本号 +4. 判断当前网络是否可用 +5. 当前是否是移动网络 +6. 当前是否是wifi +7. ListView根据item计算出实际的高度 +8. 拨打电话(记得在清单文件添加权限) +9. 字符串是否包含汉字 +10. 从assets 文件夹中读取图片 +11. 判断字符串是否为空 +12. 递归删除文件夹及里边的文件 +13.非常全面的一个图片库,个人开发者必看 +14.各种常用单位转换以及屏幕尺寸获取:获得屏幕宽度(像素),获得屏幕高度(像素),px2dip,dip2px,px2sp,sp2px +15. 压缩图片,防止Bitmap过大导致OOM +16.请求打开蓝牙 +17.读取Excel文件(http://www.jianshu.com/p/2b747c74c961) +18.应用程序启动短暂白屏或者黑屏解决办法(启动页设置背景图片) +19.判断是否短时间内重复操作 +20.得到sd卡路径 +21.从网络路径下载图片保存到本地,并返回本地保存路径 +22.改变系统语言为英文环境 +23.异或运算和校验 +24.Image,Bitmap工具类:ImageUtils.java +25.test git submodule +``` + +​ + + + diff --git a/Ch340Chip.java "b/USB\350\275\254\344\270\262\345\217\243\345\274\200\345\217\221\347\233\270\345\205\263/Ch340Chip.java" similarity index 99% rename from Ch340Chip.java rename to "USB\350\275\254\344\270\262\345\217\243\345\274\200\345\217\221\347\233\270\345\205\263/Ch340Chip.java" index 45a2bb5..39df8c8 100644 --- a/Ch340Chip.java +++ "b/USB\350\275\254\344\270\262\345\217\243\345\274\200\345\217\221\347\233\270\345\205\263/Ch340Chip.java" @@ -1,3 +1,7 @@ + +/** +CH340型号芯片相关配置 +**/ public class CH340Chip { public boolean config(UsbDeviceConnection usbDeviceConnection) { diff --git a/Utils.java "b/\345\205\266\344\273\226/Utils.java" similarity index 87% rename from Utils.java rename to "\345\205\266\344\273\226/Utils.java" index 350b865..488f095 100644 --- a/Utils.java +++ "b/\345\205\266\344\273\226/Utils.java" @@ -7,7 +7,7 @@ */ public class Utils { - 1.给当前界面添加一个透明度 + //1.给当前界面添加一个透明度 /** * 给界面添加透明度 * @@ -21,10 +21,10 @@ public static void backgroundAlpha(Activity activity, float bgAlpha) { activity.getWindow().setAttributes(lp); } - 2.一键添加qq群 + //2.一键添加qq群 //http://qun.qq.com/join.html 选择需要添加的群,然后选择手机平台,即可生成相应的代码。 - 3.得到当前版本号 + //3.得到当前版本号 public static int getVersionCode(Context context) { int code = 0; if (context == null) { @@ -38,7 +38,7 @@ public static int getVersionCode(Context context) { return code; } - 4.判断当前网络是否可用 + //4.判断当前网络是否可用 public static boolean isNetworkConnected(Context context) { if (context != null) { @@ -52,7 +52,7 @@ public static boolean isNetworkConnected(Context context) { return false; } - 5.当前是否是移动网络 + //5.当前是否是移动网络 public static boolean isMobileConnected(Context context) { if (context != null) { @@ -66,7 +66,7 @@ public static boolean isMobileConnected(Context context) { return false; } - 6.当前是否是wifi + //6.当前是否是wifi public static boolean isWifi(Context context) { if (context != null) { @@ -79,7 +79,7 @@ public static boolean isWifi(Context context) { return false; } - 7.ListView根据item计算出实际的高度 + //7.ListView根据item计算出实际的高度 public static void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter @@ -98,7 +98,7 @@ public static void setListViewHeightBasedOnChildren(ListView listView) { listView.setLayoutParams(params); } - 8.拨打电话(记得在清单文件添加权限) + //8.拨打电话(记得在清单文件添加权限) /** * @param phoneNum @@ -108,7 +108,7 @@ public void call(String phoneNum, Context context) { context.startActivity(intent); } - 9. 字符串是否包含汉字 + //9. 字符串是否包含汉字 public static boolean checkChinese(String sequence) { final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]"; boolean result = false; @@ -118,7 +118,7 @@ public static boolean checkChinese(String sequence) { return result; } - 10.从assets 文件夹中读取图片 + //10.从assets 文件夹中读取图片 public static Drawable loadImageFromAsserts(final Context ctx, String fileName) { try { InputStream is = ctx.getResources().getAssets().open(fileName); @@ -139,7 +139,7 @@ public static Drawable loadImageFromAsserts(final Context ctx, String fileName) return null; } - 11.判断字符串是否为空 + //11.判断字符串是否为空 public static boolean isNull(String string) { if (string != null) { string = string.trim(); @@ -150,7 +150,7 @@ public static boolean isNull(String string) { return true; } - 12.递归删除文件夹及里边的文件 + //12.递归删除文件夹及里边的文件 public void deletFiles(File file) { if (file.isFile()) { file.delete(); @@ -168,10 +168,10 @@ public void deletFiles(File file) { file.delete(); } } - 13.非常全面的一个图片库,个人开发者必看 + //13.非常全面的一个图片库,个人开发者必看 //http://www.iconfont.cn/collections - 14.各种单位转换 + //14.各种单位转换 /** * 获得屏幕宽度(像素) * @@ -250,7 +250,7 @@ public static int sp2px(Context context, float spValue) { return (int) (spValue * fontScale + 0.5f); } - 15. 压缩图片,防止Bitmap过大导致OOM + //15. 压缩图片,防止Bitmap过大导致OOM /** * 谷歌推荐使用方法,从资源中加载图像,并高效压缩,有效降低OOM的概率 * @@ -307,22 +307,22 @@ public int calculateInSampleSize(BitmapFactory.Options options, return inSampleSize; } - 16.请求打开蓝牙 + //16.请求打开蓝牙 //REQUEST_ENABLE_BT自定义requestCode int REQUEST_ENABLE_BT = 0; Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT) { } - 17.读取Excel文件 - 使用jxl jar包。 - 18.应用程序启动短暂白屏或者黑屏解决办法(启动页设置背景图片) + //17.读取Excel文件 + //使用jxl jar包。 + //18.应用程序启动短暂白屏或者黑屏解决办法(启动页设置背景图片) //1.自定义全屏主题 //2.设置给闪屏页 android:theme="@style/FlashStyle" - 19.判断是否是短时间内重复点击按钮之类的动作(防止重复打开页面等) + //19.判断是否是短时间内重复点击按钮之类的动作(防止重复打开页面等) private static long lastClickTime; public synchronized static boolean isDoubleClick(long time) { long currentTime = System.currentTimeMillis(); @@ -335,7 +335,7 @@ public synchronized static boolean isDoubleClick(long time) { } return isClick2; } - 20.得到sd卡路径 + //20.得到sd卡路径 public static String getSDCardPath() { File sdcardDir = null; boolean sdcardExist = Environment.getExternalStorageState().equals( @@ -348,7 +348,7 @@ public static String getSDCardPath() { } } - 21 从网络路径下载图片保存到本地,并返回本地保存路径 + //21 从网络路径下载图片保存到本地,并返回本地保存路径 public static String getImageURI(String networkPath, String dirPath, String s) { if (networkPath == null) { return null; @@ -384,7 +384,7 @@ public static String getImageURI(String networkPath, String dirPath, String s) { } return null; } - 22 改变系统语言为英文环境 + //22 改变系统语言为英文环境 public static void initLanguage() { try { Log.d("Appcontext", "初始化的时候设置系统语言为英文环境"); @@ -406,7 +406,7 @@ public static void initLanguage() { } } - 23.异或运算和校验 + //23.异或运算和校验 public static byte getXor(byte[] datas) { byte temp = datas[0]; @@ -415,4 +415,19 @@ public static byte getXor(byte[] datas) { } return temp; } + //24.在某个View正上方弹出PopupWindow + private void showPopAlongView(View v) { + View view = View.inflate(this, R.layout.pop_layout, null); + view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); + int measureHeight = view.getMeasuredHeight(); + int measureWidth = view.getMeasuredWidth(); + popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + popupWindow.setFocusable(false); + popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); + popupWindow.setOutsideTouchable(false); + int[] location = new int[2]; + v.getLocationOnScreen(location); + popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] + v.getMeasuredWidth() / 2 - measureWidth / 2, location[1] - measureHeight); + + } } diff --git a/ImageUtils.java "b/\345\233\276\347\211\207\347\233\270\345\205\263/ImageUtils.java" similarity index 72% rename from ImageUtils.java rename to "\345\233\276\347\211\207\347\233\270\345\205\263/ImageUtils.java" index 6e931ca..c3c62bc 100644 --- a/ImageUtils.java +++ "b/\345\233\276\347\211\207\347\233\270\345\205\263/ImageUtils.java" @@ -1,4 +1,6 @@ - +/** +View->Bitmap相关操作 +**/ public class ImageUtils { private final static String MEIZU = "meizu"; @@ -221,87 +223,4 @@ public static String saveBitmapPNG(Context context, Bitmap imageBitmap) { return savefile.getPath(); } - public static String uploadPic(String path, AppContext appContext) { - User user = appContext.getActiveUser(); - if (user != null && !TextUtils.isEmpty(path)) { - File file = new File(path); - if (!file.exists()) { - return null; - } - - String sn = user.getSn(); - HashMap h1 = new HashMap(); - h1.put("sn", sn); - HashMap h2 = new HashMap(); - h2.put("file", new File(path)); - try { - String reply = BaseClient.post(appContext, DataURLs.UPLOAD_PIC, Utils.rebuildMap(h1, user), h2); - JSONObject replyJson = new JSONObject(reply); - if (NetRetKeys.SUCCESS_CODE == replyJson.optInt(NetRetKeys.STATE_CODE)) { - path = replyJson.optString(NetRetKeys.DATA); - return path; - } - /* 图片上传失败,先返回本地路径 */ - return path; - } catch (Exception e) { - - /* 图片上传失败,先返回本地路径 */ - return path; - } - } - return ""; - } - - public static void copyAssetToSD(Context context, String pic) throws IOException { - OutputStream myOutput = new FileOutputStream(Utils.getImageFileDir(context) + pic); - try { - InputStream myInput = context.getAssets().open(pic); - byte[] buffer = new byte[1024]; - int length = myInput.read(buffer); - while (length > 0) { - myOutput.write(buffer, 0, length); - length = myInput.read(buffer); - } - myOutput.flush(); - - } finally { - if (myOutput != null) { - myOutput.close(); - } - } - - } - - private static void streamToSD(InputStream myInput, String path) throws IOException { - OutputStream myOutput = null; - try { - myOutput = new FileOutputStream(path); - byte[] buffer = new byte[1024]; - int length = myInput.read(buffer); - while (length > 0) { - myOutput.write(buffer, 0, length); - length = myInput.read(buffer); - } - myOutput.flush(); - myInput.close(); - } finally { - if (myOutput != null) { - myOutput.close(); - } - } - } - - public static String assetSaveToSD(Context context, String resName) { - String baseUrl = Utils.getImageFileDir(context).getAbsolutePath(); - String path = baseUrl + "/" + resName; - if (!new File(path).exists()) { - try { - streamToSD(context.getAssets().open(resName), path); - } catch (IOException e) { - - } - } - return path; - } - } diff --git "a/\347\275\221\347\273\234/HttpURLConnectionUtils.java" "b/\347\275\221\347\273\234/HttpURLConnectionUtils.java" new file mode 100644 index 0000000..4e396fa --- /dev/null +++ "b/\347\275\221\347\273\234/HttpURLConnectionUtils.java" @@ -0,0 +1,95 @@ +/** +HttpURLConnection 基本使用 +**/ + + public class NetUtils { + public static String post(String url, String content) { + HttpURLConnection conn = null; + try { + // 创建一个URL对象 + URL mURL = new URL(url); + // 调用URL的openConnection()方法,获取HttpURLConnection对象 + conn = (HttpURLConnection) mURL.openConnection(); + + conn.setRequestMethod("POST");// 设置请求方法为post + conn.setReadTimeout(5000);// 设置读取超时为5秒 + conn.setConnectTimeout(10000);// 设置连接网络超时为10秒 + conn.setDoOutput(true);// 设置此方法,允许向服务器输出内容 + + // post请求的参数 + String data = content; + // 获得一个输出流,向服务器写数据,默认情况下,系统不允许向服务器输出内容 + OutputStream out = conn.getOutputStream();// 获得一个输出流,向服务器写数据 + out.write(data.getBytes()); + out.flush(); + out.close(); + + int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法 + if (responseCode == 200) { + + InputStream is = conn.getInputStream(); + String response = getStringFromInputStream(is); + return response; + } else { + throw new NetworkErrorException("response status is "+responseCode); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (conn != null) { + conn.disconnect();// 关闭连接 + } + } + + return null; + } + + public static String get(String url) { + HttpURLConnection conn = null; + try { + // 利用string url构建URL对象 + URL mURL = new URL(url); + conn = (HttpURLConnection) mURL.openConnection(); + + conn.setRequestMethod("GET"); + conn.setReadTimeout(5000); + conn.setConnectTimeout(10000); + + int responseCode = conn.getResponseCode(); + if (responseCode == 200) { + + InputStream is = conn.getInputStream(); + String response = getStringFromInputStream(is); + return response; + } else { + throw new NetworkErrorException("response status is "+responseCode); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + + if (conn != null) { + conn.disconnect(); + } + } + + return null; + } + + private static String getStringFromInputStream(InputStream is) + throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + // 模板代码 必须熟练 + byte[] buffer = new byte[1024]; + int len = -1; + while ((len = is.read(buffer)) != -1) { + os.write(buffer, 0, len); + } + is.close(); + String state = os.toString();// 把流中的数据转换成字符串,采用的编码是utf-8(模拟器默认编码) + os.close(); + return state; + } + } \ No newline at end of file