0

我的画廊有问题,速度很慢而且滞后。我正在使用图像适配器从服务器加载图像:

package com.example.ocenitaksi;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.http.client.utils.URLEncodedUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] mobileValues;

    public ImageAdapter(Context context, String[] mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);


            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);



            // ubacivanje slika za string


                String mobile = mobileValues[position];
                if (mobile.equals(MainActivity.imena[position])) {
                //imageView.setImageURI("http://24.media.tumblr.com/avatar_a400b4dbb80e_64.png");
                Bitmap bitmap=null;;

                mobile = mobile.replace(" ", "_");

                        bitmap = DownloadImage("http://android.yunews.net/images/"+mobile+".png");


                    imageView.setImageBitmap(bitmap);

                }


        } 
        else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return mobileValues.length;
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }
    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }
}

这是我主要活动的一部分。

private class LongOperation extends AsyncTask<String, Void, String> 
    {
        protected void onPreExecute() 
        { 
            progressDialog = new ProgressDialog(MainActivity.this); 
            progressDialog.setTitle("Molimo sacekajte...");
            progressDialog.setMessage("Ucitavam taksi sluzbe...");
            progressDialog.setCancelable(true);
            progressDialog.show();
        }

        protected String doInBackground(String... params) 
        {

                niz = new JSONFunkcije().ListaSluzbi();

                imena= new String[niz.size()];
                Iterator<String> it= niz.iterator();
                for (int i=0;i<niz.size();i++)
                    imena[i]=it.next();
                slicice = new ImageAdapter(MainActivity.this, imena);


            return null;
        }
        protected void onPostExecute(String result) 
        {

            progressDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    //Button dugme1 = (Button) findViewById(R.id.dugme1);

                    ///params.height = "50dpi";
                    //dugme1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));

                    Gallery gridView = (Gallery) findViewById(R.id.gallery1);

                    gridView.setAdapter(slicice);
                    //kraj ucitvanja

                    //gridView.setAdapter(adapter);
                    gridView.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

                            // Sending image id to FullScreenActivity
                            Intent i = new Intent(getApplicationContext(), TaksiDetalji.class);
                            // passing array index
                            i.putExtra("id", imena[position]);
                            startActivity(i);
                        }
                    });

                }
            });
        System.gc();
        }
      }

我正在从 OnCrate 调用 LongOperation:

mytask = new LongOperation();
        mytask.execute(); 

ImageAdapter 有问题吗?太多的调用,下载?我试图缓存图像,但失败了。

4

2 回答 2

0

这是在 gridview、listview 和 pager 中显示图像的绝佳示例。看一下这个

https://github.com/nostra13/Android-Universal-Image-Loader

于 2013-10-01T13:27:15.633 回答
0

从您的代码中,我想说有很多原因,因为以下所有这些原因导致它运行缓慢:

  1. 您没有遵循ViewHolder 模式。另请阅读文档以使 ListView 滚动平滑
  2. 您没有遵循加载图像的标准方式(即本地缓存图像,如果已缓存则从本地加载)

最好尝试网络上可用的任何库(通用图像加载器或任何库)在 ListView 中加载图像。

于 2013-10-01T13:45:46.717 回答