我想使用gallery 在網絡中顯示圖像。當左轉或右轉來改變圖像時,每次圖像從網絡上加載再顯示總會延遲3秒。或許是當這個圖像顯示時,系統在背景加載另外一個圖像。或者在緩存中保存了所有加載的圖像,但是這可能導致內存不足。
代碼如下:
gallery = (MyGallery) this.findViewById(R.id.gallery_photo);
gallery.setAdapter(new GalleryViewerAdapter(this, URL));
代碼中的URL 指的是URL中的字符串數組。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(this.mContext);
try {
URL aryURL = new URL(URL[i]);
URLConnection conn = aryURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(is);
is.close();
iv.setImageBitmap(bm);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return iv;
}
如何修改代碼來避免每次的加載和延遲?
public class MyGallery extends Gallery {
public MyGallery(Context context, AttributeSet attrSet) {
super(context, attrSet);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
protected android.view.ViewGroup.LayoutParams generateLayoutParams (android.view.ViewGroup.LayoutParams p) {
return super.generateLayoutParams(p);
}
}