0

我已经使用Google I\O Volley Volley实现了图库视图,Volley 演示并成功下载图像并进行缓存。

首先,当我使用 LruCache 进行缓存时,它非常好,画廊滚动也很好。但是 LruCache 并没有维护 16MB 的内存,而是只缓存了 4MB。我想要缓存高达 16MB。

所以我转而使用 DiscLruCache 代替 LruCache。它做我想要的缓存(16MB)。但问题是画廊视图滚动非常慢,下载时和完成所有缓存(16MB)后并不流畅。

这是我的代码。

    package com.technotalkative.volleyimageloading;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

public class SimpleViewFrag extends Fragment {

    public static final String TAG = SimpleViewFrag.class.getSimpleName();
    private Gallery mGallery;
    private List<MyCataLogData> simpleViewList = new ArrayList<MyCataLogData>();
    MainActivity mainActivity;
    private LayoutInflater inflater;
    private int height;
    private int width;
    private int mItemWidth;
    private int mItemHeight;
    private SimpleViewAdapter mAdapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity = (MainActivity) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        height = display.getHeight();
        width = display.getWidth();

        mItemWidth = (int) (width / 1.7f);
        mItemHeight = (int) (height / 3f);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        this.inflater = inflater;
        View v = null;
        v = inflater.inflate(R.layout.simple_view, container, false);


//      String s = getArguments().getString(UCConstants.CATALOG_CONTENT_KEY);
        mGallery = (Gallery) v.findViewById(R.id.simpleViewGalleruy);

        mAdapter = new SimpleViewAdapter();
        mGallery.setAdapter(mAdapter);

//      mGallery.setSelection(1);

        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // set gallery to left side
        MarginLayoutParams mlp = (MarginLayoutParams) mGallery.getLayoutParams();
        mlp.setMargins(-((int) (metrics.widthPixels / 3.8f)), mlp.topMargin,
                    mlp.rightMargin, mlp.bottomMargin);

        parseJsonForCataLog();


        mGallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                Log.e(TAG, "position: ====="+position);

            }
        });

        return v;
    }

    public class SimpleViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return simpleViewList.size();
        }

        @Override
        public Object getItem(int position) {
            return simpleViewList.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            NetworkImageView networkImageView = null;
            TextView tvTitle = null;
            TextView tvText = null;
            View childView = null;


            if (convertView == null) {
                childView = inflater.inflate(R.layout.simple_view_content_item, null);
            } else {
                childView = convertView;
            }

            if (childView != null) {

                networkImageView = (NetworkImageView) childView
                        .findViewById(R.id.networkImageView);

                networkImageView.getLayoutParams().width = mItemWidth;
                networkImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;

                //getting an object for gallery item
                MyCataLogData cld = simpleViewList.get(position);
                if (networkImageView != null) {
                         networkImageView
                            .setImageUrl(cld.getSimpleImageUrl(), ImageCacheManager.getInstance().getImageLoader());
                }
                tvTitle = (TextView) childView.findViewById(R.id.title);
                if (tvTitle != null) {
                    tvTitle.setText(cld.getSimpleTitle());
                }

                tvText = (TextView) childView.findViewById(R.id.text);
                if (tvText != null) {
                    tvText.setText(cld.getSimpleText());
                }

            }
            return childView;
        }
    }

    protected void parseJsonForCataLog() {
        try {

            String jsonObjectString = getArguments().getString("JSONObject");

            JSONObject rootResponce = new JSONObject(jsonObjectString);

            Log.e(TAG, "rootResponce: " + rootResponce);
            JSONArray simpleArray = rootResponce.optJSONArray("SIMPLE");

            if(simpleArray == null)
                return;

            for (int i = 0; i < simpleArray.length(); i++) {
                JSONObject childMenuObject = simpleArray.getJSONObject(i);

                String textSimple = childMenuObject.optString("text");
                String titleSimple = childMenuObject.optString("title");
                String imageUrlSimple = childMenuObject.optString("imageUrl");

                MyCataLogData mcld = new MyCataLogData(null ,textSimple, titleSimple, imageUrlSimple);
                simpleViewList.add(mcld);
//              Log.e(TAG, "" + childMenuObject);
            }

            mAdapter.notifyDataSetChanged();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
4

2 回答 2

3

我对 ListView 有同样的问题,但我在 googles iosched 应用程序中找到了解决方案。您可以尝试应用以下内容,但不推荐使用 Gallery 我建议使用 Lucas Rocha 的 TwoWayView 之类的水平列表视图。可以应用这个解决方案,只需要实现 TwoWayView.OnScrollListener

添加 AbsListView onscroll 侦听器方法并在快速滚动/甩动时停止凌空

公共类 SimpleViewFrag 扩展片段实现 AbsListView.OnScrollListener {

    ListView mListView;
    mListView.setOnScrollListener(this);

@Override
public void onScrollStateChanged( AbsListView view, int scrollState ) {
    // Pause disk cache access to ensure smoother scrolling
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
        VolleyHelper.getImageLoader().getRequestQueue().stop();
    } else {
        VolleyHelper.getImageLoader().getRequestQueue().start();
    }
}

@Override
public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ) {

}
于 2013-11-15T18:00:11.040 回答
1

如果我对您的理解正确(无法ImageLoader在您的示例中找到初始化),那么您似乎正在为Volley构造函数提供磁盘缓存ImageLoader,而您应该提供内存缓存

如果您的内存缓存的大小太有限,您应该编辑代码以使其更大,或者找到/创建按ImageCache您希望的方式工作的接口的新实现。

无论您选择什么 - 不要在需要内存缓存的地方使用磁盘缓存。

于 2013-11-03T08:26:43.883 回答