Tôi đang tạo chế độ xem tùy chỉnh, đây là một loại chế độ xem thanh trượt arc.I có thể vẽ nhiều hoặc ít vòng cung dựa trên nơi người dùng chạm (trên x trục) bằng cách tính toán quét, tôi làm điều này bằng cách đầu tiên tính toán percetage nơi người dùng chạm dọc theo trục x..0% sẽ là tất cả các cách bên trái và 100% sẽ là tất cả các cách bên phải.Custom View drawArc, phát hiện cảm ứng của người dùng trên đường dẫn vẽ của arc
Tôi muốn tiến thêm một bước nữa, thay vì vẽ vòng cung dựa trên tọa độ x mà người dùng nhấn, tôi muốn làm cho nó di chuyển chỉ khi người dùng chạm vào đường dẫn vẽ vòng cung thực tế. . Tôi vẫn còn mới để xem tùy chỉnh và toán học của tôi còn hạn chế nhưng nếu tôi nhận được một số lời khuyên tôi sẽ nhờ biết ơn
class ArcProgress extends View {
Context cx;
float width;
float height;
float center_x, center_y;
final RectF oval = new RectF();
final RectF touchArea = new RectF();
float sweep = 0;
float left, right;
int percent = 0;
public ArcProgress(Context context) {
super(context);
cx = context;
}
public int getPercentage() {
return percent;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setBackgroundColor(0xfff0ebde);
width = (float) getWidth();
height = (float) getHeight();
float radius;
if (width > height) {
radius = height/3;
} else {
radius = width/3;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xffd2c8b6);
paint.setStrokeWidth(35);
paint.setStyle(Paint.Style.STROKE);
center_x = width/2;
center_y = height/2;
left = center_x - radius;
float top = center_y - radius;
right = center_x + radius;
float bottom = center_y + radius;
oval.set(left, top, right, bottom);
//this is the background arc, it remains constant
canvas.drawArc(oval, 180, 180, false, paint);
paint.setStrokeWidth(10);
paint.setColor(0xffe0524d);
//this is the red arc whichhas its sweep argument manipulated by on touch
canvas.drawArc(oval, 180, sweep, false, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
float xPosition = event.getX();
float yPosition = event.getY();
if (oval.contains(xPosition, yPosition)) {
float x = xPosition - left;
float s = x * 100;
float b = s/oval.width();
percent = Math.round(b);
sweep = (180/100.0f) * (float) percent;
invalidate();
} else {
if (xPosition < left) {
percent = 0;
sweep = (180/100.0f) * (float) percent;
invalidate();
}
if (xPosition > right) {
percent = 100;
sweep = (180/100.0f) * (float) percent;
invalidate();
}
}
}
return true;
}
}
tôi ước gì tôi chọn 2 câu trả lời hàng đầu, tôi sẽ bấm bạn cảm ơn một triệu – brux
đừng lo lắng, vui lòng trợ giúp :) – Plato
tôi thay đổi suy nghĩ của mình và làm chính xác điều này. mã hoạt động, bạn có bị IRC không? – brux