Android开发常用的工具类

来源:互联网 发布:软件开发学徒骗局 编辑:程序博客网 时间:2024/05/16 09:14

在日常的工作中整理了一些常用的代码,把它们封装成了一个工具方便调用。

1.将一个实体对象反射成一个Map集合,比如说一个接口要传递的参数非常多,而这些参数本身就封装在一个实体对象里面,上传的参数又往往是使用一个Map集合来拼接接口,这样使用实体类转Map就显得方便了许多。

public class BeanUtils {    public static Map<String,String> beanToMap(Object object){        Class c=object.getClass();        Field[] fields=c.getDeclaredFields();        Map<String,String> map=new HashMap<>();        for (Field field : fields) {            String name=field.getName();            String value=getValue(object,name)+"";            map.put(name,value);        }        return map;    }    public static Object getValue(Object object,String key) {        String name="get"+key.substring(0,1).toUpperCase()+key.substring(1);        try {            Method method = object.getClass().getDeclaredMethod(name, new Class[0]);            return method.invoke(object, new Object[]{});        } catch (Exception e) {            e.printStackTrace();        }        return null;    }}

2.图片相关的工具类

package com.lianjiu.b.common.utils;import android.app.Activity;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.BitmapFactory.Options;import android.graphics.BlurMaskFilter;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Shader;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.Build;import android.os.Environment;import android.util.DisplayMetrics;import android.view.Display;import android.view.View;import android.view.WindowManager;import com.lianjiu.b.base.Constant;import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.lang.reflect.Field;import java.math.BigDecimal;import java.util.Calendar;import java.util.Date;public class BitmapUtil {    private final static String TAG = BitmapUtil.class.getSimpleName();    public static Uri uri;    public static String fileName = "";    /**     * 计算缩放比例     *     * filename 图片路径 maxWidth 目标宽度 maxHeight 目标高度     */    public static Bitmap scalePicture(String filename, int maxWidth, int maxHeight) {        Bitmap bitmap = null;        try {            Options opts = new Options();            opts.inJustDecodeBounds = true;            BitmapFactory.decodeFile(filename, opts);            int srcWidth = opts.outWidth;            int srcHeight = opts.outHeight;            int desWidth = 0;            int desHeight = 0;            // 缩放比例            double ratio = 0.0;            if (srcWidth > srcHeight) {                ratio = srcWidth / maxWidth;                desWidth = maxWidth;                desHeight = (int) (srcHeight / ratio);            } else {                ratio = srcHeight / maxHeight;                desHeight = maxHeight;                desWidth = (int) (srcWidth / ratio);            }            // 设置输出宽度、高度            Options newOpts = new Options();            newOpts.inSampleSize = (int) (ratio) + 1;            newOpts.inJustDecodeBounds = false;            newOpts.outWidth = desWidth;            newOpts.outHeight = desHeight;            bitmap = BitmapFactory.decodeFile(filename, newOpts);        } catch (Throwable e) {            e.printStackTrace();        }        return bitmap;    }    /**     * 计算缩放比例     *     * srcBitmap 原图 目标宽度 目标高度     */    public static Bitmap scalePicture(Bitmap srcBitmap, int maxWidth, int maxHeight) {        try {            Bitmap bitmap = null;            int srcWidth = srcBitmap.getWidth();            int srcHeight = srcBitmap.getHeight();            // 计算缩放率,新尺寸除原始尺寸            float scaleWidth = ((float) maxWidth) / srcWidth;            float scaleHeight = ((float) maxHeight) / srcHeight;            // 创建操作图片用的matrix对象            Matrix matrix = new Matrix();            // 缩放图片动作            matrix.postScale(scaleWidth, scaleHeight);            // 创建新的图片            bitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, true);            return bitmap;        } catch (Throwable t) {            t.printStackTrace();            System.gc();        }        return srcBitmap;    }    public static Bitmap createBitmap(String bmPath, Options opt) {        try {            opt.inPreferredConfig = Config.RGB_565;            opt.inPurgeable = true;            opt.inInputShareable = true;            FileInputStream fis = new FileInputStream(bmPath);            return BitmapFactory.decodeStream(fis, null, opt);        } catch (Throwable t) {            LogUtil.log(t);        }        return null;    }    public static Bitmap createBitmap(Context context, int resId) {        Bitmap bmp;        Resources res = context.getResources();        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            bmp = BitmapFactory.decodeResource(res, resId);        } else {            Options opt = new Options();            opt.inPreferredConfig = Config.RGB_565;            /**             * 如果 inPurgeable 设为True的话表示使用BitmapFactory创建的Bitmap 用于存储Pixel的内存空间在系统内存不足时可以被回收,             * 在应用需要再次访问Bitmap的Pixel时(如绘制Bitmap或是调用getPixel), 系统会再次调用BitmapFactory decoder重新生成Bitmap的Pixel数组。             * 为了能够重新解码图像,bitmap要能够访问存储Bitmap的原始数据。             *             * 在inPurgeable为false时表示创建的Bitmap的Pixel内存空间不能被回收, 这样BitmapFactory在不停decodeByteArray创建新的Bitmap对象,             * 不同设备的内存不同,因此能够同时创建的Bitmap个数可能有所不同, 200个bitmap足以使大部分的设备重新OutOfMemory错误。 当isPurgable设为true时,系统中内存不足时,             * 可以回收部分Bitmap占据的内存空间,这时一般不会出现OutOfMemory 错误。             * API 21 时被忽略             */            opt.inPurgeable = true;            /**             * 配合inPurgeable使用,决定bitmap是否可以共享加载图片数据的引用,如果是false重新创建时会使用深层拷贝             */            opt.inInputShareable = true;            bmp = BitmapFactory.decodeResource(res, resId, opt);        }        return bmp;    }    public static Bitmap createBitmap(Bitmap oriBm) {        try {            return Bitmap.createBitmap(oriBm);        } catch (OutOfMemoryError e) {            System.gc();            LogUtil.log(e);            try {                LogUtil.e("System.out", "try to creat the bitmap again");                return Bitmap.createBitmap(oriBm);            } catch (Throwable t) {                LogUtil.log(t);            }        } catch (Throwable t) {            LogUtil.log(t);        } finally {            System.gc();        }        return null;    }    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) {        try {            return Bitmap.createBitmap(source, x, y, width, height, m, filter);        } catch (OutOfMemoryError e) {            System.gc();            LogUtil.log(e);            try {                LogUtil.e("System.out", "try to creat the bitmap again");                return Bitmap.createBitmap(source, x, y, width, height, m, filter);            } catch (Throwable t) {                LogUtil.log(t);            }        } catch (Throwable t) {            LogUtil.log(t);        } finally {            System.gc();        }        return null;    }    public static Bitmap createBitmap(int width, int height, Config config) {        try {            return Bitmap.createBitmap(width, height, config);        } catch (OutOfMemoryError e) {            System.gc();            LogUtil.log(e);            try {                LogUtil.e("System.out", "try to creat the bitmap again");                return Bitmap.createBitmap(width, height, config);            } catch (Throwable t) {                LogUtil.log(t);            }        } catch (Throwable t) {            LogUtil.log(t);        } finally {            System.gc();        }        return null;    }    public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {        try {            return Bitmap.createBitmap(source, x, y, width, height);        } catch (OutOfMemoryError e) {            System.gc();            LogUtil.log(e);            try {                LogUtil.e("System.out", "try to creat the bitmap again");                return Bitmap.createBitmap(source, x, y, width, height);            } catch (Throwable t) {                LogUtil.log(t);            }        } catch (Throwable t) {            LogUtil.log(t);        } finally {            System.gc();        }        return null;    }    public static Bitmap createBitmap(InputStream is) {        Options opt = new Options();        opt.inPreferredConfig = Config.RGB_565;        opt.inPurgeable = true;        opt.inInputShareable = true;        // opt.inSampleSize = 2;        return BitmapFactory.decodeStream(is, null, opt);    }    /** 使用c层代码早图片 */    public static Bitmap createBitmap(String path) {        FileInputStream fis = null;        try {            fis = new FileInputStream(path);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        return createBitmap(fis);    }    /** 回收不使用的bitmap */    public static void recyledBitmap(Bitmap bitmap) {        try {            if (bitmap != null) {                if (!bitmap.isRecycled()) {                    bitmap.recycle();                    bitmap = null;                }            }            System.gc();        } catch (Throwable e) {            e.printStackTrace();        }    }    /** 获取指定View的截图 */    public static Bitmap captureView(View view) throws Throwable {        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap bmShare = view.getDrawingCache();        if (bmShare == null) {            // 使用反射,强制将mViewFlags设置为正确的数值            Field mViewFlagsField = View.class.getDeclaredField("mViewFlags");            mViewFlagsField.setAccessible(true);            mViewFlagsField.set(view, Integer.valueOf(402685952));            bmShare = view.getDrawingCache();        }        return bmShare;    }    /** 获取屏幕的截图 */    public static String captureView(Context context, String filename) {        String path = Constant.TEMPORARY_FILE_PATH + filename;        try {            WindowManager windowManager = ((Activity) context).getWindowManager();            Display display = windowManager.getDefaultDisplay();            int w = display.getWidth();            int h = display.getHeight();            Bitmap bmShare = Bitmap.createBitmap(w, h, Config.ARGB_8888);            // 2.获取屏幕            View decorview = ((Activity) context).getWindow().getDecorView();            decorview.setDrawingCacheEnabled(true);            bmShare = decorview.getDrawingCache();            CompressFormat format = CompressFormat.JPEG;            OutputStream stream = new FileOutputStream(path);            bmShare.compress(format, 100, stream);        } catch (Exception e) {            e.printStackTrace();        }        return path;    }    /**     * 创建指定bitmap的倒影bitmap     *     * @param bitmap     *            原始的图片     * @param refMargin     *            倒影和原图之间的距离     * @param refLeng     *            倒影的长度     */    public static Bitmap getInvertedImage(Bitmap bitmap, int refMargin, int refLeng) {        int width = bitmap.getWidth();        int bmHeight = bitmap.getHeight();        int height = bmHeight + refMargin + refLeng;        Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);        Canvas canvas = new Canvas();        canvas.setBitmap(bm);        Paint paint = new Paint();        canvas.drawBitmap(bitmap, 0, 0, paint);        if (refLeng > 0) {            Matrix matrix = new Matrix();            matrix.preScale(1, -1);            Bitmap refBm = Bitmap.createBitmap(bitmap, 0, bmHeight - refLeng, width, refLeng, matrix, false);            canvas.drawBitmap(refBm, 0, bmHeight + refMargin, paint);            refBm.recycle();            paint.setAntiAlias(true);            LinearGradient gradient = new LinearGradient(0, bmHeight + refMargin, 0, height, 0xff000000, 0x0000000, Shader.TileMode.CLAMP);            paint.setShader(gradient);            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));            canvas.drawRect(0, bmHeight + refMargin, width, height, paint);        }        return bm;    }    public static Bitmap getImageReflectionPart(Bitmap bitmap, int refLeng) {        int width = bitmap.getWidth();        int bmHeight = bitmap.getHeight();        Bitmap bm = Bitmap.createBitmap(width, refLeng, Config.ARGB_8888);        Canvas canvas = new Canvas();        canvas.setBitmap(bm);        Paint paint = new Paint();        Matrix matrix = new Matrix();        matrix.preScale(1, -1);        Bitmap refBm = createBitmap(bitmap, 0, 0, width, bmHeight, matrix, true);        canvas.drawBitmap(refBm, 0, 0, paint);        refBm.recycle();        paint.setAntiAlias(true);        LinearGradient gradient = new LinearGradient(0, 0, 0, refLeng, 0xc0000000, 0x00000000, Shader.TileMode.CLAMP);        paint.setShader(gradient);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));        canvas.drawRect(0, 0, width, refLeng, paint);        return bm;    }    /**     * 生成阴影     *     * @param bitmap     * @param shadow     * @return     */    public static Bitmap getShadowBitmap(Bitmap bitmap, int shadow) {        if (shadow <= 0) {            return bitmap;        }        BlurMaskFilter filter = new BlurMaskFilter(shadow, BlurMaskFilter.Blur.OUTER);        Paint shadowPaint = new Paint();        shadowPaint.setMaskFilter(filter);        int[] offsetXY = new int[] { shadow, shadow };        return bitmap.extractAlpha(shadowPaint, offsetXY);    }    /**     * 获取图片的分辨率     *     * @param path     * @return     * @throws Throwable     */    public static int[] getBitmapSize(String path) throws Throwable {        Options opt = new Options();        opt.inJustDecodeBounds = true;        opt.inPreferredConfig = Config.RGB_565;        opt.inPurgeable = true;        opt.inInputShareable = true;        FileInputStream fis = new FileInputStream(path);        BitmapFactory.decodeStream(fis, null, opt);        int[] size = new int[] { opt.outWidth, opt.outHeight };        return size;    }    /**     * TODO 图片按比例大小压缩方法(根据路径获取图片并压缩)     *     * @param srcPath     *            SD卡路径     * @param reqWidth     *            需要的宽度     * @param reqHeight     *            需要的高度     * @return Bitmap     */    public static Bitmap CompressImageByPath(String srcPath, int reqWidth, int reqHeight) {        Options op = new Options();        op.inJustDecodeBounds = true;// 只是读图片的属性        BitmapFactory.decodeFile(srcPath, op);        if (reqWidth == 0 && reqHeight == 0) {            reqWidth = op.outWidth;            reqHeight = op.outHeight;        }        op.inSampleSize = computeSampleSize(op, -1, reqWidth * reqHeight);// 设置缩放比例        op.inJustDecodeBounds = false;        op.inPreferredConfig = Config.RGB_565;        return compressImage(BitmapFactory.decodeFile(srcPath, op), 80);// 压缩好比例大小后再进行质量压缩    }    /**     * 动态计算图片缩放大小的方法     *     * @param options     * @param minSideLength     * @param maxNumOfPixels     * @return     */    public static int computeSampleSize(Options options, int minSideLength, int maxNumOfPixels) {        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);        int roundedSize;        if (initialSize <= 8) {            roundedSize = 1;            while (roundedSize < initialSize) {                roundedSize <<= 1;            }        } else {            roundedSize = (initialSize + 7) / 8 * 8;        }        return roundedSize;    }    private static int computeInitialSampleSize(Options options, int minSideLength, int maxNumOfPixels) {        double w = options.outWidth;        double h = options.outHeight;        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));        if (upperBound < lowerBound) {            // return the larger one when there is no overlapping zone.            return lowerBound;        }        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {            return 1;        } else if (minSideLength == -1) {            return lowerBound;        } else {            return upperBound;        }    }    /**     *     * 质量压缩方法     *     * bitmap源 options开始压缩的质量 《=100     *     * @return 压缩之后的bitmap     */    public static Bitmap compressImage(Bitmap bitmap, int options) {        Bitmap bit = null;        try {            if (null == bitmap) {                return bit;            }            ByteArrayOutputStream bout = new ByteArrayOutputStream();            bitmap.compress(CompressFormat.JPEG, options, bout);// 质量压缩                                                                        // 100                                                                        // 表示不压缩            System.out.println("*****************大少****************:" + bout.toByteArray().length / 1024);            ByteArrayInputStream isBm = new ByteArrayInputStream(bout.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中            bit = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片            bout.flush();            bout.close();            isBm.close();            if (!bitmap.isRecycled()) {                bitmap.recycle();// 记得释放资源,否则会内存溢出            }        } catch (IOException e) {            e.printStackTrace();        }        return bit;    }    public static boolean storageState() {        String status = Environment.getExternalStorageState();        if (status.equals(Environment.MEDIA_MOUNTED)) {            return true;        } else {            return false;        }    }    // 压缩并获得流用于上传    public static InputStream getISByBitmap(Bitmap bitmap) {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        // 得到输出流        bitmap.compress(CompressFormat.PNG, 100, baos);        // 转输入流        InputStream isBm = new ByteArrayInputStream(baos.toByteArray());        return isBm;    }    // 根据时间创建名字    public static String callTime() {        long backTime = new Date().getTime();        Calendar cal = Calendar.getInstance();        cal.setTime(new Date(backTime));        int year = cal.get(Calendar.YEAR);        int month = cal.get(Calendar.MONTH) + 1;        int date = cal.get(Calendar.DAY_OF_MONTH);        int hour = cal.get(Calendar.HOUR_OF_DAY);        int minute = cal.get(Calendar.MINUTE);        int second = cal.get(Calendar.SECOND);        String time = "" + year + month + date + hour + minute + second;        return time;    }    public static Bitmap getScreenBitmap(View v) {        View view = v.getRootView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap bitmap = view.getDrawingCache();        bitmap = compressImage(bitmap);        return bitmap;    }    /**     * 将Bitmap压缩到一定程度     *     * @param image     * @return     */    public static Bitmap compressImage(Bitmap image) {        Bitmap bitmap;        try {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            image.compress(CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中            int options = 100;            while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩                baos.reset();// 重置baos即清空baos                image.compress(CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中                options -= 10;// 每次都减少10            }            ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中            bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片            isBm.reset();            isBm.close();        } catch (Exception e) {            bitmap = image;        }        return bitmap;    }    /**     * 截图功能     *     * View v 返回byte[]     */    public static byte[] shotScreen(View v) {        byte[] buff = null;        Bitmap bitmap = getScreenBitmap(v);        if (bitmap != null) {            ByteArrayOutputStream stream = new ByteArrayOutputStream();            bitmap.compress(CompressFormat.JPEG, 100, stream);// (0 -                                                                        // 100)压缩文件            buff = stream.toByteArray();            System.out.println("bitmap got!");            return buff;        } else {            return null;        }    }    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {        ByteArrayOutputStream output = new ByteArrayOutputStream();        bmp.compress(CompressFormat.PNG, 100, output);        if (needRecycle) {            bmp.recycle();        }        byte[] result = output.toByteArray();        try {            output.close();        } catch (Exception e) {            e.printStackTrace();        }        return result;    }    // byte(字节)根据长度转成kb(千字节)和mb(兆字节)    public static int bytes2kb(long bytes) {        BigDecimal filesize = new BigDecimal(bytes);        BigDecimal megabyte = new BigDecimal(1024 * 1024);        float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP).floatValue();        if (returnValue > 2) {            return 8;        } else if (returnValue > 1) {            return 6;        }        BigDecimal kilobyte = new BigDecimal(1024);        returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP).floatValue();        if (returnValue >= 700) {            return 4;        } else if (returnValue >= 500) {            return 3;        } else if (returnValue >= 300) {            return 2;        } else {            return 1;        }    }    public static Bitmap compressImage(Bitmap image, int reqWidth, int reqHeight) {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        image.compress(CompressFormat.JPEG, 100, baos);        if (baos.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出            baos.reset();// 重置baos即清空baos            image.compress(CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中        }        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());        Options newOpts = new Options();        // 开始读入图片,此时把options.inJustDecodeBounds 设回true了        newOpts.inJustDecodeBounds = true;        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);        newOpts.inJustDecodeBounds = false;        int w = newOpts.outWidth;        int h = newOpts.outHeight;        // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为        float hh = reqHeight;// 这里设置高度为800f        float ww = reqWidth;// 这里设置宽度为480f        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可        int be = 1;// be=1表示不缩放        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放            be = (int) (newOpts.outWidth / ww);        } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放            be = (int) (newOpts.outHeight / hh);        }        if (be <= 0)            be = 1;        newOpts.inSampleSize = be;// 设置缩放比例        // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了        isBm = new ByteArrayInputStream(baos.toByteArray());        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);        return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩    }    // 写到sdcard中    public static void write(Bitmap bm) throws IOException {        if (bm == null) {            return;        }        ByteArrayOutputStream baos = new ByteArrayOutputStream();        bm.compress(CompressFormat.PNG, 100, baos);// png类型        FileOutputStream out = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + File.separator + "tesstttt.png"));        out.write(baos.toByteArray());        out.flush();        out.close();    }    /**     *     * @param x     *            图像的宽度     * @param y     *            图像的高度     * @param image     *            源图片     * @param outerRadiusRat     *            圆角的大小     * @return 圆角图片     */    public static Bitmap createFramedPhoto(int x, int y, Bitmap image, float outerRadiusRat) {        // 根据源文件新建一个darwable对象        Drawable imageDrawable = new BitmapDrawable(image);        // 新建一个新的输出图片        Bitmap output = Bitmap.createBitmap(x, y, Config.ARGB_8888);        Canvas canvas = new Canvas(output);        // 新建一个矩形        RectF outerRect = new RectF(0, 0, x, y);        // 产生一个红色的圆角矩形        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);        paint.setColor(Color.RED);        canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint);        // 将源图片绘制到这个圆角矩形上        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        imageDrawable.setBounds(0, 0, x, y);        canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);        imageDrawable.draw(canvas);        canvas.restore();        return output;    }    public static Bitmap revitionImageSize(String path) throws IOException {        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));        Options options = new Options();        options.inJustDecodeBounds = true;        // Bitmap btBitmap=BitmapFactory.decodeFile(path);        // System.out.println("原尺寸高度:"+btBitmap.getHeight());        // System.out.println("原尺寸宽度:"+btBitmap.getWidth());        BitmapFactory.decodeStream(in, null, options);        in.close();        int i = 0;        Bitmap bitmap = null;        while (true) {            if ((options.outWidth >> i <= 800) && (options.outHeight >> i <= 800)) {                in = new BufferedInputStream(new FileInputStream(new File(path)));                options.inSampleSize = (int) Math.pow(2.0D, i);                options.inJustDecodeBounds = false;                bitmap = BitmapFactory.decodeStream(in, null, options);                break;            }            i += 1;        }        // 当机型为三星时图片翻转        // bitmap = Photo.photoAdapter(path, bitmap);        // System.out.println("-----压缩后尺寸高度:" + bitmap.getHeight());        // System.out.println("-----压缩后尺寸宽度度:" + bitmap.getWidth());        return bitmap;    }    public static Bitmap getLoacalBitmap(String url) {        try {            FileInputStream fis = new FileInputStream(url);            return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片        } catch (FileNotFoundException e) {            e.printStackTrace();            return null;        }    }    /** 手机截屏 */    public static Bitmap takeScreenShot(Activity activity) {        // View是你需要截图的View        View view = activity.getWindow().getDecorView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap b1 = view.getDrawingCache();        // 获取状态栏高度        Rect frame = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        int statusBarHeight = frame.top;        System.out.println(statusBarHeight);// 获取屏幕长和高        int width = activity.getWindowManager().getDefaultDisplay().getWidth();        int height = activity.getWindowManager().getDefaultDisplay().getHeight();// 去掉标题栏        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);        view.destroyDrawingCache();        return b;    }    /**     * @param context     * @return     */    public static int getScreenHeight(Context context) {        if (null == context) {            return 0;        }        DisplayMetrics dm = new DisplayMetrics();        dm = context.getApplicationContext().getResources().getDisplayMetrics();        return dm.heightPixels;    }    //api4.2以上才能使用的毛玻璃效果图        /*private void blur(Context context,Bitmap bkg, View view, float radius) {            Bitmap overlay = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);              Canvas canvas = new Canvas(overlay);              canvas.drawBitmap(bkg, -view.getLeft(), -view.getTop(), null);              RenderScript rs = RenderScript.create(context);              Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);              ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());              blur.setInput(overlayAlloc);              blur.setRadius(radius);              blur.forEach(overlayAlloc);              overlayAlloc.copyTo(overlay);              view.setBackground(new BitmapDrawable(context.getResources(), overlay));              rs.destroy();          }  */        /**毛玻璃效果图*/        public static Bitmap fastblur(Bitmap sentBitmap, int radius) {            // Stack Blur v1.0 from            // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html            //            // Java Author: Mario Klingemann <mario at quasimondo.com>            // http://incubator.quasimondo.com            // created Feburary 29, 2004            // Android port : Yahel Bouaziz <yahel at kayenko.com>            // http://www.kayenko.com            // ported april 5th, 2012            // This is a compromise between Gaussian Blur and Box blur            // It creates much better looking blurs than Box Blur, but is            // 7x faster than my Gaussian Blur implementation.            //            // I called it Stack Blur because this describes best how this            // filter works internally: it creates a kind of moving stack            // of colors whilst scanning through the image. Thereby it            // just has to add one new block of color to the right side            // of the stack and remove the leftmost color. The remaining            // colors on the topmost layer of the stack are either added on            // or reduced by one, depending on if they are on the right or            // on the left side of the stack.            //            // If you are using this algorithm in your code please add            // the following line:            //            // Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>            Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);            if (radius < 1) {                return (null);            }            int w = bitmap.getWidth();            int h = bitmap.getHeight();            int[] pix = new int[w * h];            bitmap.getPixels(pix, 0, w, 0, 0, w, h);            int wm = w - 1;            int hm = h - 1;            int wh = w * h;            int div = radius + radius + 1;            int r[] = new int[wh];            int g[] = new int[wh];            int b[] = new int[wh];            int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;            int vmin[] = new int[Math.max(w, h)];            int divsum = (div + 1) >> 1;            divsum *= divsum;            int dv[] = new int[256 * divsum];            for (i = 0; i < 256 * divsum; i++) {                dv[i] = (i / divsum);            }            yw = yi = 0;            int[][] stack = new int[div][3];            int stackpointer;            int stackstart;            int[] sir;            int rbs;            int r1 = radius + 1;            int routsum, goutsum, boutsum;            int rinsum, ginsum, binsum;            for (y = 0; y < h; y++) {                rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;                for (i = -radius; i <= radius; i++) {                    p = pix[yi + Math.min(wm, Math.max(i, 0))];                    sir = stack[i + radius];                    sir[0] = (p & 0xff0000) >> 16;                    sir[1] = (p & 0x00ff00) >> 8;                    sir[2] = (p & 0x0000ff);                    rbs = r1 - Math.abs(i);                    rsum += sir[0] * rbs;                    gsum += sir[1] * rbs;                    bsum += sir[2] * rbs;                    if (i > 0) {                        rinsum += sir[0];                        ginsum += sir[1];                        binsum += sir[2];                    } else {                        routsum += sir[0];                        goutsum += sir[1];                        boutsum += sir[2];                    }                }                stackpointer = radius;                for (x = 0; x < w; x++) {                    r[yi] = dv[rsum];                    g[yi] = dv[gsum];                    b[yi] = dv[bsum];                    rsum -= routsum;                    gsum -= goutsum;                    bsum -= boutsum;                    stackstart = stackpointer - radius + div;                    sir = stack[stackstart % div];                    routsum -= sir[0];                    goutsum -= sir[1];                    boutsum -= sir[2];                    if (y == 0) {                        vmin[x] = Math.min(x + radius + 1, wm);                    }                    p = pix[yw + vmin[x]];                    sir[0] = (p & 0xff0000) >> 16;                    sir[1] = (p & 0x00ff00) >> 8;                    sir[2] = (p & 0x0000ff);                    rinsum += sir[0];                    ginsum += sir[1];                    binsum += sir[2];                    rsum += rinsum;                    gsum += ginsum;                    bsum += binsum;                    stackpointer = (stackpointer + 1) % div;                    sir = stack[(stackpointer) % div];                    routsum += sir[0];                    goutsum += sir[1];                    boutsum += sir[2];                    rinsum -= sir[0];                    ginsum -= sir[1];                    binsum -= sir[2];                    yi++;                }                yw += w;            }            for (x = 0; x < w; x++) {                rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;                yp = -radius * w;                for (i = -radius; i <= radius; i++) {                    yi = Math.max(0, yp) + x;                    sir = stack[i + radius];                    sir[0] = r[yi];                    sir[1] = g[yi];                    sir[2] = b[yi];                    rbs = r1 - Math.abs(i);                    rsum += r[yi] * rbs;                    gsum += g[yi] * rbs;                    bsum += b[yi] * rbs;                    if (i > 0) {                        rinsum += sir[0];                        ginsum += sir[1];                        binsum += sir[2];                    } else {                        routsum += sir[0];                        goutsum += sir[1];                        boutsum += sir[2];                    }                    if (i < hm) {                        yp += w;                    }                }                yi = x;                stackpointer = radius;                for (y = 0; y < h; y++) {                    // Preserve alpha channel: ( 0xff000000 & pix[yi] )                    pix[yi] = ( 0xff000000 & pix[yi] ) | ( dv[rsum] << 16 ) | ( dv[gsum] << 8 ) | dv[bsum];                    rsum -= routsum;                    gsum -= goutsum;                    bsum -= boutsum;                    stackstart = stackpointer - radius + div;                    sir = stack[stackstart % div];                    routsum -= sir[0];                    goutsum -= sir[1];                    boutsum -= sir[2];                    if (x == 0) {                        vmin[y] = Math.min(y + r1, hm) * w;                    }                    p = x + vmin[y];                    sir[0] = r[p];                    sir[1] = g[p];                    sir[2] = b[p];                    rinsum += sir[0];                    ginsum += sir[1];                    binsum += sir[2];                    rsum += rinsum;                    gsum += ginsum;                    bsum += binsum;                    stackpointer = (stackpointer + 1) % div;                    sir = stack[stackpointer];                    routsum += sir[0];                    goutsum += sir[1];                    boutsum += sir[2];                    rinsum -= sir[0];                    ginsum -= sir[1];                    binsum -= sir[2];                    yi += w;                }            }            bitmap.setPixels(pix, 0, w, 0, 0, w, h);            return (bitmap);        }        /**将制定的Bitmap保存到文件*/        public  static void savePic(Bitmap b, String strFileName){            FileOutputStream fos = null;            try {                fos = new FileOutputStream(strFileName);                if (null != fos)                {                    b.compress(CompressFormat.PNG, 90, fos);                    fos.flush();                    fos.close();                }            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }}

3.数据相关的工具类

package com.lianjiu.b.common.utils;import java.math.BigDecimal;import java.math.RoundingMode;public class DecimalUtil {    /**     * 金钱乘法     *     * @param v1     * @param v2     * @return     */    public static String add(String v1, String v2) {        BigDecimal b1 = new BigDecimal(v1);        BigDecimal b2 = new BigDecimal(v2);        return b1.add(b2).toString();    }    /**     * 金钱乘法     *     * @param v1     * @param v2     * @return     */    public static String multiply(String v1, String v2) {        BigDecimal b1 = new BigDecimal(v1);        BigDecimal b2 = new BigDecimal(v2);        return b1.multiply(b2).toString();    }    /**     * 乘法     *     * @param v1    乘数     * @param v2    被乘数     * @param scale 小数点保留位数     * @return     */    public static String multiplyWithScale(String v1, String v2, int scale) {        BigDecimal b1 = new BigDecimal(v1);        BigDecimal b2 = new BigDecimal(v2);        BigDecimal result = b1.multiply(b2);        result = result.setScale(scale);        return result.toString();    }    /**     * 金钱除法     *     * @param v1     * @param v2     * @return     */    public static String divide(String v1, String v2) {        BigDecimal b1 = new BigDecimal(v1);        BigDecimal b2 = new BigDecimal(v2);        return b1.divide(b2).toString();    }    /**     * 如果除不断,产生无限循环小数,那么就会抛出异常,解决方法就是对小数点后的位数做限制     *     * @param v1     * @param v2 小数模式 ,DOWN,表示直接不做四舍五入,参考{@link RoundingMode}     * @return     */    public static String divideWithRoundingDown(String v1, String v2) {        return divideWithRoundingMode(v1, v2, RoundingMode.DOWN);    }    /** 选择小数部分处理方式 */    public static String divideWithRoundingMode(String v1, String v2,                                                RoundingMode roundingMode) {        return divideWithRoundingModeAndScale(v1, v2, RoundingMode.DOWN, 0);    }    /**     * 选择小数点后部分处理方式,以及保留几位小数     *     * @param v1           除数     * @param v2           被除数     * @param roundingMode 小数处理模式     * @param scale        保留几位     * @return     */    public static String divideWithRoundingModeAndScale(String v1, String v2,                                                        RoundingMode roundingMode, int scale) {        BigDecimal b1 = new BigDecimal(v1);        BigDecimal b2 = new BigDecimal(v2);        return b1.divide(b2, scale, roundingMode).toString();    }    /**     * 金钱减法     *     * @param v1     * @param v2     * @return     */    public static String subtract(String v1, String v2) {        BigDecimal b1 = new BigDecimal(v1);        BigDecimal b2 = new BigDecimal(v2);        return b1.subtract(b2).toString();    }}

4.文件相关的工具类

package com.lianjiu.b.common.utils;import android.annotation.TargetApi;import android.app.Activity;import android.content.ContentUris;import android.content.Context;import android.database.Cursor;import android.graphics.Bitmap;import android.net.Uri;import android.os.Environment;import android.provider.DocumentsContract;import android.provider.MediaStore;import com.lianjiu.b.base.AppApplication;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.DecimalFormat;import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.zip.GZIPInputStream;public class FileUtils {    private static String SDPATH = Environment.getExternalStorageDirectory() + "/";;    public static String getSDPATH() {        return SDPATH;    }    /**     * yckx图片存储的路径     * */    public static String FILEPAHT="/sdcard/yckx/";    /**     * yckx截屏临时文件     * */    public static String SHARE_FILEName="yckx_screen_temp";    /**     * 在SD卡上创建文件     */    public File creatSDFile(String fileName) throws IOException {        File file = new File(SDPATH + fileName);        file.createNewFile();        return file;    }    /**     * 在SD卡上创建目录     */    public static File creatSDDir(String dirName) {        File dir = new File(SDPATH + dirName);        dir.mkdir();        return dir;    }    /**     * 判断SD卡上的文件夹是否存在     */    public static boolean isFileExist(String fileName) {        File file = new File(fileName);        return file.exists();    }    /** 删除目录 */    public static boolean deleteDirectory(File dir) {        File[] bookFiles = dir.listFiles();        for (File bookFile : bookFiles) {            if (bookFile.isDirectory())                deleteDirectory(bookFile);            bookFile.delete();        }        return dir.delete();    }    /** 删除文件 */    public static boolean deleteFileByPath(String filePath) {        if (!StringUtils.isEmpty(filePath)) {            File delFile = new File(filePath);            if (delFile.exists()) {                return delFile.delete();            }        }        return false;    }    /**     * 删除某一目录     *      * @param path     *            路径     */    public static boolean deleteFolder(String path) {        File file = new File(path);        return deleteFolder(file);    }    /**     * 删除某一目录     *      * @param folder     *            删除的路径文件     */    public static boolean deleteFolder(File folder) {        boolean result = false;        try {            String childs[] = folder.list();            if (childs == null || childs.length <= 0) {                if (folder.delete()) {                    result = true;                }            } else {                for (int i = 0; i < childs.length; i++) {                    String childName = childs[i];                    String childPath = folder.getPath() + File.separator + childName;                    File filePath = new File(childPath);                    if (filePath.exists() && filePath.isFile()) {                        if (filePath.delete()) {                            result = true;                        } else {                            result = false;                            break;                        }                    } else if (filePath.exists() && filePath.isDirectory()) {                        if (deleteFolder(filePath)) {                            result = true;                        } else {                            result = false;                            break;                        }                    }                }                folder.delete();            }        } catch (Exception e) {            e.printStackTrace();            result = false;        }        return result;    }    /**     * 将一个InputStream里面的数据写入到SD卡中     */    public File write2SDFromInput(String path, String fileName, InputStream input) {        File file = null;        OutputStream output = null;        try// InputStream里面的数据写入到SD卡中的固定方法        {            creatSDDir(path);            file = creatSDFile(path + fileName);            output = new FileOutputStream(file);            byte buffer[] = new byte[4 * 1024];            while ((input.read(buffer)) != -1) {                output.write(buffer);            }            output.flush();        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                output.close();            } catch (Exception e) {                e.printStackTrace();            }        }        return file;    }    public static String getSizeKBString(int f) {        DecimalFormat fmt = new DecimalFormat("0.#");        String s = fmt.format(f) + "KB";        float f2 = f;        if (f2 > 1024) {            f2 = f / 1024.0f;            s = fmt.format(f2) + "MB";        }        return s;    }    public static String getSizeString(long f) {        DecimalFormat fmt = new DecimalFormat("0.#");        float f1 = 1;        if (f >= 1024) {            f1 = f / 1024.0f;        }        String s = fmt.format(f1) + "KB";        float f2 = 1;        if (f1 >= 1024) {            f2 = f1 / 1024.0f;            s = fmt.format(f2) + "MB";        }        return s;    }    public static boolean untieGzip(String GzipPath, String filePath) {        boolean mk = false;        try {            GZIPInputStream in = new GZIPInputStream(new FileInputStream(GzipPath));            OutputStream out = new FileOutputStream(filePath);            byte[] buf = new byte[1024];            int len;            while ((len = in.read(buf)) > 0) {                out.write(buf, 0, len);            }            in.close();            out.close();            mk = true;        } catch (Exception e) {            e.printStackTrace();        }        return mk;    }    /**     * 获取常用文件储存路径     *      * @return     */    public static String getSavePath() {        return getPath(false);    }    /**     * 获取临时储存路径     *      * @return     */    public static String getTemporarySavePath() {        return getPath(true);    }    /**     * 获取储存路径     *      * @param isTemporary     *            //是否是临时文件     * @return     */    public static String getPath(boolean isTemporary) {        String sdDir = "";        String childDir = isTemporary == true ? "/yckx/pro/" : "/yckx/";        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); // 判断sd卡是否存在        if (sdCardExist) {            sdDir = Environment.getExternalStorageDirectory() + childDir;// 获取跟目录        } else {            sdDir = AppApplication.getInstance().getFilesDir().getAbsolutePath() + childDir;        }        File file = new File(sdDir);        if (!file.exists()) {            file.mkdir();        }        return sdDir;    }    /**     * 获取系统的总内存(单位Byte)     *      * @return     */    public static long getTotalMemory() {        try {            FileReader localFileReader = new FileReader("/proc/meminfo");            BufferedReader localBufferedReader = new BufferedReader(localFileReader);            String memTotal = localBufferedReader.readLine();            localFileReader.close();            String regEx = "\\d{1,}";            Pattern p = Pattern.compile(regEx);            Matcher m = p.matcher(memTotal);            if (m.find()) {                return (Long.parseLong(m.group(0)) * 1024);            }        } catch (Throwable e) {            LogUtil.log(e);        }        return -1;    }    public static void writeFileToSD(InputStream is, String pathName, String fileName) {        String sdStatus = Environment.getExternalStorageState();        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {            LogUtil.d("XXTIMG", "SD card is not avaiable/writeable right now.");            return;        }        try {            File path = new File(pathName);            File file = new File(pathName + fileName);            if (!path.exists()) {                LogUtil.d("XXTIMG", "Create the path:" + pathName);                path.mkdir();            }            if (!file.exists()) {                LogUtil.d("XXTIMG", "Create the file:" + fileName);                file.createNewFile();            }        } catch (Exception e) {            LogUtil.e("XXTIMG", "Error on writeFilToSD.");            e.printStackTrace();        }    }    /**     * 根据文件路径,将图片文件存储到sdcard中     *     * */    public static void saveFileToSDcard(String filePath, Bitmap saveBitmap){        try {            FileOutputStream b = null;            File yckx_tempFile = new File("/sdcard/yckx/");            if (!yckx_tempFile.exists()) {                yckx_tempFile.mkdirs();// 创建文件夹            }            File file = new File("/sdcard/yckx/" + filePath);            b = new FileOutputStream(file);//写到存储卡            saveBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件        }catch (FileNotFoundException e){              e.printStackTrace();        }    }    /*  * 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换  * @param activity  * @param imageUri  * @author yaoxing  * @date 2014-10-12          */    @TargetApi(19)    public static String getImageAbsolutePath(Activity context, Uri imageUri) {        if (context == null || imageUri == null)            return null;        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) {            if (isExternalStorageDocument(imageUri)) {                String docId = DocumentsContract.getDocumentId(imageUri);                String[] split = docId.split(":");                String type = split[0];                if ("primary".equalsIgnoreCase(type)) {                    return Environment.getExternalStorageDirectory() + "/" + split[1];                }            } else if (isDownloadsDocument(imageUri)) {                String id = DocumentsContract.getDocumentId(imageUri);                Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));                return getDataColumn(context,contentUri, null, null);            } else if (isMediaDocument(imageUri)) {                String docId = DocumentsContract.getDocumentId(imageUri);                String[] split = docId.split(":");                String type = split[0];                Uri contentUri = null;                if ("image".equals(type)) {                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;                } else if ("video".equals(type)) {                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;                } else if ("audio".equals(type)) {                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;                }                String selection = MediaStore.Images.Media._ID + "=?";                String[] selectionArgs = new String[] { split[1] };                return getDataColumn(context, contentUri, selection, selectionArgs);            }        } // MediaStore (and general)        else if ("content".equalsIgnoreCase(imageUri.getScheme())) {            // Return the remote address            if (isGooglePhotosUri(imageUri))                return imageUri.getLastPathSegment();            return getDataColumn(context, imageUri, null, null);        }        // File        else if ("file".equalsIgnoreCase(imageUri.getScheme())) {            return imageUri.getPath();        }        return null;    }    public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {        Cursor cursor = null;        String column = MediaStore.Images.Media.DATA;        String[] projection = { column };        try {            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);            if (cursor != null && cursor.moveToFirst()) {                int index = cursor.getColumnIndexOrThrow(column);                return cursor.getString(index);            }        } finally {            if (cursor != null)                cursor.close();        }        return null;    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is ExternalStorageProvider.     */    public static boolean isExternalStorageDocument(Uri uri) {        return "com.android.externalstorage.documents".equals(uri.getAuthority());    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is DownloadsProvider.     */    public static boolean isDownloadsDocument(Uri uri) {        return "com.android.providers.downloads.documents".equals(uri.getAuthority());    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is MediaProvider.     */    public static boolean isMediaDocument(Uri uri) {        return "com.android.providers.media.documents".equals(uri.getAuthority());    }    /**     * @param uri The Uri to check.     * @return Whether the Uri authority is Google Photos.     */    public static boolean isGooglePhotosUri(Uri uri) {        return "com.google.android.apps.photos.content".equals(uri.getAuthority());    }}

5.图片相关工具类(压缩)

package com.lianjiu.b.common.utils;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;import com.lianjiu.b.base.Constant;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class ImageCompress {    public static File scal(String path) {        File outputFile = new File(path);        if (outputFile.exists()) {            long fileSize = outputFile.length();            final long fileMaxSize = 200 * 1024;            if (fileSize >= fileMaxSize) {                BitmapFactory.Options options = new BitmapFactory.Options();                options.inJustDecodeBounds = true;                BitmapFactory.decodeFile(path, options);                int height = options.outHeight;                int width = options.outWidth;                double scale = Math.sqrt((float) fileSize / fileMaxSize);                options.outHeight = (int) (height / scale);                options.outWidth = (int) (width / scale);                options.inSampleSize = (int) (scale + 0.5);                options.inJustDecodeBounds = false;                Bitmap bitmap = BitmapFactory.decodeFile(path, options);                outputFile = new File(Constant.TEMPORARY_FILE_PATH + "/" + System.currentTimeMillis() + ".jpg");                FileOutputStream fos = null;                try {                    fos = new FileOutputStream(outputFile);                    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);                    fos.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                Log.d("", "sss ok " + outputFile.length());                if (!bitmap.isRecycled()) {                    bitmap.recycle();                }            }        }        return outputFile;    }}

6.Json解析相关工具类(需要用到Gson的jar包)

package com.lianjiu.b.common.utils;import com.google.gson.Gson;import org.json.JSONException;import org.json.JSONObject;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;public class JsonReponseHandler{    /** listKey=pageEntity */    public static <T> List<T> getListFromJsonWithPageEntity(Object response, Type type)    {        return getListFromJson(response, type, "pageEntity");    }    /**     * 将服务器返回的JSON转化为List,两层,     *      * @param response     *            服务器的HTTP响应,JSON数据,之所以用object,是希望将msg.obj直接传送过来     * @param type     *            List的数据类型,如List<Comment>     * @     *            更新ListView的适配器     *     *            进度对话框     * @author 陈福荣, 吴格非     */    public static <T> List<T> getListFromJson(Object response, Type type, String listKey)    {        if (response == null)        {            return null;        }        JSONObject jo = null;        if (response instanceof JSONObject)        {            jo = (JSONObject) response;        } else if (response instanceof String)        {            try            {                jo = new JSONObject(response.toString());            } catch (JSONException e)            {                e.printStackTrace();            }        }        List<T> list = null;        try        {            // 取list            if (jo != null && !jo.equals(""))            {                String json = String.valueOf(jo.getJSONArray(listKey));                Gson gson = new Gson();                if (!"null".equals(json))                {// 服务端可能返回null                    list = gson.fromJson(json, type);                }                if (list == null)                {                    list = new ArrayList<T>();                }            }        } catch (JSONException e)        {            e.printStackTrace();        }        return list;    }}

7.数据相关工具类

package com.lianjiu.b.common.utils;import java.math.BigDecimal;import java.text.NumberFormat;/** * @Description  专门对数据进行数量 * @Author Created by LinXZ on 2016/8/31 00:08. */public class NumberUtils {    /***     * double 数据保存小数操作     *     * */   public static double scaleNumber(double targetNumber,int scale){       BigDecimal b1 = new BigDecimal(targetNumber);       return b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();   }    /**     *     * double 数据保留小数操作     * */    public static String sacleNumber2(double targetNumber, int scale ){        NumberFormat ddf1 = NumberFormat.getNumberInstance();        ddf1.setMaximumFractionDigits(scale);       return ddf1.format(targetNumber);    }    /**格式化距离*/    public static String formatNumberofDistance(float distance){        if(distance<1000){            return sacleNumber2(distance,1)+"m";        }else{            return sacleNumber2(distance/1000,2)+"km";        }    }    /**数量格式化*/    public static String forNumberCount(int count){        if (count>1000){            Double temp=Double.parseDouble(count+"");            return temp/10000+"万";        }        return count+"";    }    /***********************************高德地图与百度地图的经纬度转换*****************************/    /**     *     * http://blog.csdn.net/meegomeego/article/details/39927017     *     * 高德地图使用的是火星系坐标:GCJ-02     * 百度地图使用的是百度坐标:在GCJ-02的基础上进行BD-09加密     *     * */    private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;    /**     * 将 GCJ-02 坐标转换成 BD-09 坐标     * GoogleMap和高德map用的是同一个坐标系GCJ-02(高德地图装成为百度地图)     * */    public static double[] bd_encrypt(double gg_lat, double gg_lon) {        double bd_lat = 0.0;        double bd_lon = 0.0;        double location[] = new double[2];        double x = gg_lon, y = gg_lat;        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);        bd_lon = z * Math.cos(theta) + 0.0065;        bd_lat = z * Math.sin(theta) + 0.006;        location[0] = bd_lat;        location[1] = bd_lon;        return location;    }    /**     * 将 BD-09 坐标转换成 GCJ-02 坐标     * GoogleMap和高德map用的是同一个坐标系GCJ-02(百度地图转换高德地图)     * */    public static double[] bd_decrypt(double bd_lat, double bd_lon) {        double gg_lat = 0.0;        double gg_lon = 0.0;        double location[] = new double[2];        double x = bd_lon - 0.0065, y = bd_lat - 0.006;        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);        gg_lon = z * Math.cos(theta);        gg_lat = z * Math.sin(theta);        location[0] = gg_lat;        location[1] = gg_lon;        return location;    }}

8.手机应用相关的工具类

package com.lianjiu.b.common.utils;import android.content.Context;import android.content.pm.ApplicationInfo;import android.os.Build;import android.os.Environment;import android.telephony.TelephonyManager;import android.text.InputType;import android.view.ViewTreeObserver;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;import android.widget.RelativeLayout;import com.lianjiu.b.base.AppApplication;import java.io.File;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import java.util.UUID;/** * 手机基本信息 * * @author Linxz */public class PhoneUtil {    private TelephonyManager telephonyManager;    private Context context;    static PhoneUtil phoneinfo;    private Map<String, Object> infoMap;    public PhoneUtil(Context context) {        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);        this.context = context;        infoMap = new HashMap<String, Object>();    }    public static PhoneUtil getInstance(Context context) {        if (phoneinfo == null) {            phoneinfo = new PhoneUtil(context);        }        return phoneinfo;    }    /**     * 手机型号     *     * @return     * @time 2011-6-1 下午03:20:14     * @author:linyg     */    public String getModel() {        String model = "";        if (infoMap.containsKey("get_model")) {            model = (String) infoMap.get("get_model");        } else {            model = Build.MODEL;            infoMap.put("get_model", model);        }        return model;    }    /**     * 获取设备唯一码     *     * @param     * @return     */    public String getIMEI() {        boolean invalidDeviceID = false;        // 获取imei号        String deviceID = telephonyManager.getDeviceId();        LogUtil.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", deviceID);        if (deviceID == null) {            invalidDeviceID = true;        } else if (deviceID.length() == 0 || deviceID.startsWith("000000") || deviceID.equals                ("0") || deviceID.startsWith("111111")                || deviceID.startsWith("222222") || deviceID.startsWith("333333") || deviceID                .startsWith("333333")                || deviceID.startsWith("444444") || deviceID.startsWith("555555") || deviceID                .startsWith("666666")                || deviceID.startsWith("777777") || deviceID.startsWith("888888") || deviceID                .startsWith("999999")                || deviceID.startsWith("123456") || deviceID.startsWith("abcdef") || deviceID                .equals("unknown")) {            invalidDeviceID = true;        }        // 如果未获取到 2.2以上的系统才能使用        if (invalidDeviceID && Integer.parseInt(Build.VERSION.SDK) >= 9) {            try {                Class<?> c = Class.forName("android.os.SystemProperties");                Method get = c.getMethod("get", String.class, String.class);                deviceID = (String) (get.invoke(c, "ro.serialno", "unknown"));            } catch (Exception e) {            }            LogUtil.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", deviceID);            if (deviceID == null) {                invalidDeviceID = true;            } else if (deviceID.length() == 0 || deviceID.startsWith("000000") || deviceID.equals                    ("0") || deviceID.startsWith("111111")                    || deviceID.startsWith("222222") || deviceID.startsWith("333333") || deviceID                    .startsWith("333333")                    || deviceID.startsWith("444444") || deviceID.startsWith("555555") || deviceID                    .startsWith("666666")                    || deviceID.startsWith("777777") || deviceID.startsWith("888888") || deviceID                    .startsWith("999999")                    || deviceID.startsWith("123456") || deviceID.startsWith("abcdef") || deviceID                    .equals("unknown")) {                invalidDeviceID = true;            } else {                invalidDeviceID = false;            }        }        // 以上都无法获取到,则从sharepreference和SD卡        if (invalidDeviceID) {            String spDeviceId = SharedPreferenceUtil.getInstance(context).getString                    (SharedPreferenceUtil.IMEI);            if (spDeviceId != null && !spDeviceId.equals("")) {                deviceID = spDeviceId;            }            if (deviceID == null) {                invalidDeviceID = true;            } else if (deviceID.length() == 0 || deviceID.startsWith("000000") || deviceID.equals                    ("0") || deviceID.startsWith("111111")                    || deviceID.startsWith("222222") || deviceID.startsWith("333333") || deviceID                    .startsWith("333333")                    || deviceID.startsWith("444444") || deviceID.startsWith("555555") || deviceID                    .startsWith("666666")                    || deviceID.startsWith("777777") || deviceID.startsWith("888888") || deviceID                    .startsWith("999999")                    || deviceID.startsWith("123456") || deviceID.startsWith("abcdef") || deviceID                    .equals("unknown")) {                invalidDeviceID = true;            } else {                invalidDeviceID = false;            }        }        // 自动生成,并将保存在本地        if (invalidDeviceID) {            deviceID = UUID.randomUUID().toString();        }        if (!StringUtils.isEmpty(deviceID)) {            SharedPreferenceUtil.getInstance(context).putString(SharedPreferenceUtil.IMEI,                    deviceID);        }        LogUtil.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", deviceID);        return deviceID;    }    /**     * 手机号读取     */    public String getNativePhoneNumber() {        telephonyManager = (TelephonyManager) context                .getSystemService(Context.TELEPHONY_SERVICE);        String nativePhoneNumber = null;        nativePhoneNumber = telephonyManager.getLine1Number();        return nativePhoneNumber;    }    /**     * 获取是否debug模式     *     * @return     */    public static boolean isApkDebugable() {        try {            ApplicationInfo info = AppApplication.getInstance().getApplicationInfo();            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;        } catch (Exception e) {        }        return false;    }    /**     * 获取sd卡路径     *     * @return     */    public static String getSDPath() {        String sdDir = "";        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment                .MEDIA_MOUNTED); // 判断sd卡是否存在        if (sdCardExist) {            sdDir = Environment.getExternalStorageDirectory() + "/dbgs/";// 获取跟目录        } else {            sdDir = "/data/data/dbgs/";        }        File file = new File(sdDir);        if (!file.exists()) {            file.mkdir();        }        return sdDir;    }    /**     * 打卡手机软键盘     */    public  void openSoftkeyboard(Context context, EditText editText) {        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context                .INPUT_METHOD_SERVICE);        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);        editText.setFocusableInTouchMode(true);        editText.requestFocus();        //设置光标在文本的最后一个字        String draft =editText.getText().toString().trim();        editText.setSelection(draft.length());//set cursor to the end    }   /* */    /**     * 关闭软键盘     *//*    public static void keyBoardCancle(BaseActivity context) {        View view = context.getWindow().peekDecorView();        if (view != null) {            InputMethodManager inputmanger = (InputMethodManager) context.getSystemService            (Context.INPUT_METHOD_SERVICE);            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);        }    }    /**     * 监听软件盘的打开关闭状态     */    public void openOrCloseSoftboard(final EditText editText, final RelativeLayout            mainLayout) {        editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);        editText.setFocusableInTouchMode(true);        editText.requestFocus();        InputMethodManager inputManager = (InputMethodManager) editText.getContext()                .getSystemService(Context.INPUT_METHOD_SERVICE);        inputManager.showSoftInput(editText, 0);        mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(                new ViewTreeObserver.OnGlobalLayoutListener() {                    @Override                    public void onGlobalLayout() {                        int heightDiff = mainLayout.getRootView().getHeight() - mainLayout                                .getHeight();                        if (heightDiff > 100) { // 说明键盘是弹出状态                            //ToastUtil.show(getApplicationContext(), "弹出", 2000);                        } else {                            editText.setInputType(InputType.TYPE_NULL);                            editText.setFocusableInTouchMode(false);                            //ToastUtil.show(getApplicationContext(), "隐藏", 2000);                        }                    }                });    }    /**     * 获取手机屏幕分辨率     */    public static float getDensity(Context context) {        return context.getResources().getDisplayMetrics().density;    }    /**     * 获取当前包名     */    public String getAppPakage() {        try {            String pkName = context.getPackageName();            return pkName;        } catch (Exception e) {        }        return "";    }    /**     * 获取APP版本号     */    public int getAppVersionCode() {        try {            String pkName = context.getPackageName();            int versionCode = context.getPackageManager()                    .getPackageInfo(pkName, 0).versionCode;            return versionCode;        } catch (Exception e) {        }        return 0;    }    /**     * 获取APP版本名称     */    public String getAppVersionName() {        try {            String pkName = context.getPackageName();            String versionName = context.getPackageManager().getPackageInfo(                    pkName, 0).versionName;            return versionName;        } catch (Exception e) {        }        return "";    }    /**     * 判断是否需要版本更新     *     * @return     */    public boolean checkAppVersionName(String serverVersionName) {        String appVersionName = getAppVersionName();        int appVersion = Integer.parseInt(appVersionName.replace(".", ""));        int serverVersion = Integer.parseInt(serverVersionName.replace(".", ""));        if (appVersion > serverVersion) {            return false;        } else if (appVersion == serverVersion) {            return false;        } else {            return true;        }    }}

9.手机SharePreference存储工具类

package com.lianjiu.b.common.utils;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import com.google.gson.Gson;import com.lianjiu.b.module.entity.bean.CustBaseinfo;import com.lianjiu.b.module.entity.bean.UserInfo;public class SharedPreferenceUtil {    private static SharedPreferenceUtil sharedPreferenceUtil;    private static SharedPreferences sharedPreferences;//用户资料    public final static String CUSTBASEINFO="CustBaseInfo";//登录注册下的数据    public final static String CUSTBASEINFO_STORE="CustBaseInfo_Store";//商家门店信息    public final static String PHONENUM="PhoneNum";//用户注册的环信手机号    public final static String PASSWORD="PassWord";//用户注册环信密码    private final static String KEY = "dbgs_sharedpreferences";    public final static String GUIDE = "guide"; // 引导页    public final static String IMEI = "imei"; // 设备唯一码    public final static String TOKEN = "token"; // 用户token    public final static String SETMSG = "setmsg"; // 用户消息设置缓存    public final static String IMTOKEN = "imtoken"; // 用户imtoken    public final static String XGTOKEN = "xgtoken"; // 用户xgtoken    public final static String LBSSearchResponse = "LBSSearchResponse";    public final static String KEY_PUSH = "Key_push";    public final static String COMPANY_APPLY = "company_apply";    public final static String SEARCH_RECORD = "search_cord";//商家搜索记录    /**     * 搜索页面记录。 <br/><br/>Author: Create by Yu.Yao on 2016/9/26。     */    public final static String SEARCH_HISTORY = "search_history";    private SharedPreferenceUtil(Context context) {        sharedPreferences = context.getSharedPreferences(KEY, Context.MODE_PRIVATE);    }    public static SharedPreferenceUtil getInstance(Context context) {        if (sharedPreferenceUtil == null) {            sharedPreferenceUtil = new SharedPreferenceUtil(context);        }        return sharedPreferenceUtil;    }    /**     * 设置String类型值     *     * @param key     * @param value     */    public void putString(String key, String value) {        Editor editor = sharedPreferences.edit();        editor.putString(key, value);        editor.commit();    }    /**     * 设置long类型值     *     * @param key     * @param value     */    public void putLong(String key, long value) {        Editor editor = sharedPreferences.edit();        editor.putLong(key, value);        editor.commit();    }    /**     * 设置int类型值     *     * @param key     * @param value     */    public void putInt(String key, int value) {        Editor editor = sharedPreferences.edit();        editor.putInt(key, value);        editor.commit();    }    /**     * 设置Boolean类型值     *     * @param key     * @param value     */    public void putBoolean(String key, boolean value) {        Editor editor = sharedPreferences.edit();        editor.putBoolean(key, value);        editor.commit();    }    /**     * 设置Float类型值     *     * @param key     * @param value     */    public void putFloat(String key, float value) {        Editor editor = sharedPreferences.edit();        editor.putFloat(key, value);        editor.commit();    }    /**     * 读取String类型值,默认为""     *     * @param key     */    public String getString(String key) {        return sharedPreferences == null ? "" : sharedPreferences.getString(key, "");    }    /**     * 读取boolean类型值,默认为false;     *     * @param key     * @return     */    public boolean getBoolean(String key, boolean deafultValue) {        return sharedPreferences.getBoolean(key, deafultValue);    }    /**     * 读取int类型值,默认为0     *     * @param key     * @return     */    public int getInt(String key) {        return sharedPreferences.getInt(key, 0);    }    /**     * 读取long类型值,默认为0     *     * @param key     * @return     */    public long getLong(String key) {        return sharedPreferences.getLong(key, 0);    }    /**     * 读取float类型值,默认为0     *     * @param key     * @return     */    public float getFloat(String key) {        return sharedPreferences.getFloat(key, 0);    }    /**     * 判断是否存在此字段     */    public boolean has(String key) {        return sharedPreferences.contains(key);    }    // 移除某个字段    public void remove(String key) {        Editor editor = sharedPreferences.edit();        editor.remove(key);        editor.commit();    }    private final static String USER="user";    /**保存当前用户信息*/  /*  public  void saveUser(User user){        if (user!=null){            putString(USER,new Gson().toJson(user));            String token=user.getToken();            putString(TOKEN,token);        }    }    *//**获取当前用户信息*//*    public User getUser(){        String userJSON=getString(USER);        if (!StringUtils.isEmpty(userJSON))            return new Gson().fromJson(userJSON,User.class);        else            return null;    }*/    public static boolean isLogin(Context context) {        String token = SharedPreferenceUtil.getInstance(context).getString(TOKEN);        if (StringUtils.isEmpty(token))            return false;        else            return true;    }    public void cleanUserCookie() {        Editor editor = sharedPreferences.edit();        editor.clear();        editor.commit();    }    /**获取用户商家对于的商家ID*/    public static String getCustId(Context context){       String userStringInfo=SharedPreferenceUtil.getInstance(context).getString(CUSTBASEINFO);        UserInfo userInfo=new Gson().fromJson(userStringInfo,UserInfo.class);        if (userInfo!=null)            return userInfo.getCustid();        return "";    }    /**获取商家基本信息*/    public static CustBaseinfo getCustBseInf(Context context){        String userStringInfo=SharedPreferenceUtil.getInstance(context).getString(CUSTBASEINFO_STORE);        if (userStringInfo!=null){            CustBaseinfo custBaseinfo=new Gson().fromJson(userStringInfo,CustBaseinfo.class);            return custBaseinfo;        }        return null;    }    /**获取用户Id*/    public static String getUserId(Context context){        String userInfoString=SharedPreferenceUtil.getInstance(context).getString(CUSTBASEINFO);        if (userInfoString!=null){            UserInfo userInfo=new Gson().fromJson(userInfoString,UserInfo.class);            if (userInfo!=null)                return userInfo.getUserid();        }        return "";    }}

10.字符串工具类

package com.lianjiu.b.common.utils;import java.math.BigDecimal;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Various String utility functions. Most of the functions herein are * re-implementations of the ones in apache commons StringUtils. */public class StringUtils {    public static final String ERROR_TOAST = "网络环境不给力,请检查网络";    public static final String NO_MORE_DATE = "没有更多数据";    public static final String EMPTY = "";    public static final String SUCCESSCODE = "200";    public static final String SUCCESSDESC = "请求成功";    public static final String EMPTYNUM = "0";    public static final String BLANK_SPACE = " ";    public static String trim(String text) {        if (text == null)            return "";        return text.trim();    }    /**     * @param text     * @return     * @Description 判断字符串是否为空     * @author Created by qinxianyuzou on 2014-12-11.     */    public static boolean isEmpty(String text) {        return text == null || text.trim().compareTo("") == 0 || text.equals("null");    }    /**     * Description: 返回字符串本身,防空指针.<br/><br/>     * Author: Create by Yu.Yao on 2016/9/13.<br/><br/>     */    public static String removalNull(String defaultString, String text) {        if (isEmpty(text)) {            return defaultString;        }        return text;    }    /**     * Checks if a String is whitespace, empty ("") or null.</p>     */    public static boolean isBlank(String str) {        int strLen;        if (str == null || (strLen = str.length()) == 0) {            return true;        }        for (int i = 0; i < strLen; i++) {            if ((Character.isWhitespace(str.charAt(i)) == false)) {                return false;            }        }        return true;    }    public static String listToString(List<String> list, String separator) {        if (list == null || list.size() == 0)            return "";        StringBuilder string = new StringBuilder();        for (int i = 0; i < list.size(); i++) {            string.append(list.get(i));            if (i < list.size() - 1) {                string.append(separator);            }        }        return string.toString();    }    public static String stringArrayToString(String[] array, String separator) {        if (array == null || array.length == 0)            return "";        StringBuilder string = new StringBuilder();        for (int i = 0; i < array.length; i++) {            string.append(array[i]).append(separator);        }        return string.toString();    }    public final static int getIntValue(String str) {        if (str != null && str.length() > 0) {            try {                return Integer.parseInt(str);            } catch (Exception e) {            }        }        return 0;    }    public final static long getLongValue(String str) {        if (str != null && str.length() > 0) {            try {                return Long.parseLong(str);            } catch (Exception e) {            }        }        return 0;    }    public static String removeEmptyChar(String src) {        if (src == null || src.length() == 0)            return src;        return src.replaceAll("[\r]*[\n]*[ ]*[ ]*", "");    }    public static String getFileNameFromUrl(String url) {        // 名字不能只用这个        // 通过 ‘?’ 和 ‘/’ 判断文件名        String extName = "";        String filename;        int index = url.lastIndexOf('?');        if (index > 1) {            extName = url.substring(url.lastIndexOf('.') + 1, index);        } else {            extName = url.substring(url.lastIndexOf('.') + 1);        }        filename = hashKeyForDisk(url) + "." + extName;        return filename;    }    /**     * 一个散列方法,改变一个字符串(如URL)到一个散列适合使用作为一个磁盘文件名。     */    public static String hashKeyForDisk(String key) {        String cacheKey;        try {            final MessageDigest mDigest = MessageDigest.getInstance("MD5");            mDigest.update(key.getBytes());            cacheKey = bytesToHexString(mDigest.digest());        } catch (NoSuchAlgorithmException e) {            cacheKey = String.valueOf(key.hashCode());        }        return cacheKey;    }    private static String bytesToHexString(byte[] bytes) {        StringBuilder sb = new StringBuilder();        for (int i = 0; i < bytes.length; i++) {            String hex = Integer.toHexString(0xFF & bytes[i]);            if (hex.length() == 1) {                sb.append('0');            }            sb.append(hex);        }        return sb.toString();    }    public static String Md5(String string) {        if (string != null && !string.equals("")) {            try {                MessageDigest md5 = MessageDigest.getInstance("MD5");                char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};                byte[] md5Byte = md5.digest(string.getBytes("UTF8"));                StringBuffer sb = new StringBuffer();                for (int i = 0; i < md5Byte.length; i++) {                    sb.append(HEX[(int) (md5Byte[i] & 0xff) / 16]);                    sb.append(HEX[(int) (md5Byte[i] & 0xff) % 16]);                }                string = sb.toString();            } catch (Exception e) {            }        }        return string;    }    /**     * 根据各国的手机号码规则,检测输入     *     * @param code     * @param number     * @return     * @time 2011-7-22 上午09:41:04     * @author:linyg     */    public static boolean phoneNumberValid(String code, String number) {        // 手机号固定在5-20范围内        if (number.length() < 5 || number.length() > 20) {            return false;        }        String match = "";        if ("86".equals(code)) {// 中国            if (number.length() != 11) {                return false;            } else {                match = "^[1]{1}[0-9]{2}[0-9]{8}$";            }        }        // 正则匹配        if (!"".equals(match)) {            return number.matches(match);        }        return true;    }    public static boolean phoneNumberValid(String number) {        // 手机号固定在5-20范围内        if (number.length() < 5 || number.length() > 20) {            return false;        }        String match = "";        if (number.length() != 11) {            return false;        } else {            // match = "^[1]{1}[0-9]{2}[0-9]{8}$";            // match = "^(13[0-9]|14[5|7]|15[0-9]|17[0-9]|18[0-9])\\d{8}$";            match = "^(1[3456789])\\d{9}$";        }        // 正则匹配        if (!"".equals(match)) {            return number.matches(match);        }        return true;    }    /**     * 判断邮箱地址是否有效限     */    public static boolean isEmailAddValid(String address) {        if (address != null && address.length() > 0) {            char[] cAddress = address.toCharArray();            for (char c : cAddress) {                if (c > 127) {                    return false;                }            }            Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");            Matcher m = p.matcher(address);            return m.matches();        }        return false;    }    /**     * 判断密码是否有效     * <p>     * 0 -- 非法;1 -- 正确; 2 -- 不一致     */    public static int isPasswordValid(String password, String repeated) {        if (password != null) {            int len = password.length();            if (len >= 6 && len <= 16) {                char[] cPsw = password.toCharArray();                boolean wrongChar = false;                for (char c : cPsw) {                    if (c >= 128) { // 找到非ascii码                        wrongChar = true;                        break;                    }                }                if (!wrongChar) {                    return password.equals(repeated) ? 1 : 2;                }            }        }        return 0;    }    /**     * 过滤掉 \r 换行 \n回车     *     * @param str     * @return     */    public static String replaceBlank(String str) {        String dest = "";        if (str != null) {            Pattern p = Pattern.compile("\\r+|\n+");            Matcher m = p.matcher(str);            dest = m.replaceAll(" ");// .replaceAll(" +", " ");        }        return dest;    }    // 半角字符与全角字符混乱所致:这种情况一般就是汉字与数字、英文字母混用,可以避免由于占位导致的排版混乱问题了    public static String ToDBC(String input) {        char[] c = input.toCharArray();        for (int i = 0; i < c.length; i++) {            if (c[i] == 12288) {                c[i] = (char) 32;                continue;            }            if (c[i] > 65280 && c[i] < 65375)                c[i] = (char) (c[i] - 65248);        }        return new String(c);    }    public static String ListTOString(ArrayList<String> dataList) {        if (null == dataList || dataList.size() == 0) {            return "";        }        StringBuilder str_b = new StringBuilder();        for (String ss : dataList) {            str_b.append(ss);            str_b.append("$");        }        return str_b.substring(0, str_b.length() - 1);    }    public static <T> boolean isEmptyData(List<T> newData) {        if (newData == null) {            return true;        } else {            if (newData.size() == 0) {                return true;            } else {                return false;            }        }    }    /**     * 方法名称:transMapToString     * 传入参数:map     * 返回值:String 形如 username'chenziwen^password'1234     */    public static String transMapToString(Map map) {        Map.Entry entry;        StringBuffer sb = new StringBuffer();        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); ) {            entry = (Map.Entry) iterator.next();            sb.append(entry.getKey().toString()).append("'").append(null == entry.getValue() ? "" :                    entry.getValue().toString()).append(iterator.hasNext() ? "^" : "");        }        return sb.toString();    }    /**     * 方法名称:transStringToMap     * 传入参数:mapString 形如 username'chenziwen^password'1234     * 返回值:Map     */    public static Map transStringToMap(String mapString) {        Map map = new HashMap();        StringTokenizer items;        for (StringTokenizer entrys = new StringTokenizer(mapString, "^"); entrys.hasMoreTokens();             map.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())) : null))            items = new StringTokenizer(entrys.nextToken(), "'");        return map;    }    /**     * 判断字符串是不是全是数字     */    public static boolean isNumeric(String str) {        for (int i = str.length(); --i >= 0; ) {            if (!Character.isDigit(str.charAt(i))) {                return false;            }        }        return true;    }    /**     * 距离格式化     */    public static String formatDistance(float distance) {        DecimalFormat df = new DecimalFormat("0.#");        if (distance < 1000) {            return df.format(distance) + "m";        }        return df.format(distance / 1000) + "km";    }    public static String formatDistance(double distance) {        DecimalFormat df = new DecimalFormat("0.#");        if (distance < 1000) {            return df.format(distance) + "m";        }        return df.format(distance / 1000) + "km";    }    /*    校验过程: 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和。 3、将奇数位总和加上偶数位总和,结果应该可以被10整除。            */    /**     * 校验银行卡卡号     */    public static boolean checkBankCard(String bankCard) {        if (bankCard.length() < 15 || bankCard.length() > 19) {            return false;        }        char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));        if (bit == 'N') {            return false;        }        return bankCard.charAt(bankCard.length() - 1) == bit;    }    /**     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位     *     * @param nonCheckCodeBankCard     * @return     */    public static char getBankCardCheckCode(String nonCheckCodeBankCard) {        if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0                || !nonCheckCodeBankCard.matches("\\d+")) {            //如果传的不是数据返回N            return 'N';        }        char[] chs = nonCheckCodeBankCard.trim().toCharArray();        int luhmSum = 0;        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {            int k = chs[i] - '0';            if (j % 2 == 0) {                k *= 2;                k = k / 10 + k % 10;            }            luhmSum += k;        }        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');    }    /**     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1     *     * @param s 需要得到长度的字符串     * @return int 得到的字符串长度     */    public static int getStrLength(String s) {        if (s == null)            return 0;        char[] c = s.toCharArray();        int len = 0;        for (int i = 0; i < c.length; i++) {            len++;            if (!isLetter(c[i])) {                len++;            }        }        return len;    }    public static boolean isLetter(char c) {        int k = 0x80;        return c / k == 0 ? true : false;    }    /**     * 对字符串进行格式化,最多显示长度为length,超过时,其后的内容使用“...代替”     *     * @param str     * @param length     * @return     */    public static String regexString(String str, int length) {        if (str.length() > length) {            return str.substring(0, length - 4) + "...";        }        return str;    }    /**     * 对URL进行格式化:构造形式为URL/XXX/XXX     *     * @param params     * @param url     * @return     */    public static String formatUrl(List<Object> params, String url) {        StringBuilder builder = new StringBuilder();        builder.append(url);        for (Object param : params) {            builder.append("/" + param);        }        return builder.toString();    }    /** 拼接链接完整URL */    public static String generateUrl(String url, Map<String, Object> params) {        StringBuilder urlBuilder = new StringBuilder(url);        // web接口: url?key=val&key=val&key=val;        if (null != params) {            urlBuilder.append("?");            Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();            while (iterator.hasNext()) {                Map.Entry<String, Object> param = iterator.next();                String key = param.getKey();                Object value = param.getValue();                urlBuilder.append(key).append('=').append(value);                if (iterator.hasNext()) {                    urlBuilder.append('&');                }            }        }        return urlBuilder.toString();    }    /**     * 用于字符串为null会报错的控件     */    public static String getNoNUllString(String txt) {        if (txt == null)            return "";        return txt;    }    /**金额为分的格式 */    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";    /**     * 分转为元     * @param fen     * @return     */    public static String fromFenToYuan(int fen){        if(!String.valueOf(fen).matches(CURRENCY_FEN_REGEX)) {            return "金额格式有误";        }        String yuan = BigDecimal.valueOf(fen).divide(new BigDecimal(100)).toString();        return yuan;    }}

11.时间倒计时工具类

package com.lianjiu.b.common.utils;import android.os.Handler;import android.os.Message;import java.util.Timer;import java.util.TimerTask;/** * 时间倒计时工具类 *  * */public class TimerUtil {    private static TimerUtil instance;    private MyTimer timer;    /**倒计时时间,默认60s*/    private int TIME_DEF=60;    /***/    private int showTime;    /**是否正在进行倒计时*/    private boolean isRunning;    private TimerUtil(){    }    synchronized public static TimerUtil getInstance(){        if (instance==null)            instance=new TimerUtil();        return instance;    }    public void setTime(int time){        this.TIME_DEF=time;    }    public MyTimer startRunning(){        showTime=TIME_DEF;        timer=new MyTimer();        final TimerTask timerTask=new TimerTask() {            @Override            public void run() {                showTime--;                Message msg=new Message();                msg.what=showTime;                showTimeHandler.sendMessage(msg);            }        };        timer.schedule(timerTask, 1000, 1000);        return timer;    }    class MyTimer extends Timer {        public void resCancel(){            this.cancel();        };    }    private Handler showTimeHandler=new Handler(){        public void handleMessage(Message msg) {            if (msg.what<=0) {                if(timer!=null)                    timer.cancel();                timer=null;                if (mTimerCallBack!=null)                    mTimerCallBack.donInFinishTime(msg.what);                isRunning=false;            }else{                if (mTimerCallBack!=null)                    mTimerCallBack.doInRunTime(msg.what);            }        };    };    public interface TimerCallBack{        public void doInRunTime(int time);        public void donInFinishTime(int time);    }    private TimerCallBack mTimerCallBack;    public void setTimerCallBack(TimerCallBack timerCallBack){        this.mTimerCallBack=timerCallBack;    }}

12.时间相关工具类

package com.lianjiu.b.common.utils;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class TimeUtil {    /**     * 格式化时间 如:12小时前     *      * @param timestr     *            秒     */    public static String fmttoCN(String timestr) {        String timeText = null;        if (null == timestr || "".equals(timestr)) {            return "";        }        long time = Long.valueOf(timestr);        // SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");        // try {        // time = dateFormat.parse(timestr).getTime();        // } catch (ParseException e) {        // e.printStackTrace();        // }        Date dt = new Date();        long nowSec = dt.getTime();        long timediff = (nowSec - time) / 1000;        if (timediff < 60) {            // 小与1分钟显示 ‘刚刚’            timeText = "刚刚";        } else if (timediff >= 60 && timediff < 60 * 60) {            // 小于1小时 显示‘分钟’            timeText = String.valueOf((int) timediff / 60) + "分钟前";        } else if (timediff >= 60 * 60 && timediff < 24 * 60 * 60) {            // 小于24小时,则显示‘时’            timeText = String.valueOf((int) timediff / (60 * 60)) + "小时前";        } else if (timediff >= 24 * 60 * 60 && timediff < 30 * 24 * 60 * 60) {            // 小于1个月,则显示‘天’            timeText = String.valueOf((int) timediff / (24 * 60 * 60)) + "天前";        } else if (timediff >= 30 * 24 * 60 * 60 && timediff < 12 * 30 * 24 * 60 * 60) {            // 小于1年,则显示‘月’            timeText = String.valueOf((int) timediff / (30 * 24 * 60 * 60)) + "个月前";        } else if (timediff >= 12 * 30 * 24 * 60 * 60) {            // 大于1年显示‘年’            timeText = String.valueOf((int) timediff / (12 * 30 * 24 * 60 * 60)) + "年前";        }        return timeText;    }    /**     * 时间转换     * @param dataFormat     * @param timeStamp     * @return     */    public static String formatData(String dataFormat, long timeStamp) {        if (timeStamp == 0) {            return "";        }        timeStamp = timeStamp * 1000;        String result = "";        SimpleDateFormat format = new SimpleDateFormat(dataFormat);        result = format.format(new Date(timeStamp));        return result;    }    //  public static String timeToStr(String timestr) {    //      String timeText = null;    //      if (null == timestr || "".equals(timestr)) {    //          return "";    //      }    //      long time = Long.valueOf(timestr);    //      if(isSameDay(time)){    //          timeText = "今天"+longToTime(time, "kk:mm");    //      }else{    //              //      }    //      Date dt = new Date();    //      long nowSec = dt.getTime();    //      long timediff = (nowSec - time) / 1000;    //      if (timediff < 24 * 60 * 60) {    //          timeText = "今天"+longToTime(time, "kk:mm");    //      } else if (timediff >= 24 * 60 * 60 && timediff < 2 * 24 * 60 * 60) {    //          timeText = "昨天"+longToTime(time, "kk:mm");    //      } else{    //          timeText = longToTime(time, "yyyy-MM-dd kk:mm");    //      }    //      return timeText;    //  }    /**     * @author LuoB.     * @param oldTime 较小的时间     * @param newTime 较大的时间 (如果为空   默认当前时间 ,表示和当前时间相比)     * @return -1 :同一天.    0:昨天 .   1 :至少是前天.     * @throws ParseException 转换异常     */    public static String timeToStr(String oldTime){        try {            Date today;            //将下面的 理解成  yyyy-MM-dd 00:00:00 更好理解点            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");            String todayStr = format.format(new Date());            today = format.parse(todayStr);            long time = Long.valueOf(oldTime);            Date olddate  =  new Date(time);            //昨天 86400000=24*60*60*1000 一天            if((today.getTime()-olddate.getTime())>0 && (today.getTime()-olddate.getTime())<=86400000) {                return "昨天"+longToTime(time, "HH:mm");            }            else if((today.getTime()-olddate.getTime())<=0){ //至少是今天                return "今天"+longToTime(time, "HH:mm");            }            else{ //至少是前天                return longToTime(time, "yyyy-MM-dd HH:mm");            }        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return "";    }    /**     * @param time     *            时间     * @param level     *            参考Calendar     * @return "yyyy-MM-dd kk:mm:ss" 格式的时间     */    public static String longToTime(long time, int level) {        String format = "yyyy-MM-dd HH:mm:ss";        switch (level) {        case Calendar.MINUTE: {            format = "yyyy-MM-dd HH:mm";        }        break;        case Calendar.HOUR: {            format = "yyyy-MM-dd HH";        }        break;        case Calendar.DATE: {            format = "yyyy-MM-dd";        }        break;        case Calendar.MONTH: {            format = "yyyy-MM";        }        break;        case Calendar.YEAR: {            format = "yyyy";        }        break;        }        SimpleDateFormat sdf = new SimpleDateFormat(format);        return sdf.format(time);    }    public static String longToTime(long time, String format){        SimpleDateFormat sdf = new SimpleDateFormat(format);        return sdf.format(time);    }    /** 时间转换为long */    public static long timeToSecond(String time, int level) {        String format = "yyyy-MM-dd HH:mm:ss";        switch (level) {        case Calendar.MINUTE: {            format = "yyyy-MM-dd HH:mm";        }        break;        case Calendar.HOUR: {            format = "yyyy-MM-dd HH";        }        break;        case Calendar.DATE: {            format = "yyyy-MM-dd";        }        break;        case Calendar.MONTH: {            format = "yyyy-MM";        }        break;        case Calendar.YEAR: {            format = "yyyy";        }        break;        }        SimpleDateFormat sdf = new SimpleDateFormat(format);        Date date = null;        long second = 0;        try {            date = sdf.parse(time);        } catch (ParseException e) {            e.printStackTrace();        }        if (date != null) {            second = date.getTime();        }        return second;    }    /** 时间转换为long */    public static long timeToSecond2(String time, int level) {        String format = "yyyy年MM月dd日HH时:mm分:ss秒";        switch (level) {        case Calendar.MINUTE: {            format = "yyyy年MM月dd日HH时mm分";        }        break;        case Calendar.HOUR: {            format = "yyyy年MM月dd日HH时";        }        break;        case Calendar.DATE: {            format = "yyyy年MM月dd日";        }        break;        case Calendar.MONTH: {            format = "yyyy年MM月";        }        break;        case Calendar.YEAR: {            format = "yyyy年";        }        break;        }        SimpleDateFormat sdf = new SimpleDateFormat(format);        Date date = null;        long second = 0;        try {            date = sdf.parse(time);        } catch (ParseException e) {            e.printStackTrace();        }        if (date != null) {            second = date.getTime();        }        return second;    }    /** 时间转换为long */    public static long timeToSecond3(String time, String format) {        SimpleDateFormat sdf = new SimpleDateFormat(format);        Date date = null;        long second = 0;        try {            date = sdf.parse(time);        } catch (ParseException e) {            e.printStackTrace();        }        if (date != null) {            second = date.getTime();        }        return second;    }    public static String longToTime2(long time) {        String format = "yyyy/MM/dd";        SimpleDateFormat sdf = new SimpleDateFormat(format);        return sdf.format(time);    }    /**     * 将秒转换为时间格式(00:00)     *      * @param s     *            秒数     * @return     */    public static String secToTime(int s) {        String time = null;        int m = s / 60; // 分        s = s - (m * 60); // 秒        time = String.format("%02d:%02d", m, s);        return time;    }    // date    public static String getMonth(long time) {        Date date = new Date(time);        SimpleDateFormat formatter = new SimpleDateFormat("M");        formatter.format(date);        return formatter.format(date);    }    // date    public static String getDay(long time) {        Date date = new Date(time);        SimpleDateFormat formatter = new SimpleDateFormat("d");        formatter.format(date);        return formatter.format(date);    }    // date    public static String getWeek(long time) {        Date date = new Date(time);        SimpleDateFormat formatter = new SimpleDateFormat("EEEE");        formatter.format(date);        return formatter.format(date);    }    public static String[] getDatas() {        String[] datas = new String[60];        Calendar calendar = Calendar.getInstance();        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 EEEE");        datas[0] = format.format(calendar.getTime());        for (int i = 1; i < 60; i++) {            calendar.add(Calendar.DATE, 1);            datas[i] = format.format(calendar.getTime());        }        return datas;    }    public static String formatAmPmToCN(long time) {        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日ahh时mm分");        return sdf.format(new Date(time));    }    public static String formatAmPmToCN2(long time) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");        return sdf.format(new Date(time));    }    public static String formatAllAmPmToCN(long time) {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 a hh时mm分ss秒");        return sdf.format(new Date(time));    }    /**今天时间*/    public static String todayTime(String pattern){        SimpleDateFormat sdf=new SimpleDateFormat(pattern);        return sdf.format(new Date());    }    /**     * 字符串转换成日期     * @param str:time     * @param pattern:格式     * @return date:date     */    public static Date StrToDate(String str, String pattern) {        SimpleDateFormat format = new SimpleDateFormat(pattern);        Date date = null;        try {            date = format.parse(str);        } catch (ParseException e) {            e.printStackTrace();        }        return date;    }    /**格式化时间*/    public static String fmTime(Date date, String pattern){        SimpleDateFormat sdf=new SimpleDateFormat(pattern);        return sdf.format(date);    }    /**比较两个时间那个前,哪个后     *     * @param DATE1:开始时间     *     * @param DATE2:结束时间     *     * @param pattern:时间格式,如:"yyyy年MM月dd日"     *      * @return      *         1:DATE1在DATE2之前     *        -1:DATE1在DATE2之后     *         0:DATE1、DATE2同一时间     *      * */    public static int compare_date(String DATE1, String DATE2, String pattern) {        DateFormat df = new SimpleDateFormat(pattern);        try {            Date dt1 = df.parse(DATE1);            Date dt2 = df.parse(DATE2);            if (dt1.getTime() > dt2.getTime()) {                return 1;            } else if (dt1.getTime() < dt2.getTime()) {                return -1;            } else {                return 0;            }        } catch (Exception exception) {            exception.printStackTrace();        }        return 0;    }    /**     * 判断当前时间是否在上午9点到下午9点内     * @return     */    public static boolean compareIn9(){        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        try {            String today = todayTime("yyyy-MM-dd");            Date dt1 = df.parse(today+" 09:00:00");            Date dt2 = df.parse(today+" 20:00:00");            Date now = new Date();            if (now.getTime() >= dt1.getTime() && now.getTime() <=dt2.getTime()) {                return true;            } else {                return false;            }        } catch (Exception exception) {            exception.printStackTrace();        }        return false;    }    public static String hasTimeNow(long time){        long today = new Date().getTime();        long mis = today - time;        int hour = (int)(mis/(3600*1000));        int mu = (int) (mis/(60*1000));        return hour>0?hour+"小时":mu+"分钟";            }}

12.Toast相关工具类

package com.lianjiu.b.common.utils;import android.content.Context;import android.os.Handler;import android.os.Looper;import android.widget.Toast;public class ToastHelper {    private static Handler handler = new Handler(Looper.getMainLooper());    private static ToastHelper toast;    private static Toast toast1 = null;    private Context context;    private static Object synObj = new Object();    public static ToastHelper getInstance() {        if (toast == null) {            toast = new ToastHelper();        }        return toast;    }    public void init(Context context) {        this.context = context;    }    public Toast _toast(String str) {        return displayToastShort(str);    }    /**     * 显示Toast,duration为short     *     *     *            {@link Context} 当前窗体的上下文     * @param str     *            {@link String} 消息主体     *     */    public Toast displayToastShort(String str) {        Toast toast = Toast.makeText(context, str, Toast.LENGTH_SHORT);        toast.show();        return toast;    }    /**     * 显示Toast,duration为long     *     *     *            {@link Context} 当前窗体的上下文     * @param str     *            {@link String} 消息主体     *     */    public void displayToastLong(String str) {        Toast.makeText(context, str, Toast.LENGTH_LONG).show();    }    /**     * 快速关闭toast     * 不停的疯狂的点击某个按钮,触发了toast以后,toast内容会一直排着队的显示出来,不能很快的消失。这样可能会影响用户的使用。     *     *     *            {@link Context} 当前窗体的上下文     * @param str     *            {@link String} 消息主体     *     * */    public  void displayToastWithQuickClose(            final String str) {        new Thread(new Runnable() {            public void run() {                handler.post(new Runnable() {                    public void run() {                        synchronized (synObj) {                            if (toast1 != null) {                                toast1.setText(str);                                toast1.setDuration(Toast.LENGTH_SHORT);                            } else {                                toast1 = Toast.makeText(context, str,                                        Toast.LENGTH_SHORT);                            }                            toast1.show();                        }                    }                });            }        }).start();    }}

13.UI相关工具类:

/**     * 获取屏幕分辨率:宽     *     * @param context     * @return     */    public static int getScreenPixWidth(Context context) {        DisplayMetrics dm = new DisplayMetrics();        if (!(context instanceof Activity)) {            dm = context.getResources().getDisplayMetrics();            return dm.widthPixels;        }        WindowManager wm = ((Activity) context).getWindowManager();        if (wm == null) {            dm = context.getResources().getDisplayMetrics();            return dm.widthPixels;        }        wm.getDefaultDisplay().getMetrics(dm);        return dm.widthPixels;    }    /**     * 获取屏幕分辨率:高     *     * @param context     * @return     */    public static int getScreenPixHeight(Context context) {        DisplayMetrics dm = new DisplayMetrics();        if (!(context instanceof Activity)) {            dm = context.getResources().getDisplayMetrics();            return dm.heightPixels;        }        WindowManager wm = ((Activity) context).getWindowManager();        if (wm == null) {            dm = context.getResources().getDisplayMetrics();            return dm.heightPixels;        }        wm.getDefaultDisplay().getMetrics(dm);        return dm.heightPixels;    } /**     * 获取当前手机的独立像素     *     * @param context     * @return     */    public static float getDensity(Context context) {        return context.getResources().getDisplayMetrics().density;    }    /**     * dp2px     */    public static int dip2px(float dipValue) {        return (int) (dipValue * getDensity(AppApplication.getInstance()) + 0.5f);    }    /**     * px2dp     */    public static int px2dip(float pxValue) {        return (int) (pxValue / getDensity(AppApplication.getInstance()) + 0.5f);    }public static void openBrowser(Context context, String url) {        Intent intent = new Intent();        intent.setAction(Intent.ACTION_VIEW);        LogUtil.d("down", "" + url);        Uri content_url = Uri.parse(url);        intent.setData(content_url);        context.startActivity(intent);    }    public static void cancleAllNotification(Context context) {        NotificationManager nm = (NotificationManager)     context.getSystemService(Context.NOTIFICATION_SERVICE);        nm.cancelAll();    }/**     * 版本更新,显示加载的进度框     */    public static void showProgress(Context context,int maxProgress,OnDialogClickListener loadListener)    {        ProgressDialog progressDialog=new ProgressDialog(context);        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置水平的进度        progressDialog.setMax(maxProgress);        progressDialog.show();        loadListener.onClick();    }//使用Glide加载图片,需要Glide的jar包  /**带默认图*/    public static void imageNet(Context context, String url, ImageView view, boolean isLocal, int defaltIcon) {        if (!StringUtils.isEmpty(url) && url.startsWith("http")) {            imageNet2(context, url, view, isLocal, defaltIcon);        } else {            Glide.with(context).load(isLocal ? url : getImgUrl(url)).diskCacheStrategy(DiskCacheStrategy.SOURCE).dontAnimate().placeholder(defaltIcon).into(view);        }    }    /**不带默认图*/    public static void imageNet(Context context, String url, ImageView view, boolean isLocal) {        if (!StringUtils.isEmpty(url) && url.startsWith("http")) {            imageNet2(context, url, view, isLocal);        } else {            Glide.with(context).load(isLocal ? url : getImgUrl(url)).diskCacheStrategy(DiskCacheStrategy.SOURCE).dontAnimate().into(view);        }    }    public static void imageNet2(Context context, String url, ImageView view, boolean isLocal, int defaltIcon) {        Glide.with(context).load(isLocal ? url : url).diskCacheStrategy(DiskCacheStrategy.SOURCE).dontAnimate().placeholder(defaltIcon).into(view);    }    /**不带默认图*/    public static void imageNet2(Context context, String url, ImageView view, boolean isLocal ) {        Glide.with(context).load(isLocal ? url : url).diskCacheStrategy(DiskCacheStrategy.SOURCE).dontAnimate().into(view);    }    public static String getImgUrl(String url) {       // return Config.TEST_IMG_BASE_URL + url;        return "";    } /*     * 按正方形裁切图片     */    public static Bitmap ImageCrop(Bitmap bitmap) {        int w = bitmap.getWidth(); // 得到图片的宽,高        int h = bitmap.getHeight();        if (w == h) {            return bitmap;        } else {            int wh = w > h ? h : w;// 裁切后所取的正方形区域边长            int retX = w > h ? (w - h) / 2 : 0;// 基于原图,取正方形左上角x坐标            int retY = w > h ? 0 : (h - w) / 2;            // 下面这句是关键            return Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null, false);        }    }public static void deleteAllFiles(File root) {        File files[] = root.listFiles();        if (files != null)            for (File f : files) {                if (f.isDirectory()) { // 判断是否为文件夹                    deleteAllFiles(f);                    try {                        f.delete();                    } catch (Exception e) {                    }                } else {                    if (f.exists()) { // 判断是否存在                        deleteAllFiles(f);                        try {                            f.delete();                        } catch (Exception e) {                        }                    }                }            }    } /**     * TextView非空校验     */    public static boolean checkTv(BaseActivity activity, TextView ...tv) {        for (TextView textView:tv){            String temp = textView.getText().toString().trim();            if (StringUtils.isEmpty(temp)) {                return false;            }        }        return true;    }    /**     * TextView非空校验,空则吐司提示     */    public static boolean checkTv(BaseActivity activity, TextView tv, String msg) {        String temp = tv.getText().toString().trim();        if (StringUtils.isEmpty(temp)) {            activity.showToast(msg);            return false;        }        return true;    }    /**     * TextView非空校验,空则添加晃动动画效果+吐司提示     * */    public static boolean checkTvWithAnim(BaseActivity activity, TextView tv, String msg) {        String temp = tv.getText().toString().trim();        if (StringUtils.isEmpty(temp)) {            startShakeAnimation(tv);            activity.showToast(msg);            return false;        }        return true;    }    /**验证TextView中的文本长度至少N个字符*/    public static boolean checkTvLimit(BaseActivity activity,TextView tv,int limit,String msg){        String temp = tv.getText().toString().trim();        if (temp.length()<limit){            startShakeAnimation(tv);            activity.showToast(msg);            return false;        }        return true;    }    /**验证TextView中的文本长度至少N个字符*/    public static boolean checkTvLimit(BaseActivity activity,TextView tv,int limitlef,int limitright,String msg){        String temp = tv.getText().toString().trim();        if (temp.length()<limitlef||temp.length()>limitright){            startShakeAnimation(tv);            activity.showToast(msg);            return false;        }        return true;    }    /**     * 开始晃动动画     */    public static void startShakeAnimation(TextView textView) {        if (textView.getAnimation() == null) {            textView.setAnimation(shakeAnimation(4));        }        textView.startAnimation(textView.getAnimation());    }    /**     * 晃动动画     *     * @param counts 0.5秒钟晃动多少下     */    public static Animation shakeAnimation(int counts) {        TranslateAnimation animation = new TranslateAnimation(0, 10, 0, 0);        animation.setInterpolator(new CycleInterpolator(counts));        animation.setDuration(500);        return animation;    }    /**验证手机号码是否正确,不正确则提示,并晃动窗口*/    public static boolean checkPhoneNumberAvailable(BaseActivity activity,TextView phoneView){        String phoneNum=phoneView.getText().toString().trim();        if (StringUtils.isEmpty(phoneNum)){            activity.showToast("手机号不能为空");            startShakeAnimation(phoneView);            return false;        }else if (!StringUtils.phoneNumberValid(phoneNum)){            activity.showToast("请输入正确的手机号");            startShakeAnimation(phoneView);             return false;        }        return true;    }    /**     * 判断密码是否一致     */    public static boolean checkPwdSame(BaseActivity activity,TextView tv,TextView tvConfirm,String msg){        String temp=tv.getText().toString().trim();        String tempConfirm=tvConfirm.getText().toString().trim();        if(temp.equals(tempConfirm)){            return true;        }        activity.showToast(msg);        return false;    }
0 0
原创粉丝点击