我正在GridView中显示服务器上带有URL的大约50多个图像,其中图像为(120 * 120) 加载图像时花费太多时间。平均原始图片大小约为50-200 KB
滑行代码:
在GridViewAdapter中
RequestOptions reqOpt = RequestOptions.fitCenterTransform().transform(new RoundedCorners(5));
...
GlideApp
.with(context)
.load(item.getUrl())
.apply(reqOpt)
.placeholder(R.drawable.place_holder)
.into(holder.ivThumb);
在Gradle中
...
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
...
答案 0 :(得分:1)
尝试一下,
它将优化您的内存以加载图像。
RequestOptions reqOpt = RequestOptions
.fitCenterTransform()
.transform(new RoundedCorners(5))
.diskCacheStrategy(DiskCacheStrategy.ALL) // It will cache your image after loaded for first time
.override(holder.ivThumb.getWidth(),holder.ivThumb.getHeight()) // Overrides size of downloaded image and converts it's bitmaps to your desired image size;
从此处查看更多信息:Glide reference
答案 1 :(得分:0)
仅当您从网址获取图片时添加此代码,此代码会减小该图片的大小
您也可以这样做
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
//save scaled down image to cache dir
newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File imageFile = new File(filePath);
// write the bytes in file
FileOutputStream fo = new FileOutputStream(imageFile);
fo.write(bytes.toByteArray());
答案 2 :(得分:0)
您可以在缩略图中使用-
GlideApp.with(context)
.load(item.getUrl())
.thumbnail(/*sizeMultiplier=*/ 0.25f)
.apply(reqOpt)
.placeholder(R.drawable.place_holder)
.into(holder.ivThumb);
答案 3 :(得分:0)
您可以使用picaso,它的加载速度比滑行要快
Picasso.get()
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
implementation 'com.squareup.picasso:picasso:2.71828'