更改图像时画廊滞后

时间:2011-12-21 02:37:21

标签: android gallery

我尝试使用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;
}

如何修改我的代码可以避免每次加载和延迟?

1 个答案:

答案 0 :(得分:1)

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);
    }
}

也许你可以试试这个。

相关问题