aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/resource/drawable/DrawableResource.java
blob: b3efc6219d0812eebfe8915314fded68b9e3542e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.bumptech.glide.load.resource.drawable;

import android.graphics.drawable.Drawable;

import com.bumptech.glide.load.engine.Resource;

/**
 * Simple wrapper for an Android {@link Drawable} which returns a
 * {@link android.graphics.drawable.Drawable.ConstantState#newDrawable() new drawable}
 * based on it's {@link android.graphics.drawable.Drawable.ConstantState state}.
 *
 * <b>Suggested usages only include {@code T}s where the new drawable is of the same or descendant class.</b>
 *
 * @param <T> type of the wrapped {@link Drawable}
 */
public abstract class DrawableResource<T extends Drawable> implements Resource<T> {
    protected final T drawable;
    private boolean returnedOriginalDrawable;

    public DrawableResource(T drawable) {
        this.drawable = drawable;
    }

    @SuppressWarnings("unchecked")
    // drawables should always return a copy of the same class
    @Override
    public final T get() {
        if (!returnedOriginalDrawable) {
            returnedOriginalDrawable = true;
            return drawable;
        } else {
            return (T) drawable.getConstantState().newDrawable();
        }
    }
}