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

197 lines
6.6 KiB
Java
Raw Normal View History

/*
* This is the source code of Telegram for Android v. 1.3.x.
* 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.
*/
package org.telegram.ui.Components;
import android.graphics.Canvas;
import android.graphics.Paint;
2018-07-30 04:07:02 +02:00
import android.graphics.RectF;
2020-01-23 07:15:40 +01:00
import android.os.SystemClock;
import android.view.MotionEvent;
2020-01-23 07:15:40 +01:00
import android.view.View;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
public class SeekBar {
public interface SeekBarDelegate {
void onSeekBarDrag(float progress);
2019-08-22 01:53:26 +02:00
default void onSeekBarContinuousDrag(float progress) {
}
}
2018-07-30 04:07:02 +02:00
private static Paint paint;
private static int thumbWidth;
2016-04-22 15:49:00 +02:00
private int thumbX = 0;
2019-08-22 01:53:26 +02:00
private int draggingThumbX = 0;
2016-04-22 15:49:00 +02:00
private int thumbDX = 0;
private boolean pressed = false;
2016-04-22 15:49:00 +02:00
private int width;
private int height;
2016-03-06 02:49:31 +01:00
private SeekBarDelegate delegate;
2018-07-30 04:07:02 +02:00
private int backgroundColor;
private int cacheColor;
private int circleColor;
private int progressColor;
private int backgroundSelectedColor;
private RectF rect = new RectF();
private int lineHeight = AndroidUtilities.dp(2);
2016-04-22 15:49:00 +02:00
private boolean selected;
2018-07-30 04:07:02 +02:00
private float bufferedProgress;
2020-01-23 07:15:40 +01:00
private float currentRadius;
private long lastUpdateTime;
private View parentView;
2020-01-23 07:15:40 +01:00
public SeekBar(View parent) {
2018-07-30 04:07:02 +02:00
if (paint == null) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
2020-01-23 07:15:40 +01:00
parentView = parent;
thumbWidth = AndroidUtilities.dp(24);
currentRadius = AndroidUtilities.dp(6);
}
2016-03-06 02:49:31 +01:00
public void setDelegate(SeekBarDelegate seekBarDelegate) {
delegate = seekBarDelegate;
}
public boolean onTouch(int action, float x, float y) {
if (action == MotionEvent.ACTION_DOWN) {
int additionWidth = (height - thumbWidth) / 2;
2020-01-23 07:15:40 +01:00
if (x >= -additionWidth && x <= width + additionWidth && y >= 0 && y <= height) {
if (!(thumbX - additionWidth <= x && x <= thumbX + thumbWidth + additionWidth)) {
thumbX = (int) x - thumbWidth / 2;
if (thumbX < 0) {
thumbX = 0;
} else if (thumbX > width - thumbWidth) {
2020-07-26 10:03:38 +02:00
thumbX = width - thumbWidth;
2020-01-23 07:15:40 +01:00
}
}
pressed = true;
2019-08-22 01:53:26 +02:00
draggingThumbX = thumbX;
2016-10-11 13:57:01 +02:00
thumbDX = (int) (x - thumbX);
return true;
}
} else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
if (pressed) {
2019-08-22 01:53:26 +02:00
thumbX = draggingThumbX;
if (action == MotionEvent.ACTION_UP && delegate != null) {
2016-10-11 13:57:01 +02:00
delegate.onSeekBarDrag((float) thumbX / (float) (width - thumbWidth));
}
pressed = false;
return true;
}
} else if (action == MotionEvent.ACTION_MOVE) {
if (pressed) {
2019-08-22 01:53:26 +02:00
draggingThumbX = (int) (x - thumbDX);
if (draggingThumbX < 0) {
draggingThumbX = 0;
} else if (draggingThumbX > width - thumbWidth) {
draggingThumbX = width - thumbWidth;
}
if (delegate != null) {
delegate.onSeekBarContinuousDrag((float) draggingThumbX / (float) (width - thumbWidth));
}
return true;
}
}
return false;
}
2018-07-30 04:07:02 +02:00
public void setColors(int background, int cache, int progress, int circle, int selected) {
backgroundColor = background;
cacheColor = cache;
circleColor = circle;
progressColor = progress;
backgroundSelectedColor = selected;
2016-04-22 15:49:00 +02:00
}
public void setProgress(float progress) {
2016-10-11 13:57:01 +02:00
thumbX = (int) Math.ceil((width - thumbWidth) * progress);
if (thumbX < 0) {
thumbX = 0;
} else if (thumbX > width - thumbWidth) {
thumbX = width - thumbWidth;
}
}
2018-07-30 04:07:02 +02:00
public void setBufferedProgress(float value) {
bufferedProgress = value;
}
2017-03-31 01:58:05 +02:00
public float getProgress() {
return (float) thumbX / (float) (width - thumbWidth);
}
2019-08-22 01:53:26 +02:00
public int getThumbX() {
return (pressed ? draggingThumbX : thumbX) + thumbWidth / 2;
}
public boolean isDragging() {
return pressed;
}
2016-04-22 15:49:00 +02:00
public void setSelected(boolean value) {
selected = value;
}
public void setSize(int w, int h) {
width = w;
height = h;
}
2019-08-22 01:53:26 +02:00
public int getWidth() {
return width - thumbWidth;
}
2018-07-30 04:07:02 +02:00
public void setLineHeight(int value) {
lineHeight = value;
}
2016-04-22 15:49:00 +02:00
2018-07-30 04:07:02 +02:00
public void draw(Canvas canvas) {
rect.set(thumbWidth / 2, height / 2 - lineHeight / 2, width - thumbWidth / 2, height / 2 + lineHeight / 2);
paint.setColor(selected ? backgroundSelectedColor : backgroundColor);
canvas.drawRoundRect(rect, thumbWidth / 2, thumbWidth / 2, paint);
if (bufferedProgress > 0) {
paint.setColor(selected ? backgroundSelectedColor : cacheColor);
rect.set(thumbWidth / 2, height / 2 - lineHeight / 2, thumbWidth / 2 + bufferedProgress * (width - thumbWidth), height / 2 + lineHeight / 2);
canvas.drawRoundRect(rect, thumbWidth / 2, thumbWidth / 2, paint);
}
2020-01-23 07:15:40 +01:00
rect.set(thumbWidth / 2, height / 2 - lineHeight / 2, thumbWidth / 2 + (pressed ? draggingThumbX : thumbX), height / 2 + lineHeight / 2);
2018-07-30 04:07:02 +02:00
paint.setColor(progressColor);
canvas.drawRoundRect(rect, thumbWidth / 2, thumbWidth / 2, paint);
paint.setColor(circleColor);
2020-01-23 07:15:40 +01:00
int newRad = AndroidUtilities.dp(pressed ? 8 : 6);
if (currentRadius != newRad) {
long newUpdateTime = SystemClock.elapsedRealtime();
long dt = newUpdateTime - lastUpdateTime;
if (dt > 18) {
dt = 16;
}
if (currentRadius < newRad) {
currentRadius += AndroidUtilities.dp(1) * (dt / 60.0f);
if (currentRadius > newRad) {
currentRadius = newRad;
}
} else {
currentRadius -= AndroidUtilities.dp(1) * (dt / 60.0f);
if (currentRadius < newRad) {
currentRadius = newRad;
}
}
if (parentView != null) {
parentView.invalidate();
}
}
canvas.drawCircle((pressed ? draggingThumbX : thumbX) + thumbWidth / 2, height / 2, currentRadius, paint);
}
}