从URL下载图像并将其显示在GridView中

DEV*_*RMA 2 android gridview

我有一个URL存在一些图像.我想从该URL检索所有图像并在中显示这些图像GridView.现在当我点击网格视图中的任何拇指预览时,它应该放大或加载到全屏.

拍摄快照以便更好地理解在此输入图像描述

Bas*_*sil 6

您可以尝试以下代码.延迟加载图像是加载图像的一个很好的解决方案,您可以尝试从以下链接延迟加载:在ListView中延迟加载图像.在这里他们在布局中使用了ListView,因此图像和相应的文本显示为列表项,您可以将ListView更改为GridView,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView
        android:id="@+id/gridv"
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:columnWidth="90dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="Clear Cache"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我认为这将解决问题.


And*_*lva 5

首先找出一种如何从单个链接下载所有图像的方法,我认为这有点困难.

然后将所有链接位置放入字符串数组中.现在使用以下代码下载图像.

    public Drawable LoadImage(String url) {

    Drawable d;
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (NullPointerException e) {
        d = getResources().getDrawable(R.drawable.icon);
        return d;
    } catch (Exception e) {
        d = getResources().getDrawable(R.drawable.icon);
        return d;
    }
}
Run Code Online (Sandbox Code Playgroud)

获取存储链接位置的字符串数组的长度.在for循环内部尝试执行上面的代码.这将返回一个可绘制对象,您可以将其转换为资源或位图,并将其添加到GridView.