tôi đang tạo ra một seekbar dọc bằng cách sử dụng lớp sauSet thumb tập trung & drawable ép vào tùy chỉnh seekbar lập trình
public class VerticalSeekBar extends SeekBar {
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
@Override
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax()
- (int) (getMax() * event.getY()/getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
Và sau đó tạo ra các seekbar trong hoạt động sử dụng
VerticalSeekBar myZoomBar = new VerticalSeekBar(this);
Drawable drawable = getResources().getDrawable(R.drawable.green_bar);
ClipDrawable clip = new ClipDrawable(drawable, Gravity.LEFT,ClipDrawable.HORIZONTAL);
Drawable drawable2 = getResources().getDrawable(R.drawable.white_bar);
InsetDrawable d1 = new InsetDrawable(drawable2, 5, 5, 5, 5);
myZoomBar.setThumb(getResources().getDrawable(R.drawable.whitecircle));
LayerDrawable mylayer = new LayerDrawable(new Drawable[] { d1, clip });
myZoomBar.setProgressDrawable(mylayer);
myZoomBar.setMax(100);
myZoomBar.setProgress(50);
LinearLayout.LayoutParams zoomBarParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
zoomBarParams.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout zoomLayout = new LinearLayout(this);
zoomLayout.addView(myZoomBar, zoomBarParams);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,Gravity.CENTER);
addContentView(zoomLayout, frameLayoutParams);
Câu hỏi đặt ra là làm sao thế nào để tôi thiết lập ngón tay cái ép và tập trung drawable từ mã?
Tôi đã thấy bản chỉnh sửa của bạn cho câu trả lời của tôi. Đến lúc tôi chấp nhận nó, một người khác đã từ chối nó. Nhân tiện, vì chúng tôi đang thay đổi ngón tay cái có thể vẽ thành press_state trên ACTION_DOWN, nên không nên giữ nguyên cách này cho đến khi gặp ACTION_UP? Nếu ACTION_MOVE được phát hiện, nó sẽ chỉ sau khi ACTION_DOWN đã được gọi (và ngón tay cái có thể vẽ được thiết lập). Tôi giả sử luồng sự kiện trong khi kéo luôn là: ACTION_DOWN >> ACTION_MOVE >> ACTION_UP. Tôi có sai ở đây không? – Vikram
Có, nhưng tôi đang sử dụng lớp tùy chỉnh để vẽ một thanh tìm kiếm dọc như bạn có thể thấy ở trên, vì vậy tôi phải bao gồm điều đó :) –