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

时间:2016-08-06 03:29:08

标签: android listview android-studio bitmapsource

所以目前我有一个列表,旁边显示图像和文字,但显示图片有问题。我认为最好的方法是使用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();
            }
        });

    }

ListClass;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class listClass extends ArrayAdapter<String>{



    //Next three lines introduces Variables to use in the class
    private final Activity context;
    private final String[] web;
    private final Drawable[] imageId;

    //Constructor to get Web, Image and context to set adapter
    public listClass(Activity context,  String[] web, Drawable[] imageId) {

            super(context, R.layout.list, web);
            this.context = context;
            this.web = web;
            this.imageId = imageId;

    }

    //Public method to ...
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);
        imageView.setImageDrawable(imageId[position]);
        return rowView;
    }
}

ImageHandler使用InputStream但不起作用;

import android.media.Image;
import java.net.URL;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import android.graphics.drawable.Drawable;




public class imageHandler {

    Image picture;


    public static Drawable LoadImageFromWebOperations(String url) {
        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "earthdownload");
            return d;
        } catch (Exception e) {
            return null;
        }
    }
}

XML for list ;;

<?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>

3 个答案:

答案 0 :(得分:3)

如果您正在使用图片,文字组合我建议您查看回收者的观点。它们对图像效率更高。但是,使用列表视图没有坏处。

要加载图像,请使用Picasso库。 http://square.github.io/picasso/

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

答案 1 :(得分:1)

正如Nikhil建议的那样,尝试使用Picasso库。它易于实施和高效。

您只需输入一行即可加载图片。您可以在适配器的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() {}
})

希望这有帮助!

答案 2 :(得分:0)

使用Volley库加载图像 并使用此类加载图像。

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);
}

}

为ImageView添加此XML

 <!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/thumbnail"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp" />

在适配器中设置

if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);

有关详情,请访问here