简体   繁体   English

使用通用图像加载器

[英]Using Universal Image Loader

I am trying to optimize photo gallery in my app and I came across Universal Image Loader. 我试图在我的应用程序中优化照片库,但遇到了Universal Image Loader。 However, all attempts to implement it into my application have failed. 但是,所有将其实现到我的应用程序中的尝试都失败了。 Here's 2 classes from my current application: the activity class: 这是我当前应用程序中的2个类:活动类:

public class Galerija extends Activity {

ArrayList<RSSItem> lista = new ArrayList<RSSItem>();
ArrayList<String> lst_slika = new ArrayList<String>();
RSSItem tempItem = new RSSItem();
ImageAdapter adapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_galerija);

    try 
    {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader myReader = sp.getXMLReader();

        URL url = new URL("http://erdut.gausstesting.info/generateXML/galerija.php");

        XMLHandler myXMLHandler = new XMLHandler();
        myReader.setContentHandler(myXMLHandler);
        myReader.parse(new InputSource(url.openStream()));
        lista = myXMLHandler.getRss_lista();
        lst_slika = lista.get(0).getImages();

    } catch (Exception e) {
        System.out.println(e);
    }

    adapter = new ImageAdapter(this, lst_slika);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(adapter);

    gridview.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }
    });

} }

And here is the adapter class: 这是适配器类:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> lista;


public ImageAdapter(Context c, ArrayList<String> lista) {
    mContext = c;
    this.lista = lista;

}

public int getCount() {
    return lista.size();
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);

        imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convertView;
    }

    new ImageDownloadTask(imageView).execute(lista.get(position));

    return imageView;
}}

I want to modify these class by using Universal Image Loader (or any other method to cache my images), because currently my photo gallery activity crashes due to Out of Memory error. 我想通过使用Universal Image Loader(或其他任何缓存我的图像的方法)来修改这些类,因为当前我的图库活动由于内存不足错误而崩溃。

Write below code line into your adapter's getView() method, here imageUrls[position] is array of Image Urls and holder.image is imageview. 将下面的代码行写入适配器的getView()方法,此处imageUrls [position]是Image Urls的数组,holder.image是imageview。

imageLoader.displayImage(imageUrls[position], holder.image, null);

And write below code line into your adapter constructor. 并将下面的代码行写入您的适配器构造函数。

ImageLoader imageLoader=new  ImageLoader(activity.getApplicationContext());

And see below link for complete source code of Universal Image Loader Example. 并请参见下面的链接,以获取Universal Image Loader示例的完整源代码。

Android - Universal Image Loader Android-通用图片加载器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM