2014-07-20 01:31:49 +02:00
|
|
|
/*
|
2019-01-23 18:03:33 +01:00
|
|
|
* This is the source code of Telegram for Android v. 5.x.x.
|
2014-07-20 01:31:49 +02: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.
|
2014-07-20 01:31:49 +02:00
|
|
|
*/
|
|
|
|
|
2014-12-01 18:56:31 +01:00
|
|
|
package org.telegram.ui.Components;
|
2014-07-20 01:31:49 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
2014-07-20 01:31:49 +02:00
|
|
|
|
|
|
|
public class VideoSeekBarView extends View {
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
private Paint paint = new Paint();
|
|
|
|
private Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
private int thumbWidth = AndroidUtilities.dp(12);
|
|
|
|
private int thumbHeight = AndroidUtilities.dp(12);
|
2014-07-20 01:31:49 +02:00
|
|
|
private int thumbDX = 0;
|
|
|
|
private float progress = 0;
|
|
|
|
private boolean pressed = false;
|
2016-10-11 13:57:01 +02:00
|
|
|
private SeekBarDelegate delegate;
|
2014-07-20 01:31:49 +02:00
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
public interface SeekBarDelegate {
|
|
|
|
void onSeekBarDrag(float progress);
|
2014-07-20 01:31:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public VideoSeekBarView(Context context) {
|
|
|
|
super(context);
|
2016-10-11 13:57:01 +02:00
|
|
|
paint.setColor(0xff5c5c5c);
|
|
|
|
paint2.setColor(0xffffffff);
|
2014-07-20 01:31:49 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 13:57:01 +02:00
|
|
|
public void setDelegate(SeekBarDelegate seekBarDelegate) {
|
|
|
|
delegate = seekBarDelegate;
|
2014-07-20 01:31:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
if (event == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
float x = event.getX();
|
|
|
|
float y = event.getY();
|
|
|
|
float thumbX = (int)((getMeasuredWidth() - thumbWidth) * progress);
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
|
|
|
int additionWidth = (getMeasuredHeight() - thumbWidth) / 2;
|
|
|
|
if (thumbX - additionWidth <= x && x <= thumbX + thumbWidth + additionWidth && y >= 0 && y <= getMeasuredHeight()) {
|
|
|
|
pressed = true;
|
|
|
|
thumbDX = (int)(x - thumbX);
|
|
|
|
getParent().requestDisallowInterceptTouchEvent(true);
|
|
|
|
invalidate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
|
|
|
|
if (pressed) {
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_UP && delegate != null) {
|
|
|
|
delegate.onSeekBarDrag(thumbX / (float)(getMeasuredWidth() - thumbWidth));
|
|
|
|
}
|
|
|
|
pressed = false;
|
|
|
|
invalidate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
|
|
|
if (pressed) {
|
|
|
|
thumbX = (int)(x - thumbDX);
|
|
|
|
if (thumbX < 0) {
|
|
|
|
thumbX = 0;
|
|
|
|
} else if (thumbX > getMeasuredWidth() - thumbWidth) {
|
|
|
|
thumbX = getMeasuredWidth() - thumbWidth;
|
|
|
|
}
|
|
|
|
progress = thumbX / (getMeasuredWidth() - thumbWidth);
|
|
|
|
invalidate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setProgress(float progress) {
|
|
|
|
if (progress < 0) {
|
|
|
|
progress = 0;
|
|
|
|
} else if (progress > 1) {
|
|
|
|
progress = 1;
|
|
|
|
}
|
|
|
|
this.progress = progress;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getProgress() {
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
|
int y = (getMeasuredHeight() - thumbHeight) / 2;
|
|
|
|
int thumbX = (int)((getMeasuredWidth() - thumbWidth) * progress);
|
2016-10-11 13:57:01 +02:00
|
|
|
canvas.drawRect(thumbWidth / 2, getMeasuredHeight() / 2 - AndroidUtilities.dp(1), getMeasuredWidth() - thumbWidth / 2, getMeasuredHeight() / 2 + AndroidUtilities.dp(1), paint);
|
|
|
|
canvas.drawCircle(thumbX + thumbWidth / 2, y + thumbHeight / 2, thumbWidth / 2, paint2);
|
2014-07-20 01:31:49 +02:00
|
|
|
}
|
|
|
|
}
|