【问题标题】:ViewPager and ListView CacheViewPager 和 ListView 缓存
【发布时间】:2018-05-20 13:39:04
【问题描述】:

我有一个带有 4 个 ListViews 的 ViewPager。列表视图都下载唯一的图像,我想缓存它们,但出现错误,应用程序运行速度很慢。有没有办法将所有图像缓存在 1 LruCache 或类似的东西中? 到目前为止,这是我的代码 第一个列表适配器

public class TwitchAdapter extends ArrayAdapter<Twitch>{
private Context context;
private int resourceId;
private ArrayList<Twitch> twitchList;
LruCache<Integer,Bitmap> logoCache;
LruCache<Integer,Bitmap> iconCache;
public TwitchAdapter(Context c, int resId, ArrayList<Twitch> objects){
    super(c, resId, objects);
    this.context = c;
    this.resourceId = resId;
    this.twitchList = objects;
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    iconCache = new LruCache<Integer, Bitmap>( maxMemory / 16);
    logoCache = new LruCache<Integer, Bitmap>( maxMemory / 64);
}
public View getView(final int position, View convertView, final ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(resourceId, parent, false);
    final Twitch twitch = twitchList.get(position);

    TextView viewrs = (TextView) view.findViewById(R.id.viewers);
    TextView language = (TextView) view.findViewById(R.id.language);
    TextView status = (TextView) view.findViewById(R.id.streamer_status);
    TextView name = (TextView) view.findViewById(R.id.streamer_name);
    ImageView profile = (ImageView) view.findViewById(R.id.logo);
    CircleImageView imageView = (CircleImageView) view.findViewById(R.id.twitch_icon);
    int id = twitch.getId();
    status.setText(twitch.getStatus());
    language.setText(twitch.getLanguage());
    name.setText(twitch.getName());
    viewrs.setText(twitch.getViewrs()+"");
    Bitmap bitmapIcon = iconCache.get(id+1);
    if (bitmapIcon != null){


        imageView.setImageBitmap(bitmapIcon);
    }else {
        LoadImageTask(twitch,imageView);
    }
    Bitmap bitmapLogo = logoCache.get(twitch.getId());
    if (bitmapLogo != null){
        profile.setImageBitmap(bitmapLogo);
    }else {
        LoadIconTast(twitch,profile);
    }

    return view;
}



public void LoadImageTask(final Twitch twitch, final ImageView iconView){

        ImageRequest request  =new ImageRequest(twitch.getIconUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
           iconView.setImageBitmap(response);
            iconCache.put(twitch.getId()+1,response);
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);

}
private void LoadIconTast(final Twitch twitch, final ImageView profileView) {
    ImageRequest request  =new ImageRequest(twitch.getLogoUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            profileView.setImageBitmap(response);
            iconCache.put(twitch.getId(),response);
            notifyDataSetChanged();
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);
}

}

还有一个完全一样的 这是错误

12-06 19:15:24.392 31970-2219/app.mma.introsliderproject E/AndroidRuntime: FATAL EXCEPTION: Thread-100374
                                                                       Process: app.mma.introsliderproject, PID: 31970
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 17 byte allocation with 27048 free bytes and 26KB until OOM
                                                                           at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:323)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:541)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:564)
                                                                           at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:404)
                                                                           at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:157)
                                                                           at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)

【问题讨论】:

    标签: android listview caching android-viewpager android-lru-cache


    【解决方案1】:

    您可以使用 Glide 或 Picasso 等图像缓存库。 他们可以自动调整您的图像大小以适应视图大小并减少内存消耗。

    【讨论】:

    • 我知道,但无论如何我需要将所有列表图像保存在一个缓存中我想知道如何做到这一点
    • “一个缓存”是什么意思?我不明白为什么这个解决方案不适合你,请补充一些细节。
    • 我的 ViewPager 上的所有列表图像保存在单个缓存上与我在 3 个 LruCaches 中的代码不同
    • 你为什么要自己实现cahcing逻辑而不使用某些库?
    • 我也想要,但我不知道你能给我一些建议吗?
    【解决方案2】:

    我写了这个和它的工作

    public class Cache  {
    LruCache<Integer,Bitmap> logoCache = new LruCache<>((int) (Runtime.getRuntime().maxMemory()/1024/8));
    
    public void setCache(int key, Bitmap bitmap){
    
        logoCache.put(key,bitmap);
    }
    public Bitmap getCache(int key){
    
        Bitmap bitmap = logoCache.get(key);
        return bitmap;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多