有的时候多次点击会弹出很多Toast,导致一直有Toast显示。一直消不掉。用下面方法可让Toast只显示一条,当再次需要Toast时只会改文本。
/** * Toast工具 */ private static Toast toast; public static void showToast(Context context, String content) { if (toast == null) { toast = Toast.makeText(context, content, Toast.LENGTH_SHORT); } else { toast.setText(content); } toast.show(); }
禁止快速点击导致的一些系统崩溃。
/** * 禁止快速点击 */ private static long lastClickTime; public static boolean isFastDoubleClick(long timer) { long time = (new Date).getTime() long timeD = time - lastClickTime; if ( 0 < timeD && timeD < timer) { return true; } lastClickTime = time; return false; } public static boolean isFastDoubleClick() { return isFastDoubleClick(1000); }
/** * 获取IMEI * * @param context * @return */ public static String getIMEI(Context context) { TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Service.TELEPHONY_SERVICE); String imei = telephonyManager.getDeviceId(); return imei; } /** * MD5加密 * * @param inStr * @return */ public static String string2MD5(String inStr) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { e.printStackTrace(); return ""; } char[] charArray = inStr.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) byteArray[i] = (byte) charArray[i]; byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) hexValue.append("0"); hexValue.append(Integer.toHexString(val)); } return hexValue.toString().toUpperCase(); } /** * 获取版本号 * * @param context * @return */ public static String getVersionName(Context context) { PackageManager pm = context.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0); return info.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return ""; } } public static int getVersionCode(Context context)//获取版本号(内部识别号) { try { PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return pi.versionCode; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } } /** * 禁止快速点击 */ private static long lastClickTime; public static boolean isFastDoubleClick(long timer) { long time = System.currentTimeMillis(); long timeD = time - lastClickTime; if ( 0 < timeD && timeD < timer) { return true; } lastClickTime = time; return false; } public static boolean isFastDoubleClick() { return isFastDoubleClick(1000); } /** * 清理图片信息 * @param dateNow * @param path */ public static void clearPic(Date dateNow,String path,long time){ File file = new File(path); File[] files = file.listFiles(); if (files.length > 0) { for (int i = 0; i < files.length; i++) { File myfile = files[i]; String filename = myfile .getAbsoluteFile().getName(); if(filename.length()<=20)continue;//文件格式不正常的图片则不操作 Date fileDate = DateUtils.dateStringToDate(myfile .getAbsoluteFile().getName().substring(6, 20)); if (dateNow.getTime() - fileDate.getTime() > time) { Log.d("MyTimerClearData", "删除图片文件"+path+"图片名字"+myfile); myfile.delete(); } else { continue; } } } } // 获得可用的内存 @SuppressWarnings("deprecation") public static long getmem_UNUSED(Context mContext) { final double memery = 0.20;//内存空间太少时 File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); long useMemery = availableBlocks * blockSize / (1024 * 1024); //如果剩余空间小于20%则删除最久一天的数据 if((float)useMemery/(float)getmem_ALL()<memery){ ClearDataAndPic clearDataAndPic = new ClearDataAndPic(); clearDataAndPic.clearDataMore(); } return useMemery; } // 获得总体内存 @SuppressWarnings("deprecation") public static long getmem_ALL() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getBlockCount(); return availableBlocks * blockSize / (1024 * 1024); }
String 编码
/** * 将string转换成unicode * * @param string * @return */ public static String string2UTF8(String string) { if (string == null) string = "无信息"; String descStr = ""; try { descStr = new String(java.net.URLEncoder.encode(string, "utf-8").getBytes()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return descStr; } /** * 将unicode转换成string * * @param string * @return */ public static String UTF82string(String string) { if (string == null) string = "无信息"; String descStr = ""; try { descStr = new String(java.net.URLDecoder.decode(string, "utf-8").getBytes()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return descStr; }
获得多点触摸的坐标点,可以对图片进行缩放
//多点触摸 if (MotionEventCompat.getPointerCount(event) > 1) { float touchX_1 = MotionEventCompat.getX(event, 0); float touchY_1 = MotionEventCompat.getY(event, 0); float touchX_2 = MotionEventCompat.getX(event, 1); float touchY_2 = MotionEventCompat.getY(event, 1); double cha = Math.sqrt(Math.pow( Math.abs(touchX_1-touchX_2),2)+ Math.pow( Math.abs(touchY_1-touchY_2),2)); if(cha2!=0.0){ double fangda = cha-cha2; if(Math.abs(fangda)>8){ setZoom(zoom+(new Double(fangda*11/100).intValue())); } }