我正在尝试一个简单的测试。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int N = 20;
private final List<ImageView> images = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout root = ((GridLayout) findViewById(R.id.root));
for (int i = 0; i < N; i++) {
ImageView image = new ImageView(this);
images.add(image);
root.addView(image, 100, 100);
}
findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
load();
}
});
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build()
);
load();
}
private void load() {
for (int i = 0; i < images.size(); i++)
Glide.with(this).load("https://dummyimage.com/100x100/000/fff&text=" + (i + 1)).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).priority(Priority.IMMEDIATE).into(images.get(i));
}
}
MyGlideModule.java
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
}
@Override
public void registerComponents(Context context, Glide glide) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
client.addInterceptor(logging);
client.addNetworkInterceptor(new StethoInterceptor());
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client.build());
glide.register(GlideUrl.class, InputStream.class, factory);
}
}
问题是图像是逐个加载的,但我希望它们会同时加载。
它在Chrome网络跟踪中得到了很好的展示(感谢Stetho)
是否可以滑动开始一次加载所有图像?
答案 0 :(得分:1)
解决方案是为Glide手动设置池执行器(以及可选的OkHttp):
public class MyGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setResizeService(new FifoPriorityThreadPoolExecutor(100));
}
@Override
public void registerComponents(Context context, Glide glide) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectionPool(new ConnectionPool(100, 3, TimeUnit.SECONDS));
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(builder.build());
glide.register(GlideUrl.class, InputStream.class, factory);
}
}
默认情况下,Glide使用的线程池大小等于CPU核心数。因此,如果它仅检测到1个核心,则一次只能加载一个核心 您可以根据需要在池中定义任意数量的线程(例如,此处为100)。
反过来,OkHttp在连接池上也有默认限制,并且它没有像你期望的那样糊涂(我在模拟器上发现只有4)。您可以通过将带有自定义参数的连接池对象传递给OkHttp构建器来增加此限制。