我正在使用GridView中的服务器显示大约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'
...
试试这个,
它将优化您的内存以加载图像。
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
你可以在缩略图中使用 -
GlideApp.with(context)
.load(item.getUrl())
.thumbnail(/*sizeMultiplier=*/ 0.25f)
.apply(reqOpt)
.placeholder(R.drawable.place_holder)
.into(holder.ivThumb);
你可以使用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'
只需在从URL获取图像时添加此项,此代码就会减小该图像的大小
你也可以这样做,
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());