Android 如何在列表中高效地显示图像?

Android 如何在列表中高效地显示图像?,android,listview,android-studio,bitmapsource,Android,Listview,Android Studio,Bitmapsource,所以现在我有一个列表,在旁边显示图像和文本,但是在显示图片时遇到了问题。我认为最好的方法是使用JSOUP解析图像,然后下载图像并使用bitmapsFactory对其进行解码,但失败了,因为它需要太多的内存来显示图片。有没有办法在线显示图像,这样就不会占用空间?我曾想过使用Webview,然后插入链接并显示图片,但这样行吗?我在列表中包括了我的MainActivity、ListClass、ImageHandler和XML 主要活动 import android.os.Bundle; import

所以现在我有一个列表,在旁边显示图像和文本,但是在显示图片时遇到了问题。我认为最好的方法是使用JSOUP解析图像,然后下载图像并使用bitmapsFactory对其进行解码,但失败了,因为它需要太多的内存来显示图片。有没有办法在线显示图像,这样就不会占用空间?我曾想过使用Webview,然后插入链接并显示图片,但这样行吗?我在列表中包括了我的MainActivity、ListClass、ImageHandler和XML

主要活动

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.widget.FrameLayout;
import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;




public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener   {

    ListView list;
    imageHandler imagess = new imageHandler();

    String[] web = {"Egypt "};
    Drawable[] imageId = { imagess.LoadImageFromWebOperations("http://egyptianstreets.com/wp-content/uploads/2016/08/satelliteegypt-400x240.jpg")};

    Handler handler;
    Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        listClass adapter = new
                listClass(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
            }
        });

    }
列表的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="150dp"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/txt"
        android:paddingRight="16dp"
        android:textSize="15dp"
        android:layout_alignLeft="@+id/img"
        android:layout_marginLeft="150dp"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:ellipsize="none"
        android:maxLines="100"
        android:scrollHorizontally="false"/>

</RelativeLayout>

如果您使用的是图像、文本组合,我建议您查看回收器视图。它们处理图像的效率更高。不过,使用列表视图并没有什么害处

要加载图像,请使用毕加索库


只需一行代码即可将图像加载到图像视图中。希望这有帮助。

如果您使用的是图像、文本组合,我建议您查看回收器视图。它们处理图像的效率更高。不过,使用列表视图并没有什么害处

要加载图像,请使用毕加索库


只需一行代码即可将图像加载到图像视图中。希望这有帮助。

使用截击库加载图像 并使用该类加载图像

public class LruBitmapCache extends LruCache<String, Bitmap> implements
    ImageCache {
public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public LruBitmapCache() {
    this(getDefaultLruCacheSize());
}

public LruBitmapCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

有关更多信息,您可以使用Volley library加载图像 并使用该类加载图像

public class LruBitmapCache extends LruCache<String, Bitmap> implements
    ImageCache {
public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public LruBitmapCache() {
    this(getDefaultLruCacheSize());
}

public LruBitmapCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

有关详细信息,请按Nikhil建议的那样转到

中,尝试使用毕加索库。它易于实现,而且效率也很高

您只需输入一行即可加载图像。您可以在适配器的getView方法中执行此操作

Picasso.with(context).load(imageURI/path/file).fit().error(R.id.error_img) //an image that gets displayed in case of error
.into(R.id.img_container, new Callback() {
   void onSuccess() {}
   void onError() {}
})

希望这有帮助

正如尼希尔所建议的,尝试使用毕加索图书馆。它易于实现,而且效率也很高

您只需输入一行即可加载图像。您可以在适配器的getView方法中执行此操作

Picasso.with(context).load(imageURI/path/file).fit().error(R.id.error_img) //an image that gets displayed in case of error
.into(R.id.img_container, new Callback() {
   void onSuccess() {}
   void onError() {}
})

希望这有帮助

试试android毕加索库。@Razgriz我考虑过这一点,或者使用Glide库,但有人告诉我,这些库将图像下载到手机上,这将是低效的。Glide或毕加索不下载图像,而是缓存它们。这是一种非常有效的方法。你也可以关闭缓存。试试android毕加索库。@Razgriz我考虑过这一点,或者使用Glide库,但有人告诉我,这些库将图像下载到手机上,这将是低效的。Glide或毕加索不下载图像,而是缓存它们。这是一种非常有效的方法。你也可以关闭缓存。
if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
Picasso.with(context).load(imageURI/path/file).fit().error(R.id.error_img) //an image that gets displayed in case of error
.into(R.id.img_container, new Callback() {
   void onSuccess() {}
   void onError() {}
})