NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/CheckBoxSquare.java

182 lines
6.2 KiB
Java
Raw Normal View History

2015-11-26 22:04:02 +01:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2015-11-26 22:04:02 +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-11-26 22:04:02 +01:00
*/
package org.telegram.ui.Components;
import android.animation.ObjectAnimator;
2015-11-26 22:04:02 +01:00
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
2019-05-14 14:08:05 +02:00
import androidx.annotation.Keep;
2015-11-26 22:04:02 +01:00
import android.view.View;
import org.telegram.messenger.AndroidUtilities;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.ActionBar.Theme;
2015-11-26 22:04:02 +01:00
public class CheckBoxSquare extends View {
2017-03-31 01:58:05 +02:00
private RectF rectF;
2015-11-26 22:04:02 +01:00
private Bitmap drawBitmap;
private Canvas drawCanvas;
private float progress;
private ObjectAnimator checkAnimator;
2015-11-26 22:04:02 +01:00
private boolean attachedToWindow;
private boolean isChecked;
private boolean isDisabled;
2017-03-31 01:58:05 +02:00
private boolean isAlert;
2015-11-26 22:04:02 +01:00
private final static float progressBounceDiff = 0.2f;
2020-12-23 08:48:30 +01:00
private String key1;
private String key2;
private String key3;
2017-03-31 01:58:05 +02:00
public CheckBoxSquare(Context context, boolean alert) {
2015-11-26 22:04:02 +01:00
super(context);
2019-01-23 18:03:33 +01:00
if (Theme.checkboxSquare_backgroundPaint == null) {
Theme.createCommonResources(context);
}
2020-12-23 08:48:30 +01:00
key1 = isAlert ? Theme.key_dialogCheckboxSquareUnchecked : Theme.key_checkboxSquareUnchecked;
key2 = isAlert ? Theme.key_dialogCheckboxSquareBackground : Theme.key_checkboxSquareBackground;
key3 = isAlert ? Theme.key_dialogCheckboxSquareCheck : Theme.key_checkboxSquareCheck;
2017-03-31 01:58:05 +02:00
rectF = new RectF();
2015-11-26 22:04:02 +01:00
drawBitmap = Bitmap.createBitmap(AndroidUtilities.dp(18), AndroidUtilities.dp(18), Bitmap.Config.ARGB_4444);
drawCanvas = new Canvas(drawBitmap);
2017-07-08 18:32:04 +02:00
isAlert = alert;
2015-11-26 22:04:02 +01:00
}
2020-12-23 08:48:30 +01:00
public void setColors(String unchecked, String checked, String check) {
key1 = unchecked;
key2 = checked;
key3 = check;
}
2017-03-31 01:58:05 +02:00
@Keep
2015-11-26 22:04:02 +01:00
public void setProgress(float value) {
if (progress == value) {
return;
}
progress = value;
invalidate();
}
2020-03-30 14:00:09 +02:00
@Keep
2015-11-26 22:04:02 +01:00
public float getProgress() {
return progress;
}
private void cancelCheckAnimator() {
if (checkAnimator != null) {
checkAnimator.cancel();
}
}
private void animateToCheckedState(boolean newCheckedState) {
checkAnimator = ObjectAnimator.ofFloat(this, "progress", newCheckedState ? 1 : 0);
2015-11-26 22:04:02 +01:00
checkAnimator.setDuration(300);
checkAnimator.start();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
attachedToWindow = true;
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
attachedToWindow = false;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
public void setChecked(boolean checked, boolean animated) {
if (checked == isChecked) {
return;
}
isChecked = checked;
if (attachedToWindow && animated) {
animateToCheckedState(checked);
} else {
cancelCheckAnimator();
setProgress(checked ? 1.0f : 0.0f);
}
}
public void setDisabled(boolean disabled) {
isDisabled = disabled;
invalidate();
}
public boolean isChecked() {
return isChecked;
}
@Override
protected void onDraw(Canvas canvas) {
if (getVisibility() != VISIBLE) {
return;
}
float checkProgress;
float bounceProgress;
2020-12-23 08:48:30 +01:00
int uncheckedColor = Theme.getColor(key1);
int color = Theme.getColor(key2);
2015-11-26 22:04:02 +01:00
if (progress <= 0.5f) {
bounceProgress = checkProgress = progress / 0.5f;
2017-03-31 01:58:05 +02:00
int rD = (int) ((Color.red(color) - Color.red(uncheckedColor)) * checkProgress);
int gD = (int) ((Color.green(color) - Color.green(uncheckedColor)) * checkProgress);
int bD = (int) ((Color.blue(color) - Color.blue(uncheckedColor)) * checkProgress);
int c = Color.rgb(Color.red(uncheckedColor) + rD, Color.green(uncheckedColor) + gD, Color.blue(uncheckedColor) + bD);
Theme.checkboxSquare_backgroundPaint.setColor(c);
2015-11-26 22:04:02 +01:00
} else {
bounceProgress = 2.0f - progress / 0.5f;
checkProgress = 1.0f;
2017-03-31 01:58:05 +02:00
Theme.checkboxSquare_backgroundPaint.setColor(color);
2015-11-26 22:04:02 +01:00
}
if (isDisabled) {
2017-03-31 01:58:05 +02:00
Theme.checkboxSquare_backgroundPaint.setColor(Theme.getColor(isAlert ? Theme.key_dialogCheckboxSquareDisabled : Theme.key_checkboxSquareDisabled));
2015-11-26 22:04:02 +01:00
}
float bounce = AndroidUtilities.dp(1) * bounceProgress;
rectF.set(bounce, bounce, AndroidUtilities.dp(18) - bounce, AndroidUtilities.dp(18) - bounce);
drawBitmap.eraseColor(0);
2017-03-31 01:58:05 +02:00
drawCanvas.drawRoundRect(rectF, AndroidUtilities.dp(2), AndroidUtilities.dp(2), Theme.checkboxSquare_backgroundPaint);
2015-11-26 22:04:02 +01:00
if (checkProgress != 1) {
float rad = Math.min(AndroidUtilities.dp(7), AndroidUtilities.dp(7) * checkProgress + bounce);
rectF.set(AndroidUtilities.dp(2) + rad, AndroidUtilities.dp(2) + rad, AndroidUtilities.dp(16) - rad, AndroidUtilities.dp(16) - rad);
2017-03-31 01:58:05 +02:00
drawCanvas.drawRect(rectF, Theme.checkboxSquare_eraserPaint);
2015-11-26 22:04:02 +01:00
}
if (progress > 0.5f) {
2020-12-23 08:48:30 +01:00
Theme.checkboxSquare_checkPaint.setColor(Theme.getColor(key3));
2019-12-31 14:08:08 +01:00
int endX = (int) (AndroidUtilities.dp(7) - AndroidUtilities.dp(3) * (1.0f - bounceProgress));
int endY = (int) (AndroidUtilities.dpf2(13) - AndroidUtilities.dp(3) * (1.0f - bounceProgress));
drawCanvas.drawLine(AndroidUtilities.dp(7), (int) AndroidUtilities.dpf2(13), endX, endY, Theme.checkboxSquare_checkPaint);
endX = (int) (AndroidUtilities.dpf2(7) + AndroidUtilities.dp(7) * (1.0f - bounceProgress));
endY = (int) (AndroidUtilities.dpf2(13) - AndroidUtilities.dp(7) * (1.0f - bounceProgress));
drawCanvas.drawLine((int) AndroidUtilities.dpf2(7), (int) AndroidUtilities.dpf2(13), endX, endY, Theme.checkboxSquare_checkPaint);
2015-11-26 22:04:02 +01:00
}
canvas.drawBitmap(drawBitmap, 0, 0, null);
}
}