2021-06-25 02:43:10 +02:00
|
|
|
package org.telegram.ui.Components;
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2021-06-25 02:43:10 +02:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapShader;
|
|
|
|
import android.graphics.BlendMode;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.ColorFilter;
|
|
|
|
import android.graphics.ComposeShader;
|
|
|
|
import android.graphics.Matrix;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.graphics.PixelFormat;
|
|
|
|
import android.graphics.PorterDuff;
|
2021-09-20 07:54:41 +02:00
|
|
|
import android.graphics.PorterDuffColorFilter;
|
2021-06-25 02:43:10 +02:00
|
|
|
import android.graphics.PorterDuffXfermode;
|
|
|
|
import android.graphics.Rect;
|
|
|
|
import android.graphics.RectF;
|
|
|
|
import android.graphics.Shader;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2021-12-07 14:02:02 +01:00
|
|
|
import android.graphics.drawable.GradientDrawable;
|
2021-06-25 02:43:10 +02:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.view.View;
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
import androidx.core.graphics.ColorUtils;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2021-11-05 11:06:49 +01:00
|
|
|
import org.telegram.messenger.FileLog;
|
2021-08-31 21:06:39 +02:00
|
|
|
import org.telegram.messenger.NotificationCenter;
|
2021-06-25 02:43:10 +02:00
|
|
|
import org.telegram.messenger.Utilities;
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
|
|
|
|
public class MotionBackgroundDrawable extends Drawable {
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
private final static int ANIMATION_CACHE_BITMAPS_COUNT = 3;
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
private static final boolean useLegacyBitmap = Build.VERSION.SDK_INT < 28;
|
|
|
|
private static final boolean useSoftLight = Build.VERSION.SDK_INT >= 29;
|
2021-11-05 11:06:49 +01:00
|
|
|
private static boolean errorWhileGenerateLegacyBitmap = false;
|
|
|
|
private static float legacyBitmapScale = 0.7f;
|
2021-09-20 07:54:41 +02:00
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
private int[] colors = new int[]{
|
|
|
|
0xff426D57,
|
|
|
|
0xffF7E48B,
|
|
|
|
0xff87A284,
|
|
|
|
0xffFDF6CA
|
|
|
|
};
|
|
|
|
|
|
|
|
private long lastUpdateTime;
|
|
|
|
private WeakReference<View> parentView;
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
private final CubicBezierInterpolator interpolator = new CubicBezierInterpolator(0.33, 0.0, 0.0, 1.0);
|
2021-06-25 02:43:10 +02:00
|
|
|
|
|
|
|
private int translationY;
|
|
|
|
|
|
|
|
private boolean isPreview;
|
|
|
|
|
2021-12-30 11:52:40 +01:00
|
|
|
public float posAnimationProgress = 1.0f;
|
2021-06-25 02:43:10 +02:00
|
|
|
private int phase;
|
|
|
|
|
|
|
|
private RectF rect = new RectF();
|
|
|
|
private Bitmap currentBitmap;
|
2021-07-15 16:24:57 +02:00
|
|
|
private Bitmap gradientFromBitmap;
|
|
|
|
private Bitmap[] gradientToBitmap = new Bitmap[ANIMATION_CACHE_BITMAPS_COUNT];
|
2021-06-25 02:43:10 +02:00
|
|
|
private Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
|
|
|
|
private Paint paint2 = new Paint(Paint.FILTER_BITMAP_FLAG);
|
2021-07-15 16:24:57 +02:00
|
|
|
private Paint paint3 = new Paint();
|
2021-06-25 02:43:10 +02:00
|
|
|
private int intensity = 100;
|
2021-07-15 16:24:57 +02:00
|
|
|
private Canvas gradientCanvas;
|
|
|
|
private Canvas gradientFromCanvas;
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-08-31 21:06:39 +02:00
|
|
|
private boolean postInvalidateParent;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
private Bitmap patternBitmap;
|
|
|
|
private BitmapShader bitmapShader;
|
|
|
|
private BitmapShader gradientShader;
|
|
|
|
private Matrix matrix;
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
private boolean fastAnimation;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
private Canvas legacyCanvas;
|
|
|
|
private Bitmap legacyBitmap;
|
2021-09-20 07:54:41 +02:00
|
|
|
private Canvas legacyCanvas2;
|
|
|
|
private Bitmap legacyBitmap2;
|
2021-12-07 14:02:02 +01:00
|
|
|
private GradientDrawable gradientDrawable = new GradientDrawable();
|
2021-09-20 07:54:41 +02:00
|
|
|
private boolean invalidateLegacy;
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
private boolean rotationBack;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
private boolean rotatingPreview;
|
|
|
|
|
2021-08-31 21:06:39 +02:00
|
|
|
private Runnable updateAnimationRunnable = this::updateAnimation;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
private android.graphics.Rect patternBounds = new android.graphics.Rect();
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
private ColorFilter patternColorFilter;
|
2021-06-25 02:43:10 +02:00
|
|
|
private int roundRadius;
|
2021-09-20 07:54:41 +02:00
|
|
|
private float patternAlpha = 1f;
|
2022-01-10 03:27:47 +01:00
|
|
|
private float backgroundAlpha = 1f;
|
2021-09-20 07:54:41 +02:00
|
|
|
private int alpha = 255;
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-11-05 11:06:49 +01:00
|
|
|
private ColorFilter legacyBitmapColorFilter;
|
|
|
|
private int legacyBitmapColor;
|
|
|
|
|
2021-12-30 11:52:40 +01:00
|
|
|
private boolean isIndeterminateAnimation;
|
|
|
|
private Paint overrideBitmapPaint;
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
public MotionBackgroundDrawable() {
|
|
|
|
super();
|
2021-07-15 16:24:57 +02:00
|
|
|
init();
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public MotionBackgroundDrawable(int c1, int c2, int c3, int c4, boolean preview) {
|
2021-12-07 14:02:02 +01:00
|
|
|
this(c1, c2, c3 ,c4, 0, preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MotionBackgroundDrawable(int c1, int c2, int c3, int c4, int rotation, boolean preview) {
|
2021-06-25 02:43:10 +02:00
|
|
|
super();
|
|
|
|
isPreview = preview;
|
2021-12-07 14:02:02 +01:00
|
|
|
setColors(c1, c2, c3, c4, rotation, false);
|
2021-07-15 16:24:57 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
@SuppressLint("NewApi")
|
2021-07-15 16:24:57 +02:00
|
|
|
private void init() {
|
|
|
|
currentBitmap = Bitmap.createBitmap(60, 80, Bitmap.Config.ARGB_8888);
|
|
|
|
for (int i = 0; i < ANIMATION_CACHE_BITMAPS_COUNT; i++) {
|
|
|
|
gradientToBitmap[i] = Bitmap.createBitmap(60, 80, Bitmap.Config.ARGB_8888);
|
|
|
|
}
|
|
|
|
gradientCanvas = new Canvas(currentBitmap);
|
|
|
|
|
|
|
|
gradientFromBitmap = Bitmap.createBitmap(60, 80, Bitmap.Config.ARGB_8888);
|
|
|
|
gradientFromCanvas = new Canvas(gradientFromBitmap);
|
|
|
|
|
|
|
|
Utilities.generateGradient(currentBitmap, true, phase, interpolator.getInterpolation(posAnimationProgress), currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
2021-09-20 07:54:41 +02:00
|
|
|
if (useSoftLight) {
|
2021-06-25 02:43:10 +02:00
|
|
|
paint2.setBlendMode(BlendMode.SOFT_LIGHT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRoundRadius(int rad) {
|
|
|
|
roundRadius = rad;
|
|
|
|
matrix = new Matrix();
|
|
|
|
bitmapShader = new BitmapShader(currentBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
paint.setShader(bitmapShader);
|
|
|
|
invalidateParent();
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:06:39 +02:00
|
|
|
public BitmapShader getBitmapShader() {
|
|
|
|
return bitmapShader;
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
public Bitmap getBitmap() {
|
|
|
|
return currentBitmap;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
public int getIntensity() {
|
|
|
|
return intensity;
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
public static boolean isDark(int color1, int color2, int color3, int color4) {
|
|
|
|
int averageColor = AndroidUtilities.getAverageColor(color1, color2);
|
|
|
|
if (color3 != 0) {
|
|
|
|
averageColor = AndroidUtilities.getAverageColor(averageColor, color3);
|
|
|
|
}
|
|
|
|
if (color4 != 0) {
|
|
|
|
averageColor = AndroidUtilities.getAverageColor(averageColor, color4);
|
|
|
|
}
|
|
|
|
float[] hsb = AndroidUtilities.RGBtoHSB(Color.red(averageColor), Color.green(averageColor), Color.blue(averageColor));
|
|
|
|
return hsb[2] < 0.3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setBounds(Rect bounds) {
|
|
|
|
super.setBounds(bounds);
|
|
|
|
patternBounds.set(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPatternBounds(int left, int top, int right, int bottom) {
|
|
|
|
patternBounds.set(left, top, right, bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getPatternColor(int color1, int color2, int color3, int color4) {
|
|
|
|
if (isDark(color1, color2, color3, color4)) {
|
2021-09-20 07:54:41 +02:00
|
|
|
return !useSoftLight ? 0x7fffffff : 0xffffffff;
|
2021-06-25 02:43:10 +02:00
|
|
|
} else {
|
2021-09-20 07:54:41 +02:00
|
|
|
if (!useSoftLight) {
|
2021-06-25 02:43:10 +02:00
|
|
|
int averageColor = AndroidUtilities.getAverageColor(color3, AndroidUtilities.getAverageColor(color1, color2));
|
|
|
|
if (color4 != 0) {
|
|
|
|
averageColor = AndroidUtilities.getAverageColor(color4, averageColor);
|
|
|
|
}
|
|
|
|
return (AndroidUtilities.getPatternColor(averageColor, true) & 0x00ffffff) | 0x64000000;
|
|
|
|
} else {
|
|
|
|
return 0xff000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPatternColor() {
|
|
|
|
return getPatternColor(colors[0], colors[1], colors[2], colors[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPhase() {
|
|
|
|
return phase;
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:06:39 +02:00
|
|
|
public void setPostInvalidateParent(boolean value) {
|
|
|
|
postInvalidateParent = value;
|
|
|
|
}
|
|
|
|
|
2021-07-15 16:24:57 +02:00
|
|
|
public void rotatePreview(boolean back) {
|
2021-06-25 02:43:10 +02:00
|
|
|
if (posAnimationProgress < 1.0f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rotatingPreview = true;
|
|
|
|
posAnimationProgress = 0.0f;
|
2021-07-15 16:24:57 +02:00
|
|
|
rotationBack = back;
|
2021-06-25 02:43:10 +02:00
|
|
|
invalidateParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPhase(int value) {
|
|
|
|
phase = value;
|
|
|
|
if (phase < 0) {
|
|
|
|
phase = 0;
|
|
|
|
} else if (phase > 7) {
|
|
|
|
phase = 7;
|
|
|
|
}
|
|
|
|
Utilities.generateGradient(currentBitmap, true, phase, interpolator.getInterpolation(posAnimationProgress), currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void switchToNextPosition() {
|
2021-07-15 16:24:57 +02:00
|
|
|
switchToNextPosition(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void switchToNextPosition(boolean fast) {
|
2021-06-25 02:43:10 +02:00
|
|
|
if (posAnimationProgress < 1.0f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rotatingPreview = false;
|
2021-07-15 16:24:57 +02:00
|
|
|
rotationBack = false;
|
|
|
|
fastAnimation = fast;
|
2021-06-25 02:43:10 +02:00
|
|
|
posAnimationProgress = 0.0f;
|
|
|
|
phase--;
|
|
|
|
if (phase < 0) {
|
|
|
|
phase = 7;
|
|
|
|
}
|
|
|
|
invalidateParent();
|
2021-07-15 16:24:57 +02:00
|
|
|
gradientFromCanvas.drawBitmap(currentBitmap, 0, 0, null);
|
|
|
|
generateNextGradient();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void generateNextGradient() {
|
2021-09-20 07:54:41 +02:00
|
|
|
if (useLegacyBitmap && intensity < 0) {
|
2021-11-05 11:06:49 +01:00
|
|
|
try {
|
|
|
|
if (legacyBitmap != null) {
|
|
|
|
if (legacyBitmap2 == null || legacyBitmap2.getHeight() != legacyBitmap.getHeight() || legacyBitmap2.getWidth() != legacyBitmap.getWidth()) {
|
|
|
|
if (legacyBitmap2 != null) {
|
|
|
|
legacyBitmap2.recycle();
|
|
|
|
}
|
|
|
|
legacyBitmap2 = Bitmap.createBitmap(legacyBitmap.getWidth(), legacyBitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
legacyCanvas2 = new Canvas(legacyBitmap2);
|
|
|
|
} else {
|
|
|
|
legacyBitmap2.eraseColor(Color.TRANSPARENT);
|
2021-09-20 07:54:41 +02:00
|
|
|
}
|
2021-11-05 11:06:49 +01:00
|
|
|
legacyCanvas2.drawBitmap(legacyBitmap, 0, 0, null);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e(e);
|
|
|
|
if (legacyBitmap2 != null) {
|
|
|
|
legacyBitmap2.recycle();
|
|
|
|
legacyBitmap2 = null;
|
2021-09-20 07:54:41 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 11:06:49 +01:00
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
Utilities.generateGradient(currentBitmap, true, phase, 1f, currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
|
|
|
invalidateLegacy = true;
|
|
|
|
}
|
2021-07-15 16:24:57 +02:00
|
|
|
for (int i = 0; i < ANIMATION_CACHE_BITMAPS_COUNT; i++) {
|
|
|
|
float p = (i + 1) / (float) ANIMATION_CACHE_BITMAPS_COUNT;
|
|
|
|
Utilities.generateGradient(gradientToBitmap[i], true, phase, p, currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void switchToPrevPosition(boolean fast) {
|
|
|
|
if (posAnimationProgress < 1.0f) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rotatingPreview = false;
|
|
|
|
fastAnimation = fast;
|
|
|
|
rotationBack = true;
|
|
|
|
posAnimationProgress = 0.0f;
|
|
|
|
invalidateParent();
|
|
|
|
Utilities.generateGradient(gradientFromBitmap, true, phase, 0, currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
|
|
|
generateNextGradient();
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int[] getColors() {
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setParentView(View view) {
|
|
|
|
parentView = new WeakReference<>(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setColors(int c1, int c2, int c3, int c4) {
|
2021-12-07 14:02:02 +01:00
|
|
|
setColors(c1, c2, c3, c4, 0, true);
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
public void setColors(int c1, int c2, int c3, int c4, Bitmap bitmap) {
|
|
|
|
colors[0] = c1;
|
|
|
|
colors[1] = c2;
|
|
|
|
colors[2] = c3;
|
|
|
|
colors[3] = c4;
|
|
|
|
Utilities.generateGradient(bitmap, true, phase, interpolator.getInterpolation(posAnimationProgress), currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:02:02 +01:00
|
|
|
public void setColors(int c1, int c2, int c3, int c4, int rotation, boolean invalidate) {
|
|
|
|
if (isPreview && c3 == 0 && c4 == 0) {
|
|
|
|
gradientDrawable = new GradientDrawable(BackgroundGradientDrawable.getGradientOrientation(rotation), new int[]{c1, c2});
|
|
|
|
} else {
|
|
|
|
gradientDrawable = null;
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
colors[0] = c1;
|
|
|
|
colors[1] = c2;
|
|
|
|
colors[2] = c3;
|
|
|
|
colors[3] = c4;
|
2021-12-07 14:02:02 +01:00
|
|
|
if (currentBitmap != null) {
|
|
|
|
Utilities.generateGradient(currentBitmap, true, phase, interpolator.getInterpolation(posAnimationProgress), currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
|
|
|
if (invalidate) {
|
|
|
|
invalidateParent();
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void invalidateParent() {
|
2021-09-20 07:54:41 +02:00
|
|
|
invalidateSelf();
|
2021-06-25 02:43:10 +02:00
|
|
|
if (parentView != null && parentView.get() != null) {
|
|
|
|
parentView.get().invalidate();
|
|
|
|
}
|
2021-08-31 21:06:39 +02:00
|
|
|
if (postInvalidateParent) {
|
|
|
|
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.invalidateMotionBackground);
|
|
|
|
updateAnimation();
|
|
|
|
AndroidUtilities.cancelRunOnUIThread(updateAnimationRunnable);
|
|
|
|
AndroidUtilities.runOnUIThread(updateAnimationRunnable, 16);
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPattern() {
|
|
|
|
return patternBitmap != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIntrinsicWidth() {
|
|
|
|
if (patternBitmap != null) {
|
|
|
|
return patternBitmap.getWidth();
|
|
|
|
}
|
|
|
|
return super.getIntrinsicWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIntrinsicHeight() {
|
|
|
|
if (patternBitmap != null) {
|
|
|
|
return patternBitmap.getHeight();
|
|
|
|
}
|
|
|
|
return super.getIntrinsicHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTranslationY(int y) {
|
|
|
|
translationY = y;
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
public void setPatternBitmap(int intensity) {
|
|
|
|
setPatternBitmap(intensity, patternBitmap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NewApi")
|
2021-06-25 02:43:10 +02:00
|
|
|
public void setPatternBitmap(int intensity, Bitmap bitmap) {
|
|
|
|
this.intensity = intensity;
|
|
|
|
patternBitmap = bitmap;
|
2021-09-20 07:54:41 +02:00
|
|
|
invalidateLegacy = true;
|
|
|
|
if (patternBitmap == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (useSoftLight) {
|
2021-06-25 02:43:10 +02:00
|
|
|
if (intensity >= 0) {
|
|
|
|
paint2.setBlendMode(BlendMode.SOFT_LIGHT);
|
|
|
|
} else {
|
|
|
|
paint2.setBlendMode(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (intensity < 0) {
|
2021-09-20 07:54:41 +02:00
|
|
|
if (!useLegacyBitmap) {
|
2021-06-25 02:43:10 +02:00
|
|
|
bitmapShader = new BitmapShader(currentBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
gradientShader = new BitmapShader(patternBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
|
|
|
|
paint2.setShader(new ComposeShader(bitmapShader, gradientShader, PorterDuff.Mode.DST_IN));
|
|
|
|
matrix = new Matrix();
|
|
|
|
} else {
|
2021-09-20 07:54:41 +02:00
|
|
|
createLegacyBitmap();
|
2021-11-05 11:06:49 +01:00
|
|
|
if (!errorWhileGenerateLegacyBitmap) {
|
|
|
|
paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
|
|
|
|
} else {
|
|
|
|
paint2.setXfermode(null);
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
2021-09-20 07:54:41 +02:00
|
|
|
} else {
|
|
|
|
if (!useLegacyBitmap) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
paint2.setXfermode(null);
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
public void setPatternColorFilter(int color) {
|
|
|
|
patternColorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
|
|
|
|
invalidateParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPatternAlpha(float alpha) {
|
|
|
|
this.patternAlpha = alpha;
|
|
|
|
invalidateParent();
|
|
|
|
}
|
2022-01-10 03:27:47 +01:00
|
|
|
public void setBackgroundAlpha(float alpha) {
|
|
|
|
this.backgroundAlpha = alpha;
|
|
|
|
invalidateParent();
|
|
|
|
}
|
2021-09-20 07:54:41 +02:00
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
@Override
|
|
|
|
public void setBounds(int left, int top, int right, int bottom) {
|
|
|
|
super.setBounds(left, top, right, bottom);
|
|
|
|
patternBounds.set(left, top, right, bottom);
|
2021-09-20 07:54:41 +02:00
|
|
|
createLegacyBitmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createLegacyBitmap() {
|
2021-11-05 11:06:49 +01:00
|
|
|
if (useLegacyBitmap && intensity < 0 && !errorWhileGenerateLegacyBitmap) {
|
|
|
|
int w = (int) (patternBounds.width() * legacyBitmapScale);
|
|
|
|
int h = (int) (patternBounds.height() * legacyBitmapScale);
|
2021-07-19 17:56:43 +02:00
|
|
|
if (w > 0 && h > 0 && (legacyBitmap == null || legacyBitmap.getWidth() != w || legacyBitmap.getHeight() != h)) {
|
2021-06-25 02:43:10 +02:00
|
|
|
if (legacyBitmap != null) {
|
|
|
|
legacyBitmap.recycle();
|
|
|
|
}
|
2021-09-20 07:54:41 +02:00
|
|
|
if (legacyBitmap2 != null) {
|
|
|
|
legacyBitmap2.recycle();
|
|
|
|
legacyBitmap2 = null;
|
|
|
|
}
|
2021-11-05 11:06:49 +01:00
|
|
|
try {
|
|
|
|
legacyBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
|
|
|
|
legacyCanvas = new Canvas(legacyBitmap);
|
|
|
|
invalidateLegacy = true;
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (legacyBitmap != null) {
|
|
|
|
legacyBitmap.recycle();
|
|
|
|
legacyBitmap = null;
|
|
|
|
}
|
|
|
|
FileLog.e(e);
|
|
|
|
errorWhileGenerateLegacyBitmap = true;
|
|
|
|
paint2.setXfermode(null);
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 03:27:47 +01:00
|
|
|
public void drawBackground(Canvas canvas) {
|
|
|
|
android.graphics.Rect bounds = getBounds();
|
|
|
|
canvas.save();
|
|
|
|
float tr = patternBitmap != null ? bounds.top : translationY;
|
|
|
|
int bitmapWidth = currentBitmap.getWidth();
|
|
|
|
int bitmapHeight = currentBitmap.getHeight();
|
|
|
|
float w = bounds.width();
|
|
|
|
float h = bounds.height();
|
|
|
|
float maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
float width = bitmapWidth * maxScale;
|
|
|
|
float height = bitmapHeight * maxScale;
|
|
|
|
float x = (w - width) / 2;
|
|
|
|
float y = (h - height) / 2;
|
|
|
|
if (isPreview) {
|
|
|
|
x += bounds.left;
|
|
|
|
y += bounds.top;
|
|
|
|
canvas.clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
}
|
|
|
|
if (intensity < 0) {
|
|
|
|
canvas.drawColor(ColorUtils.setAlphaComponent(Color.BLACK, (int) (alpha * backgroundAlpha)));
|
|
|
|
} else {
|
|
|
|
if (roundRadius != 0) {
|
|
|
|
matrix.reset();
|
|
|
|
matrix.setTranslate(x, y);
|
|
|
|
float scaleW = (currentBitmap.getWidth() / (float) bounds.width());
|
|
|
|
float scaleH = (currentBitmap.getHeight() / (float) bounds.height());
|
|
|
|
float scale = 1.0f / Math.min(scaleW, scaleH);
|
|
|
|
matrix.preScale(scale, scale);
|
|
|
|
bitmapShader.setLocalMatrix(matrix);
|
|
|
|
|
|
|
|
rect.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
int wasAlpha = paint.getAlpha();
|
|
|
|
paint.setAlpha((int) (wasAlpha * backgroundAlpha));
|
|
|
|
canvas.drawRoundRect(rect, roundRadius, roundRadius, paint);
|
|
|
|
paint.setAlpha(wasAlpha);
|
|
|
|
} else {
|
|
|
|
canvas.translate(0, tr);
|
|
|
|
if (gradientDrawable != null) {
|
|
|
|
gradientDrawable.setBounds((int) x, (int) y, (int) (x + width), (int) (y + height));
|
|
|
|
gradientDrawable.setAlpha((int) (255 * backgroundAlpha));
|
|
|
|
gradientDrawable.draw(canvas);
|
|
|
|
} else {
|
|
|
|
rect.set(x, y, x + width, y + height);
|
|
|
|
Paint bitmapPaint = overrideBitmapPaint != null ? overrideBitmapPaint : paint;
|
|
|
|
int wasAlpha = bitmapPaint.getAlpha();
|
|
|
|
bitmapPaint.setAlpha((int) (wasAlpha * backgroundAlpha));
|
|
|
|
canvas.drawBitmap(currentBitmap, null, rect, bitmapPaint);
|
|
|
|
bitmapPaint.setAlpha(wasAlpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
updateAnimation();
|
|
|
|
}
|
|
|
|
public void drawPattern(Canvas canvas) {
|
|
|
|
android.graphics.Rect bounds = getBounds();
|
|
|
|
canvas.save();
|
|
|
|
float tr = patternBitmap != null ? bounds.top : translationY;
|
|
|
|
int bitmapWidth = currentBitmap.getWidth();
|
|
|
|
int bitmapHeight = currentBitmap.getHeight();
|
|
|
|
float w = bounds.width();
|
|
|
|
float h = bounds.height();
|
|
|
|
float maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
float width = bitmapWidth * maxScale;
|
|
|
|
float height = bitmapHeight * maxScale;
|
|
|
|
float x = (w - width) / 2;
|
|
|
|
float y = (h - height) / 2;
|
|
|
|
if (isPreview) {
|
|
|
|
x += bounds.left;
|
|
|
|
y += bounds.top;
|
|
|
|
canvas.clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
}
|
|
|
|
if (intensity < 0) {
|
|
|
|
if (patternBitmap != null) {
|
|
|
|
if (useLegacyBitmap) {
|
|
|
|
if (errorWhileGenerateLegacyBitmap) {
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
rect.set(x, y, x + width, y + height);
|
|
|
|
|
|
|
|
int averageColor = AndroidUtilities.getAverageColor(colors[2], AndroidUtilities.getAverageColor(colors[0], colors[1]));
|
|
|
|
if (colors[3] != 0) {
|
|
|
|
averageColor = AndroidUtilities.getAverageColor(colors[3], averageColor);
|
|
|
|
}
|
|
|
|
if (legacyBitmapColorFilter == null || averageColor != legacyBitmapColor) {
|
|
|
|
legacyBitmapColor = averageColor;
|
|
|
|
legacyBitmapColorFilter = new PorterDuffColorFilter(averageColor, PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
|
|
|
paint2.setColorFilter(legacyBitmapColorFilter);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * alpha * patternAlpha));
|
|
|
|
canvas.translate(0, tr);
|
|
|
|
canvas.drawBitmap(patternBitmap, null, rect, paint2);
|
|
|
|
} else if (legacyBitmap != null) {
|
|
|
|
if (invalidateLegacy) {
|
|
|
|
rect.set(0, 0, legacyBitmap.getWidth(), legacyBitmap.getHeight());
|
|
|
|
int oldAlpha = paint.getAlpha();
|
|
|
|
paint.setAlpha(255);
|
|
|
|
legacyCanvas.drawBitmap(currentBitmap, null, rect, paint);
|
|
|
|
paint.setAlpha(oldAlpha);
|
|
|
|
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
rect.set(x, y, x + width, y + height);
|
|
|
|
|
|
|
|
paint2.setColorFilter(null);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * 255));
|
|
|
|
legacyCanvas.save();
|
|
|
|
legacyCanvas.scale(legacyBitmapScale, legacyBitmapScale);
|
|
|
|
legacyCanvas.drawBitmap(patternBitmap, null, rect, paint2);
|
|
|
|
legacyCanvas.restore();
|
|
|
|
invalidateLegacy = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rect.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
if (legacyBitmap2 != null && posAnimationProgress != 1f) {
|
|
|
|
paint.setAlpha((int) (alpha * patternAlpha * (1f - posAnimationProgress)));
|
|
|
|
canvas.drawBitmap(legacyBitmap2, null, rect, paint);
|
|
|
|
|
|
|
|
paint.setAlpha((int) (alpha * patternAlpha * posAnimationProgress));
|
|
|
|
canvas.drawBitmap(legacyBitmap, null, rect, paint);
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
} else {
|
|
|
|
canvas.drawBitmap(legacyBitmap, null, rect, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (matrix == null) {
|
|
|
|
matrix = new Matrix();
|
|
|
|
}
|
|
|
|
matrix.reset();
|
|
|
|
matrix.setTranslate(x, y + tr);
|
|
|
|
float scaleW = (currentBitmap.getWidth() / (float) bounds.width());
|
|
|
|
float scaleH = (currentBitmap.getHeight() / (float) bounds.height());
|
|
|
|
float scale = 1.0f / Math.min(scaleW, scaleH);
|
|
|
|
matrix.preScale(scale, scale);
|
|
|
|
bitmapShader.setLocalMatrix(matrix);
|
|
|
|
|
|
|
|
matrix.reset();
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
matrix.setTranslate(x, y + tr);
|
|
|
|
matrix.preScale(maxScale, maxScale);
|
|
|
|
gradientShader.setLocalMatrix(matrix);
|
|
|
|
paint2.setColorFilter(null);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * alpha * patternAlpha));
|
|
|
|
rect.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
canvas.drawRoundRect(rect, roundRadius, roundRadius, paint2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (patternBitmap != null) {
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
rect.set(x, y, x + width, y + height);
|
|
|
|
|
|
|
|
paint2.setColorFilter(patternColorFilter);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * alpha * patternAlpha));
|
|
|
|
canvas.drawBitmap(patternBitmap, null, rect, paint2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.restore();
|
|
|
|
|
|
|
|
updateAnimation();
|
|
|
|
}
|
|
|
|
|
2021-06-25 02:43:10 +02:00
|
|
|
@Override
|
|
|
|
public void draw(Canvas canvas) {
|
|
|
|
android.graphics.Rect bounds = getBounds();
|
|
|
|
canvas.save();
|
|
|
|
float tr = patternBitmap != null ? bounds.top : translationY;
|
|
|
|
int bitmapWidth = currentBitmap.getWidth();
|
|
|
|
int bitmapHeight = currentBitmap.getHeight();
|
|
|
|
float w = bounds.width();
|
|
|
|
float h = bounds.height();
|
|
|
|
float maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
float width = bitmapWidth * maxScale;
|
|
|
|
float height = bitmapHeight * maxScale;
|
|
|
|
float x = (w - width) / 2;
|
|
|
|
float y = (h - height) / 2;
|
|
|
|
if (isPreview) {
|
|
|
|
x += bounds.left;
|
|
|
|
y += bounds.top;
|
|
|
|
canvas.clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
}
|
2021-09-20 07:54:41 +02:00
|
|
|
if (intensity < 0) {
|
2022-01-10 03:27:47 +01:00
|
|
|
canvas.drawColor(ColorUtils.setAlphaComponent(Color.BLACK, (int) (alpha * backgroundAlpha)));
|
2021-09-20 07:54:41 +02:00
|
|
|
if (patternBitmap != null) {
|
|
|
|
if (useLegacyBitmap) {
|
2021-11-05 11:06:49 +01:00
|
|
|
if (errorWhileGenerateLegacyBitmap) {
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
rect.set(x, y, x + width, y + height);
|
|
|
|
|
|
|
|
int averageColor = AndroidUtilities.getAverageColor(colors[2], AndroidUtilities.getAverageColor(colors[0], colors[1]));
|
|
|
|
if (colors[3] != 0) {
|
|
|
|
averageColor = AndroidUtilities.getAverageColor(colors[3], averageColor);
|
|
|
|
}
|
|
|
|
if (legacyBitmapColorFilter == null || averageColor != legacyBitmapColor) {
|
|
|
|
legacyBitmapColor = averageColor;
|
|
|
|
legacyBitmapColorFilter = new PorterDuffColorFilter(averageColor, PorterDuff.Mode.SRC_IN);
|
|
|
|
}
|
|
|
|
paint2.setColorFilter(legacyBitmapColorFilter);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * alpha * patternAlpha));
|
|
|
|
canvas.translate(0, tr);
|
|
|
|
canvas.drawBitmap(patternBitmap, null, rect, paint2);
|
|
|
|
} else if (legacyBitmap != null) {
|
2021-09-20 07:54:41 +02:00
|
|
|
if (invalidateLegacy) {
|
|
|
|
rect.set(0, 0, legacyBitmap.getWidth(), legacyBitmap.getHeight());
|
|
|
|
int oldAlpha = paint.getAlpha();
|
|
|
|
paint.setAlpha(255);
|
|
|
|
legacyCanvas.drawBitmap(currentBitmap, null, rect, paint);
|
|
|
|
paint.setAlpha(oldAlpha);
|
|
|
|
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
rect.set(x, y, x + width, y + height);
|
|
|
|
|
|
|
|
paint2.setColorFilter(null);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * 255));
|
2021-11-05 11:06:49 +01:00
|
|
|
legacyCanvas.save();
|
|
|
|
legacyCanvas.scale(legacyBitmapScale, legacyBitmapScale);
|
2021-09-20 07:54:41 +02:00
|
|
|
legacyCanvas.drawBitmap(patternBitmap, null, rect, paint2);
|
2021-11-05 11:06:49 +01:00
|
|
|
legacyCanvas.restore();
|
2021-09-20 07:54:41 +02:00
|
|
|
invalidateLegacy = false;
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
rect.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
if (legacyBitmap2 != null && posAnimationProgress != 1f) {
|
|
|
|
paint.setAlpha((int) (alpha * patternAlpha * (1f - posAnimationProgress)));
|
|
|
|
canvas.drawBitmap(legacyBitmap2, null, rect, paint);
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-09-20 07:54:41 +02:00
|
|
|
paint.setAlpha((int) (alpha * patternAlpha * posAnimationProgress));
|
|
|
|
canvas.drawBitmap(legacyBitmap, null, rect, paint);
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
} else {
|
|
|
|
canvas.drawBitmap(legacyBitmap, null, rect, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-05 11:06:49 +01:00
|
|
|
if (matrix == null) {
|
|
|
|
matrix = new Matrix();
|
|
|
|
}
|
2021-09-20 07:54:41 +02:00
|
|
|
matrix.reset();
|
|
|
|
matrix.setTranslate(x, y + tr);
|
|
|
|
float scaleW = (currentBitmap.getWidth() / (float) bounds.width());
|
|
|
|
float scaleH = (currentBitmap.getHeight() / (float) bounds.height());
|
|
|
|
float scale = 1.0f / Math.min(scaleW, scaleH);
|
|
|
|
matrix.preScale(scale, scale);
|
|
|
|
bitmapShader.setLocalMatrix(matrix);
|
|
|
|
|
|
|
|
matrix.reset();
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
matrix.setTranslate(x, y + tr);
|
|
|
|
matrix.preScale(maxScale, maxScale);
|
|
|
|
gradientShader.setLocalMatrix(matrix);
|
|
|
|
paint2.setColorFilter(null);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * alpha * patternAlpha));
|
|
|
|
rect.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
canvas.drawRoundRect(rect, roundRadius, roundRadius, paint2);
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (roundRadius != 0) {
|
|
|
|
matrix.reset();
|
|
|
|
matrix.setTranslate(x, y);
|
|
|
|
float scaleW = (currentBitmap.getWidth() / (float) bounds.width());
|
|
|
|
float scaleH = (currentBitmap.getHeight() / (float) bounds.height());
|
|
|
|
float scale = 1.0f / Math.min(scaleW, scaleH);
|
|
|
|
matrix.preScale(scale, scale);
|
|
|
|
bitmapShader.setLocalMatrix(matrix);
|
|
|
|
|
|
|
|
rect.set(bounds.left, bounds.top, bounds.right, bounds.bottom);
|
|
|
|
canvas.drawRoundRect(rect, roundRadius, roundRadius, paint);
|
|
|
|
} else {
|
|
|
|
canvas.translate(0, tr);
|
2021-12-07 14:02:02 +01:00
|
|
|
if (gradientDrawable != null) {
|
|
|
|
gradientDrawable.setBounds((int) x, (int) y, (int) (x + width), (int) (y + height));
|
2022-01-10 03:27:47 +01:00
|
|
|
gradientDrawable.setAlpha((int) (255 * backgroundAlpha));
|
2021-12-07 14:02:02 +01:00
|
|
|
gradientDrawable.draw(canvas);
|
|
|
|
} else {
|
|
|
|
rect.set(x, y, x + width, y + height);
|
2022-01-10 03:27:47 +01:00
|
|
|
Paint bitmapPaint = overrideBitmapPaint != null ? overrideBitmapPaint : paint;
|
|
|
|
int wasAlpha = bitmapPaint.getAlpha();
|
|
|
|
bitmapPaint.setAlpha((int) (wasAlpha * backgroundAlpha));
|
|
|
|
canvas.drawBitmap(currentBitmap, null, rect, bitmapPaint);
|
|
|
|
bitmapPaint.setAlpha(wasAlpha);
|
2021-12-07 14:02:02 +01:00
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (patternBitmap != null) {
|
|
|
|
bitmapWidth = patternBitmap.getWidth();
|
|
|
|
bitmapHeight = patternBitmap.getHeight();
|
|
|
|
maxScale = Math.max(w / bitmapWidth, h / bitmapHeight);
|
|
|
|
width = bitmapWidth * maxScale;
|
|
|
|
height = bitmapHeight * maxScale;
|
|
|
|
x = (w - width) / 2;
|
|
|
|
y = (h - height) / 2;
|
|
|
|
rect.set(x, y, x + width, y + height);
|
2021-09-20 07:54:41 +02:00
|
|
|
|
|
|
|
paint2.setColorFilter(patternColorFilter);
|
|
|
|
paint2.setAlpha((int) ((Math.abs(intensity) / 100f) * alpha * patternAlpha));
|
2021-06-25 02:43:10 +02:00
|
|
|
canvas.drawBitmap(patternBitmap, null, rect, paint2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas.restore();
|
|
|
|
|
2021-08-31 21:06:39 +02:00
|
|
|
updateAnimation();
|
|
|
|
}
|
|
|
|
|
2021-12-30 11:52:40 +01:00
|
|
|
public void updateAnimation() {
|
2021-06-25 02:43:10 +02:00
|
|
|
long newTime = SystemClock.elapsedRealtime();
|
|
|
|
long dt = newTime - lastUpdateTime;
|
|
|
|
if (dt > 20) {
|
|
|
|
dt = 17;
|
|
|
|
}
|
|
|
|
lastUpdateTime = newTime;
|
2021-08-31 21:06:39 +02:00
|
|
|
if (dt <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
|
2021-12-30 11:52:40 +01:00
|
|
|
if (isIndeterminateAnimation && posAnimationProgress == 1.0f) {
|
|
|
|
posAnimationProgress = 0f;
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
if (posAnimationProgress < 1.0f) {
|
|
|
|
float progress;
|
2021-12-30 11:52:40 +01:00
|
|
|
boolean isNeedGenerateGradient = postInvalidateParent || rotatingPreview;
|
|
|
|
if (isIndeterminateAnimation) {
|
|
|
|
posAnimationProgress += dt / 12000f;
|
|
|
|
if (posAnimationProgress >= 1.0f) {
|
|
|
|
posAnimationProgress = 0.0f;
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
2021-12-30 11:52:40 +01:00
|
|
|
float progressPerPhase = 1f / 8f;
|
|
|
|
phase = (int) (posAnimationProgress / progressPerPhase);
|
|
|
|
progress = 1f - (posAnimationProgress - phase * progressPerPhase) / progressPerPhase;
|
|
|
|
isNeedGenerateGradient = true;
|
|
|
|
} else {
|
|
|
|
if (rotatingPreview) {
|
|
|
|
int stageBefore;
|
|
|
|
float progressBefore = interpolator.getInterpolation(posAnimationProgress);
|
|
|
|
if (progressBefore <= 0.25f) {
|
|
|
|
stageBefore = 0;
|
|
|
|
} else if (progressBefore <= 0.5f) {
|
|
|
|
stageBefore = 1;
|
|
|
|
} else if (progressBefore <= 0.75f) {
|
|
|
|
stageBefore = 2;
|
|
|
|
} else {
|
|
|
|
stageBefore = 3;
|
|
|
|
}
|
|
|
|
posAnimationProgress += dt / (rotationBack ? 1000.0f : 2000.0f);
|
|
|
|
if (posAnimationProgress > 1.0f) {
|
|
|
|
posAnimationProgress = 1.0f;
|
|
|
|
}
|
|
|
|
progress = interpolator.getInterpolation(posAnimationProgress);
|
|
|
|
if (stageBefore == 0 && progress > 0.25f ||
|
|
|
|
stageBefore == 1 && progress > 0.5f ||
|
|
|
|
stageBefore == 2 && progress > 0.75f) {
|
|
|
|
if (rotationBack) {
|
|
|
|
phase++;
|
|
|
|
if (phase > 7) {
|
|
|
|
phase = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
phase--;
|
|
|
|
if (phase < 0) {
|
|
|
|
phase = 7;
|
|
|
|
}
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
2021-12-30 11:52:40 +01:00
|
|
|
}
|
|
|
|
if (progress <= 0.25f) {
|
|
|
|
progress /= 0.25f;
|
|
|
|
} else if (progress <= 0.5f) {
|
|
|
|
progress = (progress - 0.25f) / 0.25f;
|
|
|
|
} else if (progress <= 0.75f) {
|
|
|
|
progress = (progress - 0.5f) / 0.25f;
|
2021-07-15 16:24:57 +02:00
|
|
|
} else {
|
2021-12-30 11:52:40 +01:00
|
|
|
progress = (progress - 0.75f) / 0.25f;
|
|
|
|
}
|
|
|
|
if (rotationBack) {
|
|
|
|
float prevProgress = progress;
|
|
|
|
progress = 1.0f - progress;
|
|
|
|
if (posAnimationProgress >= 1.0f) {
|
|
|
|
phase++;
|
|
|
|
if (phase > 7) {
|
|
|
|
phase = 0;
|
|
|
|
}
|
|
|
|
progress = 1.0f;
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-30 11:52:40 +01:00
|
|
|
posAnimationProgress += dt / (fastAnimation ? 300.0f : 500.0f);
|
|
|
|
if (posAnimationProgress > 1.0f) {
|
|
|
|
posAnimationProgress = 1.0f;
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
2021-12-30 11:52:40 +01:00
|
|
|
progress = interpolator.getInterpolation(posAnimationProgress);
|
|
|
|
if (rotationBack) {
|
|
|
|
progress = 1.0f - progress;
|
|
|
|
if (posAnimationProgress >= 1.0f) {
|
|
|
|
phase++;
|
|
|
|
if (phase > 7) {
|
|
|
|
phase = 0;
|
|
|
|
}
|
|
|
|
progress = 1.0f;
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|
2021-07-15 16:24:57 +02:00
|
|
|
|
2021-12-30 11:52:40 +01:00
|
|
|
if (isNeedGenerateGradient) {
|
2021-07-15 16:24:57 +02:00
|
|
|
Utilities.generateGradient(currentBitmap, true, phase, progress, currentBitmap.getWidth(), currentBitmap.getHeight(), currentBitmap.getRowBytes(), colors);
|
2021-09-20 07:54:41 +02:00
|
|
|
invalidateLegacy = true;
|
2021-07-15 16:24:57 +02:00
|
|
|
} else {
|
2021-09-20 07:54:41 +02:00
|
|
|
if (useLegacyBitmap && intensity < 0) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (progress != 1f) {
|
|
|
|
float part = 1f / ANIMATION_CACHE_BITMAPS_COUNT;
|
|
|
|
int i = (int) (progress / part);
|
|
|
|
if (i == 0) {
|
|
|
|
gradientCanvas.drawBitmap(gradientFromBitmap, 0, 0, null);
|
|
|
|
} else {
|
|
|
|
gradientCanvas.drawBitmap(gradientToBitmap[i - 1], 0, 0, null);
|
|
|
|
}
|
|
|
|
float alpha = (progress - i * part) / part;
|
|
|
|
paint3.setAlpha((int) (255 * alpha));
|
|
|
|
gradientCanvas.drawBitmap(gradientToBitmap[i], 0, 0, paint3);
|
2021-07-15 16:24:57 +02:00
|
|
|
} else {
|
2021-09-20 07:54:41 +02:00
|
|
|
gradientCanvas.drawBitmap(gradientToBitmap[ANIMATION_CACHE_BITMAPS_COUNT - 1], 0, 0, paint3);
|
2021-07-15 16:24:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
invalidateParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAlpha(int alpha) {
|
2021-09-20 07:54:41 +02:00
|
|
|
this.alpha = alpha;
|
2021-06-25 02:43:10 +02:00
|
|
|
paint.setAlpha(alpha);
|
|
|
|
paint2.setAlpha(alpha);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setColorFilter(ColorFilter colorFilter) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOpacity() {
|
|
|
|
return PixelFormat.TRANSPARENT;
|
|
|
|
}
|
2021-12-07 14:02:02 +01:00
|
|
|
|
|
|
|
public boolean isOneColor() {
|
|
|
|
return colors[0] == colors[1] && colors[0] == colors[2] && colors[0] == colors[3];
|
|
|
|
}
|
2021-12-30 11:52:40 +01:00
|
|
|
|
|
|
|
public void setIndeterminateAnimation(boolean isIndeterminateAnimation) {
|
|
|
|
this.isIndeterminateAnimation = isIndeterminateAnimation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setOverrideBitmapPaint(Paint overrideBitmapPaint) {
|
|
|
|
this.overrideBitmapPaint = overrideBitmapPaint;
|
|
|
|
}
|
2021-06-25 02:43:10 +02:00
|
|
|
}
|