Skip to content

Instantly share code, notes, and snippets.

@AlexBlokh
Created September 22, 2017 13:21
Show Gist options
  • Save AlexBlokh/c4294d355048c50cba137895be7a6a8d to your computer and use it in GitHub Desktop.
Save AlexBlokh/c4294d355048c50cba137895be7a6a8d to your computer and use it in GitHub Desktop.
RoundedImageDrawable + Glide 4+
public class DrawableFadeInFactory implements TransitionFactory<Drawable> {
private final int duration;
private DrawableFadeInTransition resourceTransition;
protected DrawableFadeInFactory(int duration) {
this.duration = duration;
}
@Override
public Transition<Drawable> build(DataSource dataSource, boolean isFirstResource) {
return dataSource == DataSource.MEMORY_CACHE
? NoTransition.<Drawable>get() : getResourceTransition();
}
private Transition<Drawable> getResourceTransition() {
if (resourceTransition == null) {
resourceTransition = new DrawableFadeInTransition(duration);
}
return resourceTransition;
}
public static class Builder {
private static final int DEFAULT_DURATION_MS = 300;
private int durationMillis;
public Builder() {
this(DEFAULT_DURATION_MS);
}
public Builder(int durationMillis) {
this.durationMillis = durationMillis;
}
public DrawableFadeInFactory build() {
return new DrawableFadeInFactory(durationMillis);
}
}
}
public class DrawableFadeInTransition implements Transition<Drawable> {
private final int duration;
public DrawableFadeInTransition(int duration) {
this.duration = duration;
}
@Override
public boolean transition(Drawable current, ViewAdapter adapter) {
Drawable previous = adapter.getCurrentDrawable();
if (previous == null) {
previous = new ColorDrawable(Color.TRANSPARENT);
}
TransitionDrawable transitionDrawable = new TransitionDrawable(previous, current);
transitionDrawable.startTransition(duration);
adapter.setDrawable(transitionDrawable);
return true;
}
}
public class TransitionDrawable extends LayerDrawable {
private final static int TO_ANIMATE = 0;
private final static int ANIMATING = 1;
private final static int IDLE = 2;
private int currentState = IDLE;
private int duration;
private long transitionTimeStart;
public TransitionDrawable(Drawable background, Drawable foreground) {
super(new Drawable[]{background, foreground});
setId(0, 0);
setId(1, 1);
}
@Override
public void draw(Canvas canvas) {
if (!isVisible()) {
return;
}
Drawable background = getDrawable(0);
Drawable foreground = getDrawable(1);
switch (currentState) {
case IDLE:
foreground.setAlpha(0xFF);
foreground.draw(canvas);
break;
case TO_ANIMATE:
transitionTimeStart = SystemClock.uptimeMillis();
currentState = ANIMATING;
case ANIMATING:
int delta = (int) (SystemClock.uptimeMillis() - transitionTimeStart);
if (delta < duration) {
float fraction = 1 - ((float) (duration - delta)) / duration;
int alpha = (int) (0xFF * fraction);
if (background != null)
background.draw(canvas);
foreground.setAlpha(alpha);
foreground.draw(canvas);
invalidateSelf();
} else {
foreground.setAlpha(0xFF);
foreground.draw(canvas);
currentState = IDLE;
}
break;
}
}
public void startTransition(int duration) {
this.duration = duration;
currentState = TO_ANIMATE;
invalidateSelf();
}
}
ImageView imageView = findViewById(R.id.image);
DrawableTransitionOptions transitionOptions
= DrawableTransitionOptions.with(new DrawableFadeInFactory(150));
Glide.with(this)
.load("url")
.transition(transitionOptions)
.into(imageView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment