NekoX/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/BackDrawable.java

163 lines
5.3 KiB
Java
Raw Normal View History

2015-10-29 18:10:07 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-10-29 18:10:07 +01:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2015-10-29 18:10:07 +01:00
*/
package org.telegram.ui.ActionBar;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.animation.DecelerateInterpolator;
import org.telegram.messenger.AndroidUtilities;
public class BackDrawable extends Drawable {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
2019-05-14 14:08:05 +02:00
private boolean reverseAngle;
2015-10-29 18:10:07 +01:00
private long lastFrameTime;
private boolean animationInProgress;
private float finalRotation;
private float currentRotation;
private int currentAnimationTime;
private boolean alwaysClose;
private DecelerateInterpolator interpolator = new DecelerateInterpolator();
2017-03-31 01:58:05 +02:00
private int color = 0xffffffff;
private int rotatedColor = 0xff757575;
private float animationTime = 300.0f;
private boolean rotated = true;
2018-07-30 04:07:02 +02:00
private int arrowRotation;
2015-10-29 18:10:07 +01:00
public BackDrawable(boolean close) {
super();
paint.setStrokeWidth(AndroidUtilities.dp(2));
alwaysClose = close;
}
2017-03-31 01:58:05 +02:00
public void setColor(int value) {
color = value;
invalidateSelf();
}
public void setRotatedColor(int value) {
rotatedColor = value;
invalidateSelf();
}
2018-07-30 04:07:02 +02:00
public void setArrowRotation(int angle) {
arrowRotation = angle;
invalidateSelf();
}
2015-10-29 18:10:07 +01:00
public void setRotation(float rotation, boolean animated) {
lastFrameTime = 0;
if (currentRotation == 1) {
reverseAngle = true;
} else if (currentRotation == 0) {
reverseAngle = false;
}
lastFrameTime = 0;
if (animated) {
if (currentRotation < rotation) {
2017-03-31 01:58:05 +02:00
currentAnimationTime = (int) (currentRotation * animationTime);
2015-10-29 18:10:07 +01:00
} else {
2017-03-31 01:58:05 +02:00
currentAnimationTime = (int) ((1.0f - currentRotation) * animationTime);
2015-10-29 18:10:07 +01:00
}
lastFrameTime = System.currentTimeMillis();
finalRotation = rotation;
} else {
finalRotation = currentRotation = rotation;
}
invalidateSelf();
}
2017-03-31 01:58:05 +02:00
public void setAnimationTime(float value) {
animationTime = value;
}
public void setRotated(boolean value) {
rotated = value;
}
2015-10-29 18:10:07 +01:00
@Override
public void draw(Canvas canvas) {
if (currentRotation != finalRotation) {
if (lastFrameTime != 0) {
long dt = System.currentTimeMillis() - lastFrameTime;
currentAnimationTime += dt;
2017-03-31 01:58:05 +02:00
if (currentAnimationTime >= animationTime) {
2015-10-29 18:10:07 +01:00
currentRotation = finalRotation;
} else {
if (currentRotation < finalRotation) {
2017-03-31 01:58:05 +02:00
currentRotation = interpolator.getInterpolation(currentAnimationTime / animationTime) * finalRotation;
2015-10-29 18:10:07 +01:00
} else {
2017-03-31 01:58:05 +02:00
currentRotation = 1.0f - interpolator.getInterpolation(currentAnimationTime / animationTime);
2015-10-29 18:10:07 +01:00
}
}
}
lastFrameTime = System.currentTimeMillis();
invalidateSelf();
}
2017-03-31 01:58:05 +02:00
int rD = rotated ? (int) ((Color.red(rotatedColor) - Color.red(color)) * currentRotation) : 0;
int rG = rotated ? (int) ((Color.green(rotatedColor) - Color.green(color)) * currentRotation) : 0;
int rB = rotated ? (int) ((Color.blue(rotatedColor) - Color.blue(color)) * currentRotation) : 0;
int c = Color.rgb(Color.red(color) + rD, Color.green(color) + rG, Color.blue(color) + rB);
2015-10-29 18:10:07 +01:00
paint.setColor(c);
canvas.save();
canvas.translate(getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
2018-07-30 04:07:02 +02:00
if (arrowRotation != 0) {
canvas.rotate(arrowRotation);
}
2015-10-29 18:10:07 +01:00
float rotation = currentRotation;
if (!alwaysClose) {
canvas.rotate(currentRotation * (reverseAngle ? -225 : 135));
} else {
canvas.rotate(135 + currentRotation * (reverseAngle ? -180 : 180));
rotation = 1.0f;
}
canvas.drawLine(-AndroidUtilities.dp(7) - AndroidUtilities.dp(1) * rotation, 0, AndroidUtilities.dp(8), 0, paint);
float startYDiff = -AndroidUtilities.dp(0.5f);
float endYDiff = AndroidUtilities.dp(7) + AndroidUtilities.dp(1) * rotation;
float startXDiff = -AndroidUtilities.dp(7.0f) + AndroidUtilities.dp(7.0f) * rotation;
float endXDiff = AndroidUtilities.dp(0.5f) - AndroidUtilities.dp(0.5f) * rotation;
canvas.drawLine(startXDiff, -startYDiff, endXDiff, -endYDiff, paint);
canvas.drawLine(startXDiff, startYDiff, endXDiff, endYDiff, paint);
canvas.restore();
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return PixelFormat.TRANSPARENT;
}
@Override
public int getIntrinsicWidth() {
return AndroidUtilities.dp(24);
}
@Override
public int getIntrinsicHeight() {
return AndroidUtilities.dp(24);
}
}