Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何防止快速滚动gridview时多次加载图像?_Android_Gridview_Concurrency_Scroll_Lazy Loading - Fatal编程技术网

Android 如何防止快速滚动gridview时多次加载图像?

Android 如何防止快速滚动gridview时多次加载图像?,android,gridview,concurrency,scroll,lazy-loading,Android,Gridview,Concurrency,Scroll,Lazy Loading,我有一个带有SD卡缩略图的gridview。我使用asynctask延迟加载图像。当我慢慢滚动时,它工作得很完美,但是当我滚动太快时,不同的图像会多次加载到同一个网格项目上,最终加载正确的图像需要6-7秒。我试图通过使用getFirstVisiblePosition和getLastVisiblePosition检查视图的位置是否可见,这次一些图像从未加载 您是否尝试缓存图像?以鲁卡什为例。 这是文件: 这里是官方教程 快速使用它: LruCache<String, Bitmap>

我有一个带有SD卡缩略图的gridview。我使用asynctask延迟加载图像。当我慢慢滚动时,它工作得很完美,但是当我滚动太快时,不同的图像会多次加载到同一个网格项目上,最终加载正确的图像需要6-7秒。我试图通过使用getFirstVisiblePosition和getLastVisiblePosition检查视图的位置是否可见,这次一些图像从未加载

您是否尝试缓存图像?以鲁卡什为例。 这是文件:

这里是官方教程

快速使用它:

LruCache<String, Bitmap> mMemoryCache;
public void onCreate(Bundle b)
{
 .....
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };
....
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}
public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}
LruCache-mMemoryCache;
创建时的公共void(Bundle b)
{
.....
final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
最终int cacheSize=maxMemory/8;
mMemoryCache=新的LruCache(缓存大小){
@凌驾
受保护的int-sizeOf(字符串键、位图){
//缓存大小将以KB为单位,而不是以字节为单位
//项目数量。
返回bitmap.getByteCount()/1024;
}
};
....
}
public void addBitmapToMemoryCache(字符串键、位图){
if(getBitmapFromMemCache(key)==null){
mMemoryCache.put(键,位图);
}
}
公共位图getBitmapFromMemCache(字符串键){
返回mMemoryCache.get(key);
}
编辑: 如果以异步方式加载图像,则必须查看本教程:


它向您展示了如何处理并发性,并在当前任务正在运行时取消当前任务

您是否尝试缓存图像?以鲁卡什为例。 这是文件:

这里是官方教程

快速使用它:

LruCache<String, Bitmap> mMemoryCache;
public void onCreate(Bundle b)
{
 .....
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };
....
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}
public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}
LruCache-mMemoryCache;
创建时的公共void(Bundle b)
{
.....
final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
最终int cacheSize=maxMemory/8;
mMemoryCache=新的LruCache(缓存大小){
@凌驾
受保护的int-sizeOf(字符串键、位图){
//缓存大小将以KB为单位,而不是以字节为单位
//项目数量。
返回bitmap.getByteCount()/1024;
}
};
....
}
public void addBitmapToMemoryCache(字符串键、位图){
if(getBitmapFromMemCache(key)==null){
mMemoryCache.put(键,位图);
}
}
公共位图getBitmapFromMemCache(字符串键){
返回mMemoryCache.get(key);
}
编辑: 如果以异步方式加载图像,则必须查看本教程:


它向您展示了如何处理并发性,并在当前任务正在运行时取消当前任务

您是否尝试缓存图像?以鲁卡什为例。 这是文件:

这里是官方教程

快速使用它:

LruCache<String, Bitmap> mMemoryCache;
public void onCreate(Bundle b)
{
 .....
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };
....
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}
public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}
LruCache-mMemoryCache;
创建时的公共void(Bundle b)
{
.....
final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
最终int cacheSize=maxMemory/8;
mMemoryCache=新的LruCache(缓存大小){
@凌驾
受保护的int-sizeOf(字符串键、位图){
//缓存大小将以KB为单位,而不是以字节为单位
//项目数量。
返回bitmap.getByteCount()/1024;
}
};
....
}
public void addBitmapToMemoryCache(字符串键、位图){
if(getBitmapFromMemCache(key)==null){
mMemoryCache.put(键,位图);
}
}
公共位图getBitmapFromMemCache(字符串键){
返回mMemoryCache.get(key);
}
编辑: 如果以异步方式加载图像,则必须查看本教程:


它向您展示了如何处理并发性,并在当前任务正在运行时取消当前任务

您是否尝试缓存图像?以鲁卡什为例。 这是文件:

这里是官方教程

快速使用它:

LruCache<String, Bitmap> mMemoryCache;
public void onCreate(Bundle b)
{
 .....
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };
....
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}
public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}
LruCache-mMemoryCache;
创建时的公共void(Bundle b)
{
.....
final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
最终int cacheSize=maxMemory/8;
mMemoryCache=新的LruCache(缓存大小){
@凌驾
受保护的int-sizeOf(字符串键、位图){
//缓存大小将以KB为单位,而不是以字节为单位
//项目数量。
返回bitmap.getByteCount()/1024;
}
};
....
}
public void addBitmapToMemoryCache(字符串键、位图){
if(getBitmapFromMemCache(key)==null){
mMemoryCache.put(键,位图);
}
}
公共位图getBitmapFromMemCache(字符串键){
返回mMemoryCache.get(key);
}
编辑: 如果以异步方式加载图像,则必须查看本教程:


它向您展示了如何处理并发性并在运行时取消当前任务

如果我知道您的问题是在滚动时图像被加载了多次,那么使用缓存,您只需使用缓存的图像,或者在尚未缓存的情况下加载一次。我的问题是,快速滚动gridview时,会启动多个asyntask,当滚动完成时,这些不同的asyntask将不同的图像加载到同一个imageview中,因为gridview多次使用同一个视图。因此,每个可见图像视图在6-7秒内加载7-8个不同的图像,直到最后一个asyntask完成并加载正确的图像!我明白,看到我的编辑我使用这种方法,它的工作非常完美。只需遵循教程步骤:)如果我了解您的问题是在滚动时图像被加载了多次,使用缓存,您只需使用缓存的图像,或者在尚未缓存的情况下加载一次。我的问题是,快速滚动gridview时,当滚动完成时,会启动多个asyntask,这些不同的asyntask将不同的图像加载到相同的imageview中,因为gridview多次使用相同的视图。因此,每个可见图像视图在6-7秒内加载7-8个不同的图像,直到最后一个asyntask完成并加载正确的图像!我明白,看到我的编辑我使用这种方法,它的工作非常完美。只需要跟着图坦卡蒙