当前位置: 首页 > news >正文

手机网站开发 教程免费咨询个税

手机网站开发 教程,免费咨询个税,简单的app开发,wordpress 添加自定义小工具栏手机客户端以列表形式展示数据是非常常见的一种方式。然而列表中要显示图片#xff08;比如#xff1a;头像#xff09;就要采用异步线程加载的方式#xff0c;这样做是为了防止加载图片数据的时候#xff0c;花费时间过长#xff0c;阻塞UI线程#xff0c;从而达到保持…手机客户端以列表形式展示数据是非常常见的一种方式。然而列表中要显示图片比如头像就要采用异步线程加载的方式这样做是为了防止加载图片数据的时候花费时间过长阻塞UI线程从而达到保持App的流畅性的目的。 下面我将分享 OSChina.NET Android版客户端的列表异步线程加载图片的方法   图片缓存 private static HashMapString, SoftReferenceBitmap cache; 图片缓存是当有加载过相同的图片的时候可以快速重复使用比如同一个人的头像。 图片控件集合 private static MapImageView, String imageViews; 图片控件集合是一个Map记录当前ImageView控件对应的图片地址用来防止异步线程加载图片时候ImageView控件显示的图片与实际图片地址对应的图片不符出现错乱。 线程池 private static ExecutorService pool; 固定线程池里的并发线程数可以防止用户在快速滑动列表的时候不执行已经滑过去的加载线程。 具体的初始化代码 static { cache new HashMapString, SoftReferenceBitmap(); pool Executors.newFixedThreadPool(5); //固定线程池imageViews Collections.synchronizedMap(new WeakHashMapImageView, String());} 接下来我们来看看具体是如何加载图片的 public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp, int width, int height) { imageViews.put(imageView, url); Bitmap bitmap getBitmapFromCache(url); if (bitmap ! null) { //显示缓存图片imageView.setImageBitmap(bitmap); } else { //加载SD卡中的图片缓存String filename FileUtils.getFileName(url);String filepath imageView.getContext().getFilesDir() File.separator filename;File file new File(filepath);if(file.exists()){//显示SD卡中的图片缓存Bitmap bmp ImageUtils.getBitmap(imageView.getContext(), filename);imageView.setImageBitmap(bmp);}else{//线程加载网络图片imageView.setImageBitmap(defaultBmp);queueJob(url, imageView, width, height);}} } 上面的代码中我们根据图片的url地址先从图片缓存里面查找是否已缓存过如果没有再从SD卡的图片缓存文件中查找如果再没有最后才是加载网络图片。这才是以最快速的方式显示图片。 下面我将贴出完整的代码 /*** 异步线程加载图片工具类* author liux*/ public class BitmapManager { private static HashMapString, SoftReferenceBitmap cache; private static ExecutorService pool; private static MapImageView, String imageViews; private Bitmap defaultBmp; static { cache new HashMapString, SoftReferenceBitmap(); pool Executors.newFixedThreadPool(5); //固定线程池imageViews Collections.synchronizedMap(new WeakHashMapImageView, String());} public BitmapManager(){}public BitmapManager(Bitmap def) {this.defaultBmp def;}/*** 设置默认图片* param bmp*/public void setDefaultBmp(Bitmap bmp) { defaultBmp bmp; } /*** 加载图片* param url* param imageView*/public void loadBitmap(String url, ImageView imageView) { loadBitmap(url, imageView, this.defaultBmp, 0, 0);}/*** 加载图片-可设置加载失败后显示的默认图片* param url* param imageView* param defaultBmp*/public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp) { loadBitmap(url, imageView, defaultBmp, 0, 0);}/*** 加载图片-可指定显示图片的高宽* param url* param imageView* param width* param height*/public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp, int width, int height) { imageViews.put(imageView, url); Bitmap bitmap getBitmapFromCache(url); if (bitmap ! null) { //显示缓存图片imageView.setImageBitmap(bitmap); } else { //加载SD卡中的图片缓存String filename FileUtils.getFileName(url);String filepath imageView.getContext().getFilesDir() File.separator filename;File file new File(filepath);if(file.exists()){//显示SD卡中的图片缓存Bitmap bmp ImageUtils.getBitmap(imageView.getContext(), filename);imageView.setImageBitmap(bmp);}else{//线程加载网络图片imageView.setImageBitmap(defaultBmp);queueJob(url, imageView, width, height);}} } /*** 从缓存中获取图片* param url*/public Bitmap getBitmapFromCache(String url) { Bitmap bitmap null;if (cache.containsKey(url)) { bitmap cache.get(url).get(); } return bitmap; } /*** 从网络中加载图片* param url* param imageView* param width* param height*/public void queueJob(final String url, final ImageView imageView, final int width, final int height) { final Handler handler new Handler() { public void handleMessage(Message msg) { String tag imageViews.get(imageView); if (tag ! null tag.equals(url)) { if (msg.obj ! null) { imageView.setImageBitmap((Bitmap) msg.obj); try {//向SD卡中写入图片缓存ImageUtils.saveImage(imageView.getContext(), FileUtils.getFileName(url), (Bitmap) msg.obj);} catch (IOException e) {e.printStackTrace();}} } } }; pool.execute(new Runnable() { public void run() { Message message Message.obtain(); message.obj downloadBitmap(url, width, height); handler.sendMessage(message); } }); } /*** 下载图片-可指定显示图片的高宽* param url* param width* param height*/private Bitmap downloadBitmap(String url, int width, int height) { Bitmap bitmap null;try {//http加载图片bitmap ApiClient.getNetBitmap(url);if(width 0 height 0) {//指定显示图片的高宽bitmap Bitmap.createScaledBitmap(bitmap, width, height, true);} //放入缓存cache.put(url, new SoftReferenceBitmap(bitmap));} catch (Exception e) {e.printStackTrace();}return bitmap; } } 工具类使用 实例化时可以设置默认的显示图片:  BitmapManager bmpManager new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.loading)); 调用加载图片的方法: bmpManager.loadBitmap(imageURL, imageView);转载于:https://www.cnblogs.com/daocaowu/p/3172739.html
http://www.huolong8.cn/news/215797/

