【问题标题】:Image cannot be displayed in GridView?图片不能在GridView中显示?
【发布时间】:2019-10-11 00:09:17
【问题描述】:

我用 ImageView 和 GridView 构建了一个简单的应用程序。我从互联网资源加载图像。该应用可以显示图像(我使用ImageView 测试过),但它无法显示列表中的所有图像。

我测试了以下内容:

  1. 班级DownloadImage工作正常。
  2. ImageAdapter 也可以。

但是,图像仍然无法在GridView 上加载。 无需外部库(Picasso、Glide...)

这里是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:id="@+id/gridview"/>

</ScrollView>

这里是DownloadImage.java

package com.example.imageviewer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.URL;

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    ImageView imageview;

    public DownloadImage(ImageView imageview){
        this.imageview = imageview;
    }

    @Override
    protected Bitmap doInBackground(String... strings) {
        String url = strings[0];
        Bitmap result = null;
        try{
            InputStream in = new URL(url).openConnection().getInputStream();
            result = BitmapFactory.decodeStream(in);
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        imageview.setImageBitmap(bitmap);
    }
}

这里是ImageAdapter.java

package com.example.imageviewer;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private String[] imagelist;

    public ImageAdapter(Context context, String[] imagelist) {
        this.context = context;
        this.imagelist = imagelist;
    }

    @Override
    public int getCount() {
        return imagelist.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imagview;
        if(convertView==null)
            imagview = new ImageView(context);
        else
            imagview = (ImageView)convertView;
        new DownloadImage(imagview).execute(imagelist[position]);
        return imagview;
    }
}

这里是MainActivity.java

package com.example.imageviewer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {

    GridView gridview;
    String[] urls = {
            "https://sarahraven.images.blucommerce.com/sarahraven/product/261037_2.jpg",
            "https://ngb.org/wp-content/uploads/2018/09/longfield.gardens.Sept_.jpg"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridview = findViewById(R.id.gridview);
        ImageAdapter imageadapter = new ImageAdapter(getApplicationContext(), urls);
        gridview.setAdapter(imageadapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }
}

这里是日志:

10-11 08:41:27.040 10544-10544/? I/art: Not late-enabling -Xcheck:jni (already on)
10-11 08:41:27.458 10544-10544/com.example.imageviewer W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-11 08:41:27.646 10544-10544/com.example.imageviewer I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
10-11 08:41:27.669 10544-10544/com.example.imageviewer I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
10-11 08:41:27.767 10544-10557/com.example.imageviewer I/art: Background sticky concurrent mark sweep GC freed 2403(236KB) AllocSpace objects, 0(0B) LOS objects, 22% free, 874KB/1135KB, paused 2.275ms total 159.898ms
10-11 08:41:28.360 10544-10563/com.example.imageviewer D/OpenGLRenderer: Render dirty regions requested: true
10-11 08:41:28.419 10544-10544/com.example.imageviewer D/Atlas: Validating map...
10-11 08:41:28.764 10544-10563/com.example.imageviewer I/OpenGLRenderer: Initialized EGL, version 1.4
10-11 08:41:28.833 10544-10563/com.example.imageviewer D/EGL_emulation: eglCreateContext: 0x7f8131b44740: maj 3 min 0 rcv 3
10-11 08:41:28.865 10544-10563/com.example.imageviewer D/EGL_emulation: eglMakeCurrent: 0x7f8131b44740: ver 3 0 (tinfo 0x7f8131b11260)
10-11 08:41:28.880 10544-10563/com.example.imageviewer E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
10-11 08:41:28.880 10544-10563/com.example.imageviewer E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
10-11 08:41:28.884 10544-10563/com.example.imageviewer D/OpenGLRenderer: Enabling debug mode 0
10-11 08:41:28.985 10544-10563/com.example.imageviewer D/EGL_emulation: eglMakeCurrent: 0x7f8131b44740: ver 3 0 (tinfo 0x7f8131b11260)
10-11 08:41:29.019 10544-10544/com.example.imageviewer I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.

提前致谢!

【问题讨论】:

  • 你在 logcat 中看到什么了吗?
  • 是的,您可能不会在GridView 中一次看到所有图像,因为这需要先下载,这需要一些时间。
  • 没什么可看的。等待几分钟,但没有!没有错误!
  • 尝试为你的 imageView 的宽度和高度设置一个硬编码值,看看是否是因为 GridView 预先计算了填充期间的大小(并且 imageView 尚未加载,因此为 0dp)

标签: java android imageview


【解决方案1】:

我不建议您编写自己的图像下载器,因为已经有一些库(如 GlidePicasso)可用于此目的。

在您的情况下,问题可能是,ImageViews 需要一些时间才能先下载图像。因此,我认为您确实应该使用GlidePicasso 将图像加载到GridView 内的ImageViews 中。

我在大多数情况下都使用 Glide,它的使用和集成非常简单。您不必自己编写DownloadImage 课程。只需以下代码 sn-p 即可。

 Glide
    .with(context)
    .load(url)
    .into(imagview);

要集成 Glide,只需在您的 build.gradle 文件中添加以下依赖项,您就可以开始了!

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.10.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
}

希望对您有所帮助!

如果你不习惯使用外部库,你需要实现 Glide 自己实现的行为!

您需要在必要时延迟加载图像。在GridView 中,图像在可见时按需加载。我建议查看滚动选项,以便您可以区分何时有弹跳或轻微滚动。这有点棘手。我建议查看this answer

【讨论】:

  • 谢谢推荐!但我不想使用外部库。
【解决方案2】:

该错误是由于将GridView 放入ScrollView 中引起的。基本上,GridView 在其中包含一个可滚动的小部件。去掉ScrollView就可以正常使用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    相关资源
    最近更新 更多