gpt4 book ai didi

java - GridView中图片无法显示?

转载 作者:行者123 更新时间:2023-12-01 23:19:35 25 4
gpt4 key购买 nike

我使用 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.

提前致谢!

最佳答案

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

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

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

 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 .

关于java - GridView中图片无法显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58332594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com