mirror of https://github.com/NekoX-Dev/NekoX.git
171 lines
6.2 KiB
Java
171 lines
6.2 KiB
Java
package org.telegram.ui.Components;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Paint;
|
|
import android.text.TextPaint;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
|
|
import org.telegram.messenger.AndroidUtilities;
|
|
import org.telegram.ui.ActionBar.Theme;
|
|
|
|
public class SlideChooseView extends View {
|
|
|
|
private Paint paint;
|
|
private TextPaint textPaint;
|
|
|
|
private int circleSize;
|
|
private int gapSize;
|
|
private int sideSide;
|
|
private int lineSize;
|
|
|
|
private boolean moving;
|
|
private boolean startMoving;
|
|
private float startX;
|
|
|
|
private int startMovingPreset;
|
|
|
|
private String[] optionsStr;
|
|
private int[] optionsSizes;
|
|
|
|
int selectedIndex;
|
|
|
|
private Callback callback;
|
|
|
|
public SlideChooseView(Context context) {
|
|
super(context);
|
|
|
|
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
|
|
textPaint.setTextSize(AndroidUtilities.dp(13));
|
|
}
|
|
|
|
public void setCallback(Callback callback) {
|
|
this.callback = callback;
|
|
}
|
|
public void setOptions(int selected, String... options) {
|
|
this.callback = callback;
|
|
this.optionsStr = options;
|
|
selectedIndex = selected;
|
|
optionsSizes = new int[optionsStr.length];
|
|
for (int i = 0; i < optionsStr.length; i++) {
|
|
optionsSizes[i] = (int) Math.ceil(textPaint.measureText(optionsStr[i]));
|
|
}
|
|
requestLayout();
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
float x = event.getX();
|
|
if (event.getAction() == MotionEvent.ACTION_DOWN) {
|
|
getParent().requestDisallowInterceptTouchEvent(true);
|
|
for (int a = 0; a < optionsStr.length; a++) {
|
|
int cx = sideSide + (lineSize + gapSize * 2 + circleSize) * a + circleSize / 2;
|
|
if (x > cx - AndroidUtilities.dp(15) && x < cx + AndroidUtilities.dp(15)) {
|
|
startMoving = a == selectedIndex;
|
|
startX = x;
|
|
startMovingPreset = selectedIndex;
|
|
break;
|
|
}
|
|
}
|
|
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
|
|
if (startMoving) {
|
|
if (Math.abs(startX - x) >= AndroidUtilities.getPixelsInCM(0.5f, true)) {
|
|
moving = true;
|
|
startMoving = false;
|
|
}
|
|
} else if (moving) {
|
|
for (int a = 0; a < optionsStr.length; a++) {
|
|
int cx = sideSide + (lineSize + gapSize * 2 + circleSize) * a + circleSize / 2;
|
|
int diff = lineSize / 2 + circleSize / 2 + gapSize;
|
|
if (x > cx - diff && x < cx + diff) {
|
|
if (selectedIndex != a) {
|
|
setOption(a);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
|
|
if (!moving) {
|
|
for (int a = 0; a < 5; a++) {
|
|
int cx = sideSide + (lineSize + gapSize * 2 + circleSize) * a + circleSize / 2;
|
|
if (x > cx - AndroidUtilities.dp(15) && x < cx + AndroidUtilities.dp(15)) {
|
|
if (selectedIndex != a) {
|
|
setOption(a);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if (selectedIndex != startMovingPreset) {
|
|
setOption(selectedIndex);
|
|
}
|
|
}
|
|
startMoving = false;
|
|
moving = false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void setOption(int index) {
|
|
selectedIndex = index;
|
|
if (callback != null) {
|
|
callback.onOptionSelected(index);
|
|
}
|
|
invalidate();
|
|
}
|
|
|
|
@Override
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(74), MeasureSpec.EXACTLY));
|
|
int width = MeasureSpec.getSize(widthMeasureSpec);
|
|
circleSize = AndroidUtilities.dp(6);
|
|
gapSize = AndroidUtilities.dp(2);
|
|
sideSide = AndroidUtilities.dp(22);
|
|
lineSize = (getMeasuredWidth() - circleSize * optionsStr.length - gapSize * 2 * (optionsStr.length - 1) - sideSide * 2) / (optionsStr.length - 1);
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
textPaint.setColor(Theme.getColor(Theme.key_windowBackgroundWhiteGrayText));
|
|
int cy = getMeasuredHeight() / 2 + AndroidUtilities.dp(11);
|
|
|
|
for (int a = 0; a < optionsStr.length; a++) {
|
|
int cx = sideSide + (lineSize + gapSize * 2 + circleSize) * a + circleSize / 2;
|
|
if (a <= selectedIndex) {
|
|
paint.setColor(Theme.getColor(Theme.key_switchTrackChecked));
|
|
} else {
|
|
paint.setColor(Theme.getColor(Theme.key_switchTrack));
|
|
}
|
|
canvas.drawCircle(cx, cy, a == selectedIndex ? AndroidUtilities.dp(6) : circleSize / 2, paint);
|
|
if (a != 0) {
|
|
int x = cx - circleSize / 2 - gapSize - lineSize;
|
|
int width = lineSize;
|
|
if (a == selectedIndex || a == selectedIndex + 1) {
|
|
width -= AndroidUtilities.dp(3);
|
|
}
|
|
if (a == selectedIndex + 1) {
|
|
x += AndroidUtilities.dp(3);
|
|
}
|
|
canvas.drawRect(x, cy - AndroidUtilities.dp(1), x + width, cy + AndroidUtilities.dp(1), paint);
|
|
}
|
|
int size = optionsSizes[a];
|
|
String text = optionsStr[a];
|
|
|
|
if (a == 0) {
|
|
canvas.drawText(text, AndroidUtilities.dp(22), AndroidUtilities.dp(28), textPaint);
|
|
} else if (a == optionsStr.length - 1) {
|
|
canvas.drawText(text, getMeasuredWidth() - size - AndroidUtilities.dp(22), AndroidUtilities.dp(28), textPaint);
|
|
} else {
|
|
canvas.drawText(text, cx - size / 2, AndroidUtilities.dp(28), textPaint);
|
|
}
|
|
}
|
|
}
|
|
|
|
public interface Callback {
|
|
void onOptionSelected(int index);
|
|
}
|
|
} |