在列表视图上滚动速度慢,下载了图像缩略图

时间:2017-07-17 03:25:31

标签: android listview bitmap

我的listview上滚动速度很慢,有一些文字和图片缩略图,信息是由json webservice获得的。 这是我创建listview的类:

public class NoticiasFragment extends Fragment{
    private static final String TAG = "NoticiasFragment";
    private String resultado;
    private List<HashMap<String, String>> mAndroidMapList = new ArrayList<>();
    private ListView mListView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.noticias_tab, container, false);

        /*ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
        actionBar.setTitle("Noticias");
        actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));*/

        Noticia noticia = new Noticia();
        try {
            resultado = noticia.execute("some-url").get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        armarListaNoticias(resultado, inflater, container, view);
        return view;
    }

    protected void armarListaNoticias(String resultado, LayoutInflater inflater, ViewGroup container, View view){
        mListView = (ListView) view.findViewById(R.id.listaNoticias);

        try {
            JSONArray jArray = new JSONArray(resultado);
            String titulo = null;
            for(int i=0; i < jArray.length(); i++) {
                JSONObject jObject = jArray.getJSONObject(i);
                HashMap<String, String> map = new HashMap<>();
                map.put("Thumbnail", jObject.getString("thumbnail_path"));
                titulo = jObject.getString("preview_title");
                if(titulo.length() > 58){
                    titulo = titulo.substring(0, 58) + "...";
                }
                map.put("Titulo", titulo);
                map.put("Titulo_completo", jObject.getString("full_title"));
                map.put("Thumbnail_path", jObject.getString("thumbnail_path"));
                map.put("Descripcion", jObject.getString("full_text"));
                map.put("Resumen", "Resumen");

                mAndroidMapList.add(map);
            }
        } catch (JSONException e) {

        }

        ListAdapter adapter = new SimpleAdapter(getActivity(), mAndroidMapList, R.layout.noticia_row,
                new String[] { "Titulo", "Resumen" },
                new int[] { R.id.titulo, R.id.resumen }){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                // Get the current item from ListView

                View view = super.getView(position,convertView,parent);
                Bitmap bmp = null;

                ImageView imageView = (ImageView)view.findViewById(R.id.thumbnail);
                DescargaImagenes descargaImagenes = new DescargaImagenes();
                try {
                    bmp = descargaImagenes.execute("image-url" + mAndroidMapList.get(position).get("Thumbnail")).get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                imageView.setImageBitmap(bmp);


                //CAMBIAR FUENTE

                TextView myTextView = (TextView)view.findViewById(R.id.titulo);
                Typeface typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
                myTextView.setTypeface(typeFace);

                myTextView = (TextView)view.findViewById(R.id.resumen);
                typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
                myTextView.setTypeface(typeFace);


                if(position % 2 == 1)
                {
                    // Set a background color for ListView regular row/item
                    view.setBackgroundColor(Color.parseColor("#7F1010"));
                }
                else
                {
                    // Set the background color for alternate row/item
                    view.setBackgroundColor(Color.parseColor("#9C1F1F"));
                }
                return view;
            }
        };

        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                HashMap registro = mAndroidMapList.get(position);

                Intent i = new Intent(getActivity(), DetalleNoticia.class);

                i.putExtra("datos", registro);

                startActivity(i);
            }
        });
    }
}

我不知道如何设置图像,所以我的想法是下载并使用我的列表设置图像,该列表已经有getView()方法下载的图像路径。我还是Android的新手,所以这是我能想到的第一件事。

如何提高效果?

4 个答案:

答案 0 :(得分:3)

您可以使用Picasso图片下载库。它将以异步方式下载图像。所以它对应用程序性能会有好处。

示例:

Picasso.with(mContext).load("https://s3om/foodev/hotel/" + imageLocation).placeholder(R.drawable.ic_default_hotel).error(R.drawable.ic_default_hotel).into(holder.imageviewHotel);

了解更多information

答案 1 :(得分:1)

首先你应该检查是否异步:

  DescargaImagenes descargaImagenes = new DescargaImagenes();
            try {
                bmp = descargaImagenes.execute("https://aguilaammo.com.mx/public/" + mAndroidMapList.get(position).get("Thumbnail")).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

第二个在这里,每次从适配器中的资产创建字体时,不建议这样做。:

            TextView myTextView = (TextView)view.findViewById(R.id.titulo);
            Typeface typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
            myTextView.setTypeface(typeFace);

            myTextView = (TextView)view.findViewById(R.id.resumen);
            typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
            myTextView.setTypeface(typeFace)

相反,你应该像这样替换字体的相对位置:

 private View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        v = inflater.inflate(resource, parent, false);
          TextView myTextView = (TextView)view.findViewById(R.id.titulo);
            Typeface typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
            myTextView.setTypeface(typeFace);

            myTextView = (TextView)view.findViewById(R.id.resumen);
            typeFace=Typeface.createFromAsset(getActivity().getAssets(),"fonts/TitilliumWeb-Regular.ttf");
            myTextView.setTypeface(typeFace);
    } else {
        v = convertView;         
    }

   // bindView(position, v);
   // you type face bind create in here.

    return v;
}

最后。我建议你使用RecyclerView。这非常有用。

答案 2 :(得分:0)

现在我们通常使用OkHttp + Retrofit从net获取数据。 Retrofit会将JSON String转换为JavaBean,因此您不需要操作JSONObject ...

然后,如果您想将网络图像加载到ImageView,您可以使用Glide或Picasso或Fresco等。

当您将图像加载到ListView或GridView中的ImageView时,您应该为它们设置ScrollListener,就像列表或网格滚动时一样,您可以暂停网络请求,然后在listview停止滚动后恢复网络请求。

Glide + OkHttp + Retrofit都可以从Github下载或导入你的模型。

答案 3 :(得分:0)

使用PicassoGlide。 无需担心这些库的图像。