我已经使用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();
}
}
}