2019-12-31 14:08:08 +01:00
|
|
|
package org.telegram.ui.Components;
|
|
|
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.ColorFilter;
|
|
|
|
import android.graphics.PixelFormat;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2022-06-21 04:51:00 +02:00
|
|
|
import android.util.Log;
|
2019-12-31 14:08:08 +01:00
|
|
|
|
2022-04-16 16:43:17 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2019-12-31 14:08:08 +01:00
|
|
|
public class CrossfadeDrawable extends Drawable {
|
|
|
|
|
|
|
|
private final Drawable topDrawable;
|
|
|
|
private final Drawable bottomDrawable;
|
|
|
|
|
|
|
|
private float progress;
|
2022-06-21 04:51:00 +02:00
|
|
|
float globalAlpha = 255f;
|
2019-12-31 14:08:08 +01:00
|
|
|
|
|
|
|
public CrossfadeDrawable(Drawable topDrawable, Drawable bottomDrawable) {
|
|
|
|
this.topDrawable = topDrawable;
|
|
|
|
this.bottomDrawable = bottomDrawable;
|
2022-04-16 16:43:17 +02:00
|
|
|
|
|
|
|
if (topDrawable != null) {
|
|
|
|
topDrawable.setCallback(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void invalidateDrawable(@NonNull Drawable drawable) {
|
|
|
|
if (progress < 1.0f) {
|
|
|
|
CrossfadeDrawable.this.invalidateSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
2022-06-21 04:51:00 +02:00
|
|
|
public void scheduleDrawable(@NonNull Drawable drawable, @NonNull Runnable runnable, long l) {
|
|
|
|
if (progress < 1.0f) {
|
|
|
|
CrossfadeDrawable.this.scheduleSelf(runnable, l);
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 16:43:17 +02:00
|
|
|
@Override
|
2022-06-21 04:51:00 +02:00
|
|
|
public void unscheduleDrawable(@NonNull Drawable drawable, @NonNull Runnable runnable) {
|
|
|
|
if (progress < 1.0f) {
|
|
|
|
CrossfadeDrawable.this.unscheduleSelf(runnable);
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 16:43:17 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (bottomDrawable != null) {
|
|
|
|
bottomDrawable.setCallback(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void invalidateDrawable(@NonNull Drawable drawable) {
|
|
|
|
if (progress > 0.0f) {
|
|
|
|
CrossfadeDrawable.this.invalidateSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void scheduleDrawable(@NonNull Drawable drawable, @NonNull Runnable runnable, long l) {}
|
|
|
|
@Override
|
|
|
|
public void unscheduleDrawable(@NonNull Drawable drawable, @NonNull Runnable runnable) {}
|
|
|
|
});
|
|
|
|
}
|
2019-12-31 14:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onBoundsChange(Rect bounds) {
|
|
|
|
topDrawable.setBounds(bounds);
|
|
|
|
bottomDrawable.setBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(Canvas canvas) {
|
2022-06-21 04:51:00 +02:00
|
|
|
int topAlpha, bottomAlpha;
|
|
|
|
topDrawable.setAlpha(topAlpha = (int) (globalAlpha * (1.0f - progress)));
|
|
|
|
bottomDrawable.setAlpha(bottomAlpha = (int) (globalAlpha * progress));
|
|
|
|
if (topAlpha > 0) {
|
2019-12-31 14:08:08 +01:00
|
|
|
topDrawable.draw(canvas);
|
|
|
|
}
|
2022-06-21 04:51:00 +02:00
|
|
|
if (bottomAlpha > 0) {
|
2019-12-31 14:08:08 +01:00
|
|
|
bottomDrawable.draw(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAlpha(int alpha) {
|
2022-06-21 04:51:00 +02:00
|
|
|
globalAlpha = alpha;
|
2019-12-31 14:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setColorFilter(ColorFilter colorFilter) {
|
2022-04-21 20:03:20 +02:00
|
|
|
topDrawable.setColorFilter(colorFilter);
|
2019-12-31 14:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOpacity() {
|
|
|
|
return PixelFormat.TRANSLUCENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIntrinsicWidth() {
|
|
|
|
return topDrawable.getIntrinsicWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIntrinsicHeight() {
|
|
|
|
return topDrawable.getIntrinsicHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getProgress() {
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProgress(float value) {
|
|
|
|
progress = value;
|
2022-06-21 04:51:00 +02:00
|
|
|
invalidateSelf();
|
2019-12-31 14:08:08 +01:00
|
|
|
}
|
|
|
|
}
|