2016-9-13

1.HttpURLConnection简单使用

    public static final String DEF_CHATSET = "UTF-8";    public static final int DEF_CONN_TIMEOUT = 30000;    public static final int DEF_READ_TIMEOUT = 30000;    public static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";    /**     *     * @param strUrl 请求地址     * @param params 请求参数     * @param method 请求方法     * @return  网络请求字符串     * @throws Exception     */    public static String net(String strUrl, Map params,String method) throws Exception {        HttpURLConnection conn = null;        BufferedReader reader = null;        String rs = null;        try {            StringBuffer sb = new StringBuffer();            if(method==null || method.equals("GET")){                strUrl = strUrl+"?"+urlencode(params);            }            URL url = new URL(strUrl);            conn = (HttpURLConnection) url.openConnection();            if(method==null || method.equals("GET")){                conn.setRequestMethod("GET");            }else{                conn.setRequestMethod("POST");                conn.setDoOutput(true);                DataOutputStream out = null;                try {                    out = new DataOutputStream(conn.getOutputStream());                    out.writeBytes(urlencode(params));                } catch (Exception e) {                    // TODO: handle exception                }finally{                    if (out != null) {                        out.close();                    }                }            }            conn.setRequestProperty("User-agent", userAgent);            conn.setUseCaches(false);            conn.setConnectTimeout(DEF_CONN_TIMEOUT);            conn.setReadTimeout(DEF_READ_TIMEOUT);            conn.setInstanceFollowRedirects(false);            conn.connect();            int recode=conn.getResponseCode();            BufferedReader reader=null;            if(recode==200){                InputStream is = conn.getInputStream();                reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));                String strRead = null;                while ((strRead = reader.readLine()) != null) {                    sb.append(strRead);                }                rs = sb.toString();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (reader != null) {                reader.close();            }            if (conn != null) {                conn.disconnect();            }        }        return rs;    }    //将map型转为请求参数型    public static String urlencode(Mapdata) {        if(data==null || data.isEmpty()){            return "";        }        StringBuilder sb = new StringBuilder();        for (Map.Entry i : data.entrySet()) {            try {                sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }        }        return sb.toString();    }

2.android高斯模糊

    public Bitmap blurBitmap(Bitmap bitmap){          //Let's create an empty bitmap with the same size of the bitmap we want to blur          Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);          //Instantiate a new Renderscript          RenderScript rs = RenderScript.create(getApplicationContext());          //Create an Intrinsic Blur Script using the Renderscript          ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));          //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps          Allocation allIn = Allocation.createFromBitmap(rs, bitmap);          Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);          //Set the radius of the blur          blurScript.setRadius(25.f);          //Perform the Renderscript          blurScript.setInput(allIn);          blurScript.forEach(allOut);          //Copy the final bitmap created by the out Allocation to the outBitmap          allOut.copyTo(outBitmap);          //recycle the original bitmap          bitmap.recycle();          //After finishing everything, we destroy the Renderscript.          rs.destroy();          return outBitmap;      }  

3.DecimalFormat

要获取具体语言环境的 NumberFormat(包括默认语言环境),可调用 NumberFormat 的某个工厂方法,如 getInstance()。通常不直接调用 DecimalFormat 的构造方法,因为 NumberFormat 的工厂方法可能返回不同于 DecimalFormat 的子类NumberFormat 的工厂方法返回的就是DecimalFormat类型特殊模式字符符号          位置          本地化?    含义  0           数字          是           阿拉伯数字  #           数字字         是           阿拉伯数字,如果不存在则显示为 0  .           数字          是           小数分隔符或货币小数分隔符  -           数字          是           减号  ,           数字          是           分组分隔符  E           数字          是           分隔科学计数法中的尾数和指数。在前缀或后缀中无需加引号。  ;           子模式边界   是           分隔正数和负数子模式  %           前缀或后缀   是           乘以 100 并显示为百分数  \u2030      前缀或后缀   是           乘以 1000 并显示为千分数  ¤ (\u00A4)  前缀或后缀   否       货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。  '           前缀或后缀   否           用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock"。其中0#都表示数字站位符, 区别:0:     比实际数字的位数多,不足的地方用0补上。    new DecimalFormat("00.00").format(3.14)  //结果:03.14    new DecimalFormat("0.000").format(3.14)  //结果: 3.140    new DecimalFormat("00.000").format(3.14)  //结果:03.140    比实际数字的位数少:整数部分不改动,小数部分,四舍五入    new DecimalFormat("0.000").format(13.146)  //结果:13.146    new DecimalFormat("00.00").format(13.146)  //结果:13.15    new DecimalFormat("0.00").format(13.146)  //结果:13.15#:     比实际数字的位数多,输出不变。    new DecimalFormat("##.##").format(3.14)  //结果:3.14    new DecimalFormat("#.###").format(3.14)  //结果: 3.14    new DecimalFormat("##.###").format(3.14)  //结果:3.14    比实际数字的位数少:整数部分不改动,小数部分,四舍五入    new DecimalFormat("#.###").format(13.146)  //结果:13.146    new DecimalFormat("##.##").format(13.146)  //结果:13.15    new DecimalFormat("#.##").format(13.146)  //结果:13.15 #会把小数点后最后的0都去掉如:new DecimalFormat(",###.####").format(3333333333.000) // 结果 3,333,333,333new DecimalFormat(",###.####").format(3333333333.300) // 结果 3,333,333,333.3    double myNumber = -1234.56;    NumberFormat form;    for (int j = 0; j < 5; ++j) {        switch (j) {            case 0:                form = NumberFormat.getInstance(Locale.CHINA);//获取常规数值格式                break;            case 1:                form = NumberFormat.getNumberInstance(Locale.CHINA);//获取常规数值格式                break;            case 2:                form = NumberFormat.getIntegerInstance(Locale.CHINA);//获取整数数值格式                break;            case 3:                form = NumberFormat.getCurrencyInstance(Locale.CHINA);//获取货币数值格式                break;            default:                form = NumberFormat.getPercentInstance(Locale.CHINA);//获取显示百分比的格式                break;        }        if (form instanceof DecimalFormat) {            System.out.print("form is DecimalFormat: " + ((DecimalFormat) form).toPattern());        }        System.out.print(" -> " + form.format(myNumber));        try {            System.out.println(" -> " + form.parse(form.format(myNumber)));        } catch (ParseException e) {        }    }输出:form is DecimalFormat: #,##0.### -> -1,234.56 -> -1234.56form is DecimalFormat: #,##0.### -> -1,234.56 -> -1234.56form is DecimalFormat: #,##0 -> -1,235 -> -1235form is DecimalFormat: ¤#,##0.00 -> -¥1,234.56 -> -1234.56form is DecimalFormat: #,##0% -> -123,456% -> -1234.56

更多相关文章

  1. 基于android平台开发的计算器
  2. Android添加自己的属性
  3. Android(安卓)BigDecimal工具类
  4. android 剪切图片 显示图片的一部分
  5. 2012-06-13 16:50 Android限定EditText的输入类型为数字或者英文
  6. Attribute is missing the Android(安卓)namespace prefix
  7. 【cocos2dx 3.2】2048
  8. 玩转Android---UI篇---EditText(编辑框)
  9. Android(安卓)init进程一些容易忽视的技术细节

随机推荐

  1. android 限制编辑框输入中文
  2. [读书笔记]Android LayoutInflater.infla
  3. android stagefright框架(一)Video Playbac
  4. android 开发:保存图片到SD卡上
  5. Cheatsheet: 2013 11.12 ~ 11.30
  6. Android工程获取bulid.xml文件的方法
  7. 基于bmob的极简日记
  8. Android 2.2 API 中文文档系列(3) —— Acc
  9. Program for Android in C/C++ with the
  10. Android之实现“抽奖大轮盘”