首页 技术 正文
技术 2022年11月10日
0 收藏 769 点赞 3,976 浏览 2793 个字

wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改。分享其中关于 保存正在下载的图片URL集合和图片三种获取方式管理者,网络URL获取、内存缓存获取、外部文件缓存获取的代码供技术员学习参考使用。

package com.inuoer.util;import java.lang.ref.SoftReference;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.content.Context;import android.graphics.Bitmap;import android.os.Handler;public class AsyncImageLoader {// 保存正在下载的图片URL集合,避免重复下载用private static HashSet<String> sDownloadingSet;// 软引用内存缓存private static Map<String, SoftReference<Bitmap>> sImageCache;// 图片三种获取方式管理者,网络URL获取、内存缓存获取、外部文件缓存获取private static LoaderImpl impl;// 线程池相关private static ExecutorService sExecutorService;// 通知UI线程图片获取ok时使用private Handler handler;/** * 异步加载图片完毕的回调接口 */public interface ImageCallback {/** * 回调函数 * * @param bitmap *            : may be null! * @param imageUrl */public void onImageLoaded(Bitmap bitmap, String imageUrl);}static {sDownloadingSet = new HashSet<String>();sImageCache = new HashMap<String, SoftReference<Bitmap>>();impl = new LoaderImpl(sImageCache);}public AsyncImageLoader(Context context) {handler = new Handler();startThreadPoolIfNecessary();String defaultDir = context.getCacheDir().getAbsolutePath();setCachedDir(defaultDir);}/** * 是否缓存图片至/data/data/package/cache/目录 默认不缓存 */public void setCache2File(boolean flag) {impl.setCache2File(flag);}/** * 设置缓存路径,setCache2File(true)时有效 */public void setCachedDir(String dir) {impl.setCachedDir(dir);}/** 开启线程池 */public static void startThreadPoolIfNecessary() {if (sExecutorService == null || sExecutorService.isShutdown()|| sExecutorService.isTerminated()) {sExecutorService = Executors.newFixedThreadPool(3);// sExecutorService = Executors.newSingleThreadExecutor();}}/** * 异步下载图片,并缓存到memory中 * * @param url * @param callback *            see ImageCallback interface */public void downloadImage(final String url, final ImageCallback callback) {downloadImage(url, true, callback);}/** * * @param url * @param cache2Memory *            是否缓存至memory中 * @param callback */public void downloadImage(final String url, final boolean cache2Memory,final ImageCallback callback) {if (sDownloadingSet.contains(url)) {//Log.i("AsyncImageLoader", "###该图片正在下载,不能重复下载!");return;}Bitmap bitmap = impl.getBitmapFromMemory(url);if (bitmap != null) {if (callback != null) {callback.onImageLoaded(bitmap, url);}} else {// 从网络端下载图片sDownloadingSet.add(url);sExecutorService.submit(new Runnable() {@Overridepublic void run() {final Bitmap bitmap = impl.getBitmapFromUrl(url,cache2Memory);handler.post(new Runnable() {@Overridepublic void run() {if (callback != null)callback.onImageLoaded(bitmap, url);sDownloadingSet.remove(url);}});}});}}/** * 预加载下一张图片,缓存至memory中 * * @param url */public void preLoadNextImage(final String url) {// 将callback置为空,只将bitmap缓存到memory即可。downloadImage(url, null);}}

  

wemall-mobile详情下载地址:http://www.koahub.com/home/product/56

wemall官网地址:http://www.wemallshop.com

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐—专业的o2o系统

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905