相关文章:

  • 淘宝优惠券 如果做网站.net网站开发后编译
  • 网站建设海报素材如何写网络营销策划方案
  • 西安本地十家做网站建设的公司学做网站培训 上海
  • 苏宁易购网站建设水平qq推广的方式有哪几种
  • 滨湖网站建设做网站的好公司
  • 邯郸网站建设产品介绍iis6添加网站
  • 沂水网站优化西安网站托管
  • 深圳网站建设如何制作设计本电脑
  • 跨境电商自建站是什么意思浙江网站建设哪家好
  • 如何做推广链接排名优化网站
  • 一个域名能同时做2个网站吗西安市住房和城乡建设局官方网站
  • 建设通网站原理赣州哪里可以做网站
  • 模板网站下载网站的代理页面怎么做
  • 国外品牌网站建设wordpress可以承受多大数据
  • 专业做鞋子的网站搜索引擎优化什么意思
  • 深圳网站优化包年网站视觉规范怎么做
  • 网站建设考试知识点APP网站开发私人订制
  • 贵州专业建网站哪里有做投票的网站
  • 如何删除网站备案号wordpress字体抖动
  • 地方招聘网站如何做推广网站多数关键词
  • 网站域名怎么免费获取5000元可注册劳务公司吗
  • 国外网站做freelancer电子商务做网站设计
  • 织梦移动端网站建设做网站教学
  • 恒基建设集团网站广东网页设计
  • 济南网站制做网站建设通报
  • 荆州做网站公司郑州百度网站建设
  • 企业网站一般内容包括哪些wordpress文章默认经典
  • 医院可以做网站吗在本地做装修在那个网站好
  • 网站搜索引擎关键字怎么做网站建设w亿玛酷1流量订制
  • 做网站什么语言最好青海省公路建设管理局官方网站