我正在尝试使用 ImageLoader 库将来自 Internet 的图像加载到网格视图中。但是,在imageManager.getLoader().load(fView.pic);
.
有人可以告诉我我错在哪里。
StartApp 类:
public class StartApp extends Application {
private static ImageManager imageManager;
@Override
public void onCreate() {
super.onCreate();
LoaderSettings settings = new LoaderSettings();
settings.setDisconnectOnEveryCall(true);
settings.setCacheManager(new LruBitmapCache(this));
imageManager = new ImageManager(this, settings);
// // To clean the file cache
// imageManager.getFileManager().clean();
}
public static ImageManager getImageLoader() {
return imageManager;
}
}
活动类:
public class MainActivity extends Activity {
private GridView gridview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setPadding(10, 10, 10, 10);
gridview.setAdapter(new GridImageAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
扩展 BaseAdapter 的 GridView 适配器类:
public class GridImageAdapter extends BaseAdapter {
private Context context;
private int width = 270;
private int height = 270;
private int textFactor = 35;
private LayoutInflater layoutInflater;
private String[] nameEvents = {"image1","image2"};
private String[] urls = {"http://api.androidhive.info/images/sample.jpg","http://api.androidhive.info/images/sample.jpg" };
ImageManager imageManager;
ImageTagFactory imageTagFactory;
public GridImageAdapter(Context mContext) {
context = mContext;
layoutInflater = LayoutInflater.from(context);
/*Image Loader*/
imageManager = StartApp.getImageLoader();
imageTagFactory = ImageTagFactory.newInstance(context, R.drawable.ic_launcher);
imageTagFactory.setErrorImageId(R.drawable.not_found);
}
public int getCount() {
return nameEvents.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
FriendView fView = new FriendView();
int imagePadding = 10;
if (convertView == null) {
convertView = new View(context);
convertView = layoutInflater.inflate(R.layout.grid_cell_layout,
null);
// convertView.setBackgroundColor(Color.DKGRAY);
convertView.setPadding(3, 0, imagePadding - 1, imagePadding);
convertView.setBackgroundResource(R.drawable.background_shadow);
convertView
.setLayoutParams(new GridView.LayoutParams(width, height));
} else {
fView = (FriendView) convertView.getTag();
}
fView.pic = (ImageView) convertView.findViewById(R.id.image);
/* height has to be reduced to display the text */
fView.pic.setLayoutParams(new LinearLayout.LayoutParams(width, height
- textFactor));
fView.pic.setAdjustViewBounds(true);
fView.pic.setScaleType(ImageView.ScaleType.FIT_XY);
//((StartApp) context.getApplicationContext()).getImageLoader().getLoader().load(imageView);
fView.name = (TextView) convertView.findViewById(R.id.title);
fView.name.setTextColor(Color.BLACK);
convertView.setTag(fView);
fView.name.setText(nameEvents[position]);
// GridImageAdapter.imageLoader.DisplayImage(urls[position], fView.pic);
((ImageView) fView.pic).setTag(imageTagFactory.build(urls[position],context));
imageManager.getLoader().load(fView.pic);
return convertView;
}
static class FriendView {
TextView name;
ImageView pic;
}
}
提前致谢。