do a lazy load of images in ListView

本文介绍了一种在Android应用中ListView组件实现图片懒加载的方法。通过使用SoftReference管理Drawable资源来减少内存消耗,并利用线程异步加载图片,提高用户体验。


http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/2773496#2773496
public DrawableManager() {
    drawableMap
= new HashMap<String, SoftReference<Drawable>>();
}

public Drawable fetchDrawable(String urlString) {
   
SoftReference<Drawable> drawableRef = drawableMap.get(urlString);
   
if (drawableRef != null) {
       
Drawable drawable = drawableRef.get();
       
if (drawable != null)
           
return drawable;
       
// Reference has expired so remove the key from drawableMap
        drawableMap
.remove(urlString);
   
}

   
if (Constants.LOGGING) Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
   
try {
       
InputStream is = fetch(urlString);
       
Drawable drawable = Drawable.createFromStream(is, "src");
        drawableRef
= new SoftReference<Drawable>(drawable);
        drawableMap
.put(urlString, drawableRef);
       
if (Constants.LOGGING) Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
               
+ drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
               
+ drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
       
return drawableRef.get();
   
} catch (MalformedURLException e) {
       
if (Constants.LOGGING) Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
       
return null;
   
} catch (IOException e) {
       
if (Constants.LOGGING) Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
       
return null;
   
}
}

public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
   
SoftReference<Drawable> drawableRef = drawableMap.get(urlString);
   
if (drawableRef != null) {
       
Drawable drawable = drawableRef.get();
       
if (drawable != null) {
            imageView
.setImageDrawable(drawableRef.get());
           
return;
       
}
       
// Reference has expired so remove the key from drawableMap
        drawableMap
.remove(urlString);
   
}

   
final Handler handler = new Handler() {
       
@Override
       
public void handleMessage(Message message) {
            imageView
.setImageDrawable((Drawable) message.obj);
       
}
   
};

   
Thread thread = new Thread() {
       
@Override
       
public void run() {
           
//TODO : set imageView to a "pending" image
           
Drawable drawable = fetchDrawable(urlString);
           
Message message = handler.obtainMessage(1, drawable);
            handler
.sendMessage(message);
       
}
   
};
    thread
.start();
}

<template> <list-view id="listview" style="flex: 1;background: #fff;" :refresher-enabled="false" :refresher-default-style="refresherStyle" :refresher-triggered="refresherTriggered" @scrolltolower="onScrollTolower" @scroll="scrollEvent" @touchstart="outsideTouchEvent" > <!-- 朋友圈背景图 --> <list-item class="top"> <image class="bg-image" src="/static/1.png" mode="aspectFill"></image> <view class="user-info uni-row" ref="user-info"> <text class="username">微信朋友圈</text> <image class="user-avatar" src="https://img0.baidu.com/it/u=1415523915,841919565&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=a9e21a0e5650a672fa2cbd3b133ed7e0" mode="widthFix"></image> </view> </list-item> <!-- 视频朋友圈内容 --> <list-item class="item uni-row"> <image class="avatar" src="/static/1.png" mode="scaleToFill"></image> <view class="content uni-row uni-column"> <text class="name">UNIAPP X</text> <text class="text">{{ text }}</text> <!-- 图片 / 视频 --> <view class="video"> <video :controls="false" class="video" poster="/static/1.png" src="https://qiniu-web-assets.dcloud.net.cn/video/sample/2minute-demo.mp4"></video> </view> <!-- 时间/点赞/评论按钮 --> <comment-line></comment-line> <!-- 点赞/评论信息列表 --> <comment></comment> </view> </list-item> <!-- 图文朋友圈内容 --> <list-item class="item uni-row" v-for="index in item_count"> <image class="avatar" src="/static/1.png" mode="scaleToFill"></image> <view class="content uni-row uni-column"> <text class="name">UNIAPP X {{ index }}</text> <text class="text">{{ text }}</text> <!-- 图片 / 视频 --> <view class="images uni-row"> <uni-moments-image :imageList="imageList"></uni-moments-image> </view> <!-- 定位 --> <text class="address mt-5">上海市 &middot; 静安寺</text> <!-- 时间/点赞/评论按钮 --> <comment-line></comment-line> <!-- 点赞/评论信息列表 --> <comment></comment> </view> </list-item> </list-view> <!-- 自定义导航栏 --> <moments-header ref="header" :scrollTop="scrollTop"></moments-header> <!-- 点赞/评论 --> <comment-popup ref="commentPopup"></comment-popup> <!-- 评论输入框 --> <comment-input ref="commentInput"></comment-input> </template> <script> import MomentsHeader from './components/momentsHeader.uvue'; import CommentPopup from './components/comment-popup.uvue'; import CommentLine from './components/comment-line.uvue'; import CommentInput from './components/comment-input.uvue'; import Comment from './components/comment.uvue'; import uniMomentsImage from '../../components/uni-moments-image/uni-moments-image.uvue'; export default { components: { CommentLine, Comment, MomentsHeader, CommentInput, CommentPopup, uniMomentsImage }, data() { return { item_count: 20, listViewElement: null as UniListViewElement | null, refresherTriggered: false, refresherStyle: "none", text: `uni-app x,是下一代 uni-app,是一个跨平台应用开发引擎。uts是一门类ts的、跨平台的、新语言。uts在iOS端编译为swift、在Android端编译为kotlin、在Web端编译为js。`, imageList: [ "https://img1.baidu.com/it/u=1965663592,580944689&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=5c73d7f8aaa2a6f5114e659dd64e769d", "https://img0.baidu.com/it/u=3838093562,4126749835&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=a2cd3d4ac8c0f2024a348772577a9d0f", "https://img0.baidu.com/it/u=256816879,771155532&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=d5230b0cfdae75de3c452d973d302106", "https://img1.baidu.com/it/u=1965663592,580944689&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=5c73d7f8aaa2a6f5114e659dd64e769d", "https://img0.baidu.com/it/u=3838093562,4126749835&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=a2cd3d4ac8c0f2024a348772577a9d0f", "https://img0.baidu.com/it/u=256816879,771155532&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1713286800&t=d5230b0cfdae75de3c452d973d302106", ], scrollTop: 0, keyboardHeight: 200, // ios 默认键盘高度,这里占位 } }, onReady() { this.listViewElement = uni.getElementById<UniListViewElement>('listview'); // 监听点击评论事件 uni.$on('commentScrollEvent', (y: number) => this.listViewScrollByY(y - this.keyboardHeight + 40)); // 监听一次键盘高度回调 uni.$once('keyboardHeightEvent', (keyboardHeight: number) => { this.keyboardHeight = keyboardHeight; }); }, methods: { // 目标元素以外的touch事件 outsideTouchEvent() { // https://doc.dcloud.net.cn/uni-app-x/component/#methods (this.$refs['commentPopup'] as ComponentPublicInstance).$callMethod('hideCommentPopup'); // 隐藏点赞/评论 (this.$refs['commentInput'] as ComponentPublicInstance).$callMethod('hideKeyboard'); // 隐藏评论框与键盘 }, onScrollTolower(_ : ScrollToLowerEvent) { setTimeout(() => { this.item_count += 20 }, 300) }, scrollEvent(e : UniScrollEvent) { this.scrollTop = e.detail.scrollTop; }, listViewScrollByY(y : number) { this.listViewElement?.scrollBy(0, y) } } } </script> <style lang="scss" scoped> @import './index.scss'; </style> 这是nvue 好像 帮我改为vue2 写法
07-18
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值