滞后GridView

时间:2018-02-22 18:12:11

标签: android android-gridview

我正在显示两列的 GridView ,其中包含显示信息的适配器。

如果imatge相同,它滚动速度非常快,但是当显示相同数量的图像但是它们不同时,滚动量会滞后很多。

我尝试加载picaso和其他库但我无法解决我的问题。

(抱歉我的英语不好)

我的适配器

package com.example.anonymous.mh4;


import android.content.Context;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



import com.squareup.picasso.Picasso;

import java.io.File;
import java.lang.reflect.Field;

public class MonsterAdapter extends BaseAdapter {

    private final Context mContext;
    private final Monster[] monsters;

    public MonsterAdapter(Context context, Monster[] monsters) {
        this.mContext = context;
        this.monsters = monsters;
    }

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


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


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


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


        final Monster m = monsters[position];

        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.listviewitemmonster, null);
        }

        final ImageView imageView = (ImageView)convertView.findViewById(R.id.imglayout);
        final TextView nameTextView = (TextView)convertView.findViewById(R.id.namelayout);


        //getDrawableId(m.getIcon());



        //imageView.setImageResource(R.drawable.a1);


        // Picasso.with(mContext).load(R.drawable.a1).into(imageView);



        nameTextView.setText(m.getName());
        int id = mContext.getResources().getIdentifier(m.getIcon(), "drawable", mContext.getPackageName());
        imageView.setImageResource(id);

        //Glide.with(mContext).load("R.drawable."+m.getIcon()).into(imageView);

        /**Picasso.with(mContext).load(id)
                .error(R.drawable.unknow)
                .into(imageView);
        **/
        return convertView;

    }
}

EDITED

package com.example.anonymous.mh4;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.lang.reflect.Field;

public class MonsterAdapter extends BaseAdapter {





    private final Context mContext;


    private final Monster[] monsters;

    public MonsterAdapter(Context context, Monster[] monsters) {
        this.mContext = context;
        this.monsters = monsters;
    }



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


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


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


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


        final Monster m = monsters[position];

        if (convertView == null) {
            final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            convertView = layoutInflater.inflate(R.layout.listviewitemmonster, null);
        }

        final ImageView imageView = (ImageView)convertView.findViewById(R.id.imglayout);
        final TextView nameTextView = (TextView)convertView.findViewById(R.id.namelayout);


        //getDrawableId(m.getIcon());

        int id = mContext.getResources().getIdentifier(m.getIcon(), "drawable", mContext.getPackageName());
        Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), id);

        //imageView.setImageResource(R.drawable.a1);


        // Picasso.with(mContext).load(R.drawable.a1).into(imageView);

        //Picasso.with(mContext).load("file:///android_asset/DvpvklR.png").into(imageView2);

        nameTextView.setText(m.getName());
        imageView.setImageBitmap(bm);

        //imageView.setImageResource(id);

        //Glide.with(mContext).load("R.drawable."+m.getIcon()).into(imageView);

        /**Picasso.with(mContext).load(id)
                .error(R.drawable.unknow)
                .into(imageView);
        **/
        return convertView;

    }
}

IT工作,但是太过分了

1 个答案:

答案 0 :(得分:1)

你的问题的原因是setImageResource()是一个昂贵的同步操作,涉及解码图像。

除了降低图像质量之外,你的另一个选择是在你开始列表之前以某种方式缓存图像(只有当你的列表不是很长并且最好是在后台线程中时才能这样做。)

简而言之,您将查看列表并构建一系列资源:

Bitmap bm = BitmapFactory.decodeResource(getResources(), getIcon());
saveToMap(R.drawable.image, bm);

当您需要展示图片时,您会执行以下操作:

iconView.setImageBitmap(getFromMap(getIcon()));

您可以使用HashMap保存/获取位图或使用其他数据结构(如果列表是常量,则使用数组)。您可能还必须处理位图/回收和内存管理,以使此解决方案正常运行。