实现简单的毕加索

来源:互联网 发布:windows gui编程matlab 编辑:程序博客网 时间:2024/04/27 18:44
/** * Created by Administrator on 2016/10/8. * 它有4个方法 * 1.获取数据 * 》先从内存中获取 * 》没有再从硬盘上获取 * 2.加载数据 * 从网络上下载 * 3.保持数据 * 》先保持到硬盘上 * 》再保持到内存中 * 4.删除数据 */public class MyCache {    static final String path = Environment.getExternalStorageDirectory() + File.separator + "/cache";    int type;    //内部存储    LruCache<String, Bitmap> innerCache;    //外部存储    File dir;    //这里是压缩是传入的源图片    ImageView imageView;    //是否对图片资料压缩    boolean isFlag;    public MyCache() {        this(10, 1);    }    public MyCache(int size, int type) {        this.type = type;        innerCache = new LruCache<>(size);        dir = new File(path);        if (!dir.exists())            dir.mkdir();    }    public void setImageView(ImageView imageView) {        this.imageView = imageView;    }    public void setFlag(boolean isFlag) {        this.isFlag = isFlag;    }    //获取数据    public Bitmap get(String key) {        //把传入的字符串做一定处理如www://hao123.com变为www___hao123_com        if (key == null)            return null;        key = key.replace(".", "_").replace("//", "_").replace(":", "_").replace("/","_");        Bitmap value = null;        //从内存中获取        value = innerCache.get(key);        if (value != null)            return value;        File file = new File(dir, key + ".jpeg");        if (!file.exists())            return value;        //从外部文件获取        byte[] datas = getFromFile(file);        return compressBitmap(datas);    }    //从网上加载并显示出来    public Bitmap loadDataFromNet(String key) {        byte[] data = getDataFromNet(key);        if (data == null)            return null;        //保持到内存中        Bitmap bitmap = compressBitmap(data);        saveInner(key, bitmap);        //保持到硬盘上        saveOutter(key, bitmap);        return bitmap;    }    private void saveInner(String key, Bitmap bitmap) {        key = key.replace(".", "_").replace("//", "_").replace(":", "_").replace("/","_");        innerCache.put(key, bitmap);    }    private void saveOutter(String key, Bitmap bitmap) {        key = key.replace(".", "_").replace("//", "_").replace(":", "_").replace("/","_");        FileOutputStream fos = null;        try {            fos = new FileOutputStream(new File(dir, key + ".jpeg"));            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);        } catch (Exception e) {            e.printStackTrace();        } finally {            if (fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    //从外部文件上获取图片的字节对象    private byte[] getFromFile(File file) {        BufferedInputStream bis = null;        try {            ByteArrayOutputStream bos = new ByteArrayOutputStream();            bis = new BufferedInputStream(new FileInputStream(file));            byte[] bufs = new byte[2048];            int len = -1;            while ((len = bis.read(bufs)) != -1) {                bos.write(bufs, 0, len);            }            bos.flush();            return bos.toByteArray();        } catch (Exception e) {            e.printStackTrace();        } finally {            if (bis == null) {                try {                    bis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return null;    }    //对图片的压缩与否    public Bitmap compressBitmap(byte[] datas) {        if (imageView == null && !isFlag)            return BitmapFactory.decodeByteArray(datas, 0, datas.length);        BitmapFactory.Options options = new BitmapFactory.Options();        if (imageView != null) {            options.inJustDecodeBounds = true;            BitmapFactory.decodeByteArray(datas, 0, datas.length, options);            int width = options.outWidth;            int height = options.outHeight;            imageView.measure(0, 0);            int imagWidth = imageView.getMeasuredWidth();            int imagHeight = imageView.getMeasuredHeight();            int temp1 = width / imagWidth;            int temp2 = height / imagHeight;            temp1 = temp1 > temp2 ? temp1 : temp2;            options.inSampleSize = temp1 > 1 ? temp1 : 1;        }        if (isFlag)            options.inPreferredConfig = Bitmap.Config.RGB_565;        options.inJustDecodeBounds = false;        return BitmapFactory.decodeByteArray(datas, 0, datas.length, options);    }    //删去指定文件夹下所有的文件    public void deleteAll() {        if (!dir.exists())            return;        deleteData(dir);    }    private void deleteData(File root) {        File[] files = root.listFiles();        if (files != null) {            for (File file : files) {                deleteData(file);            }        }        root.delete();    }    private byte[] getDataFromNet(String path) {        BufferedInputStream bis = null;        try {            URL url = new URL(path);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setConnectTimeout(5000);            connection.setRequestMethod("GET");            if (connection.getResponseCode() == 200) {                bis = new BufferedInputStream(connection.getInputStream());                ByteArrayOutputStream bos = new ByteArrayOutputStream();                byte[] bufs = new byte[2048];                int len = -1;                while ((len = bis.read(bufs)) != -1) {                    bos.write(bufs, 0, len);                }                bos.flush();                return bos.toByteArray();            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (bis == null) {                try {                    bis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return null;    }}






/** * Created by Administrator on 2016/10/8. */public class MyPicaso {    private static ExecutorService threadPool = Executors.newFixedThreadPool(30);    private static Context context;    private static MyPicaso instance;    private String path;    private int id;    private static MyCache cache;    boolean compress;    private MyPicaso() {    }    //单列模式    public static MyPicaso with(Context temp) {        if (instance != null)            return instance;        instance = new MyPicaso();        //防止内存泄漏        context = temp.getApplicationContext();        cache = new MyCache();        return instance;    }    //加载地址    public MyPicaso load(String path) {        this.path = path;        return this;    }    //设置站位图片    public MyPicaso placeHolder(int id) {        this.id = id;        return this;    }    //是否压缩    public MyPicaso setCompress(boolean compress) {        this.compress = compress;        return this;    }    //把获得图片设置上去    public void into(final ImageView imageView) {        Bitmap bitmap = cache.get(path);        if (bitmap != null) {            imageView.setImageBitmap(bitmap);            return;        }        if (compress) {            cache.setImageView(imageView);            cache.setFlag(true);        }        final String temp = path;        imageView.setTag(temp);//设置标记        imageView.setImageResource(id);        threadPool.execute(new Runnable() {            @Override            public void run() {                final Bitmap bitmap1 = cache.loadDataFromNet(temp);                //这里的判断主要是为了防止复用造成的图片错位                if (imageView.getTag() != null && imageView.getTag().equals(temp)) {                    imageView.post(new Runnable() {                        @Override                        public void run() {                            imageView.setImageBitmap(bitmap1);                        }                    });                }            }        });    }    public void deleteCache(){        cache.deleteAll();    }}
 
0 0
原创粉丝点击