android– 在ListView中延迟加载图像

android– 在ListView中延迟加载图像,第1张

概述我使用ListView来显示与这些图像相关的一些图像和标题.我从互联网上获取图像.有没有办法延迟加载图像,以便文本显示时,UI不会被锁定,图像会在下载时显示?图像总数不固定.解决方法:这是我创建的用于保存我的应用当前正在显示的图像的内容.请注意,这里使用的“Log”对象是围绕Android

我使用ListVIEw来显示与这些图像相关的一些图像和标题.我从互联网上获取图像.有没有办法延迟加载图像,以便文本显示时,UI不会被锁定,图像会在下载时显示?

图像总数不固定.

解决方法:

这是我创建的用于保存我的应用当前正在显示的图像的内容.请注意,这里使用的“Log”对象是围绕Android内部最终Log类的自定义包装器.

package com.wilson.androID.library;/* licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements.  See the NOTICE filedistributed with this work for additional informationregarding copyright ownership.  The ASF licenses this fileto you under the Apache license, Version 2.0 (the"license"); you may not use this file except in compliancewith the license.  You may obtain a copy of the license athttp://www.apache.org/licenses/liCENSE-2.0Unless required by applicable law or agreed to in writing,software distributed under the license is distributed on an"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implIEd.  See the license for thespecific language governing permissions and limitationsunder the license.*/import java.io.IOException;public class DrawableManager {    private final Map<String, Drawable> drawableMap;    public DrawableManager() {        drawableMap = new HashMap<String, Drawable>();    }    public Drawable fetchDrawable(String urlString) {        if (drawableMap.containsKey(urlString)) {            return drawableMap.get(urlString);        }        Log.d(this.getClass().getSimplename(), "image url:" + urlString);        try {            inputStream is = fetch(urlString);            Drawable drawable = Drawable.createFromStream(is, "src");            if (drawable != null) {                drawableMap.put(urlString, drawable);                Log.d(this.getClass().getSimplename(), "got a thumbnail drawable: " + drawable.getBounds() + ", "                        + drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWIDth() + ", "                        + drawable.getMinimumHeight() + "," + drawable.getMinimumWIDth());            } else {              Log.w(this.getClass().getSimplename(), "Could not get thumbnail");            }            return drawable;        } catch (MalformedURLException e) {            Log.e(this.getClass().getSimplename(), "fetchDrawable Failed", e);            return null;        } catch (IOException e) {            Log.e(this.getClass().getSimplename(), "fetchDrawable Failed", e);            return null;        }    }    public voID fetchDrawableOnThread(final String urlString, final ImageVIEw imageVIEw) {        if (drawableMap.containsKey(urlString)) {            imageVIEw.setimageDrawable(drawableMap.get(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();    }    private inputStream fetch(String urlString) throws MalformedURLException, IOException {        DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();        httpGet request = new httpGet(urlString);        httpResponse response = httpClIEnt.execute(request);        return response.getEntity().getContent();    }}
总结

以上是内存溢出为你收集整理的android – 在ListView中延迟加载图像全部内容,希望文章能够帮你解决android – 在ListView中延迟加载图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104572.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存