[英]Image cannot be displayed in GridView?
I build a simple app with ImageView and GridView.我用 ImageView 和 GridView 构建了一个简单的应用程序。 I load images from an internet resource.我从互联网资源加载图像。 The app can display an image (I tested with ImageView
), but It cannot display all images in the list.该应用程序可以显示图像(我用ImageView
测试),但它不能显示列表中的所有图像。
I tested the following:我测试了以下内容:
DownloadImage
worked normally. Class DownloadImage
工作正常。ImageAdapter
is ok too. ImageAdapter
也可以。 However, images still cannot be loaded on GridView
.但是, GridView
上仍然无法加载图像。 No need external libraries (Picasso, Glide...) .无需外部库(毕加索、Glide...) 。
Here is activity_main.xml
这是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>
Here is DownloadImage.java
这是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);
}
}
Here is ImageAdapter.java
这是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;
}
}
Here is MainActivity.java
这是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) {
}
});
}
}
Here is the logcat:这是日志猫:
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.
Thanks in advance!提前致谢!
I do not recommend writing your own image downloader whereas there are some libraries like Glide and Picasso that are already available for this purpose.我不建议您编写自己的图像下载器,因为已经有一些库(如Glide和Picasso )可用于此目的。
In your case, the problem might be, the ImageView
s are taking some time to download the images first.在您的情况下,问题可能是ImageView
需要一些时间才能先下载图像。 Hence, I think you really should use Glide
or Picasso
to load your images into your ImageView
s inside the GridView
.因此,我认为您确实应该使用Glide
或Picasso
将图像加载到ImageView
内的GridView
中。
I use Glide in most of the cases and its pretty simple to use and integrate.我在大多数情况下都使用 Glide,它的使用和集成非常简单。 You do not have to write your DownloadImage
class on your own.您不必自己编写DownloadImage
class。 Just the following code snippet will do.只需以下代码片段即可。
Glide
.with(context)
.load(url)
.into(imagview);
To integrate Glide, just add the following dependency in your build.gradle
file, you are good to go!要集成 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'
}
I hope that helps!我希望这会有所帮助!
In case you are not comfortable with using external libraries, you need to implement the behavior that Glide implemented by yourself!如果你不习惯使用外部库,你需要实现 Glide 自己实现的行为!
You need to lazy load the images when necessary.您需要在必要时延迟加载图像。 In a GridView
the images are loaded on demand when visible.在GridView
中,图像在可见时按需加载。 I recommend looking into the scroll options so that you can differentiate when there is a fling or a slight scroll.我建议查看滚动选项,以便您可以区分何时有弹跳或轻微滚动。 It's a bit tricky.这有点棘手。 I recommend looking into this answer .我建议调查这个答案。
The error is caused by putting a GridView
inside a ScrollView
.该错误是由于将GridView
放入ScrollView
引起的。 Basically, GridView
contains a scrollable widget inside it.基本上, GridView
内部包含一个可滚动的小部件。 Just remove the ScrollView
and it works normally.只需删除ScrollView
正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.