我正在使用ListView来显示一些与这些图像相关的图像和标题.我正在从互联网上获取图片.有没有一种方法可以延迟加载图像,以便在文本显示时,UI不会被阻止,并且图像在下载时会显示出来?

图像的总数不是固定的.

推荐答案

下面是我创建的用来保存我的应用程序当前显示的图像的内容.请注意,这里使用的"Log"对象是我在Android内部的最后一个Log类的定制包装器.

package com.wilson.android.library;

/*
 Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless 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 ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under 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相关问答推荐

道查询注释部分房间表名称

如何让用户与我的应用生成的多个音频文件交互

如何在Jetpack composeH中创建具有弯曲末端的六边形形状

如何在Android Studio的LinearLayout中禁用阴影

如果我的圆形图像的顶部居中于卡片内部,我如何在其下方画一条弧线?

(已解决)从最近的应用程序打开应用程序时出错

在 Bash 脚本中使用 XMLLINT 解析 XML 单元测试文件,并将其放入数组中以表示成功和失败

是否可以在 compose 中使用三次贝塞尔曲线进行动画?

相机2问题:设置AE区域、AF区域和AWB区域.

需要在按钮 onclick 上从 string.xml 获取值. @Composable 调用只能在@Composable 函数的上下文中发生

我的自定义小吃店不适合我的全宽屏幕尺寸

发布 MAUI 应用程序时出现遇到重复程序集错误

Jetpack compose (Glance) 小部件在加载位图图像后不会重新组合

Kotlin Multiplatform Mobile targetSdk 已弃用

Android CompanionDeviceManager 永远找不到任何附近的蓝牙设备

Visual Studio 无法在 Android 上编译 .NET MAUI 项目

为卡片的上半部分添加一个边框,用圆角半径组合

如何在 Android 应用程序未激活/未聚焦时显示视图?

try 使用 ViewPager2 实现滑动视图时出现类型不匹配错误

IconButton 中可绘制的图标是黑色的,尽管它是白色的