我的应用程序允许用户将列数增加到5.当列设置为1-4时,似乎没有jank。
这是一个使用recyclerview和Glide的Android图库应用。
当列设置为5时,jank开始出现
你可以看到红色的组。这是Recyclerview快速滚动的时候。但是在上面的systrace上快速滚动(第1-4列)并没有导致jank。
据我所知,当列数设置为5时,屏幕上会有更多的图像视图,而当滚动速度快时,它会尝试一次加载这么多。
我已经尝试从我的布局和适配器中删除所有内容,以便达到这一点但是当我增加到5列时仍然会变得很猛。
我的问题是我能做些什么来改善它?
GlideModule
@Override
public void applyOptions(Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
.setMemoryCacheScreens(5)
.build();
builder.setMemoryCache(new LruResourceCache(calculator.getMemoryCacheSize()));
MemorySizeCalculator calculator2 = new MemorySizeCalculator.Builder(context)
.setBitmapPoolScreens(5)
.build();
builder.setBitmapPool(new LruBitmapPool(calculator2.getBitmapPoolSize()));
int diskCacheSizeBytes = 1024 * 1024 * 200; // 200 MB
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));
builder.setDefaultRequestOptions(
new RequestOptions()
//.placeholder(getPlaceHolder())
//.error(getErrorPlaceHolder())
//todo added for emulator
//.disallowHardwareConfig()
.format(DecodeFormat.PREFER_ARGB_8888)
.diskCacheStrategy(DiskCacheStrategy.ALL));
}
片段
int spanCount = columnsCount();
GridSpacingItemDecoration spacingDecoration = new GridSpacingItemDecoration(spanCount, Measure.pxToDp(3, act), true);
rv.setHasFixedSize(true);
rv.addItemDecoration(spacingDecoration);
mLayoutManager = new GridLayoutManager(getContext(), spanCount);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (mAdapter.getAllItems().get(position).getType() == TypeItem.TYPE_HEAD){
return spanCount;
} else {
return 1;
}
}
});
rv.setLayoutManager(mLayoutManager);
mAdapter = new MediaAdapter3(getContext(), GlideApp.with(this), sortingMode(), sortingOrder());
rv.setAdapter(mAdapter);
适配器的Bindview
Key signature = new MediaStoreSignature(media.getMimeType(), media.getDateModified(), media.getOrientation());
RequestOptions options = new RequestOptions()
.signature(signature)
.centerCrop()
.placeholder(placeholder);
glide
.load(media.getUri())
.apply(options)
.thumbnail(0.1f)
.into(imageView);
布局
<?xml version="1.0" encoding="utf-8"?>
<***.SquareRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/media_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/ripple">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/thumb_height">
<ImageView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/photo_preview"/>
</RelativeLayout>
</***.SquareRelativeLayout>