Add attribute for image-loader module

This commit is contained in:
Dimitry Ivanov 2018-07-17 13:10:39 +03:00
parent 65289867c4
commit b1ec2c373d
2 changed files with 27 additions and 0 deletions

View File

@ -79,6 +79,7 @@ class AppModule {
.client(client) .client(client)
.executorService(executorService) .executorService(executorService)
.resources(resources) .resources(resources)
.autoPlayGif(false)
.build(); .build();
} }

View File

@ -59,6 +59,9 @@ public class AsyncDrawableLoader implements AsyncDrawable.Loader {
private final Handler mainThread; private final Handler mainThread;
private final Drawable errorDrawable; private final Drawable errorDrawable;
// @since 1.1.0
private final boolean autoPlayGif;
private final Map<String, Future<?>> requests; private final Map<String, Future<?>> requests;
AsyncDrawableLoader(Builder builder) { AsyncDrawableLoader(Builder builder) {
@ -67,6 +70,7 @@ public class AsyncDrawableLoader implements AsyncDrawable.Loader {
this.executorService = builder.executorService; this.executorService = builder.executorService;
this.mainThread = new Handler(Looper.getMainLooper()); this.mainThread = new Handler(Looper.getMainLooper());
this.errorDrawable = builder.errorDrawable; this.errorDrawable = builder.errorDrawable;
this.autoPlayGif = builder.autoPlayGif;
this.requests = new HashMap<>(3); this.requests = new HashMap<>(3);
} }
@ -294,8 +298,15 @@ public class AsyncDrawableLoader implements AsyncDrawable.Loader {
final byte[] bytes = readBytes(stream); final byte[] bytes = readBytes(stream);
if (bytes != null) { if (bytes != null) {
try { try {
out = new GifDrawable(bytes); out = new GifDrawable(bytes);
DrawableUtils.intrinsicBounds(out); DrawableUtils.intrinsicBounds(out);
// @since 1.1.0
if (!autoPlayGif) {
((GifDrawable) out).pause();
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -346,6 +357,9 @@ public class AsyncDrawableLoader implements AsyncDrawable.Loader {
private ExecutorService executorService; private ExecutorService executorService;
private Drawable errorDrawable; private Drawable errorDrawable;
// @since 1.1.0
private boolean autoPlayGif = true;
public Builder client(@NonNull OkHttpClient client) { public Builder client(@NonNull OkHttpClient client) {
this.client = client; this.client = client;
return this; return this;
@ -366,6 +380,18 @@ public class AsyncDrawableLoader implements AsyncDrawable.Loader {
return this; return this;
} }
/**
* @param autoPlayGif flag indicating if loaded gif should automatically start when displayed
* @return self
* @since 1.1.0
*/
@NonNull
public Builder autoPlayGif(boolean autoPlayGif) {
this.autoPlayGif = autoPlayGif;
return this;
}
@NonNull
public AsyncDrawableLoader build() { public AsyncDrawableLoader build() {
if (client == null) { if (client == null) {
client = new OkHttpClient(); client = new OkHttpClient();