您好,登錄后才能下訂單哦!
要在SeekBar上實現(xiàn)非線性滑動,可以通過自定義SeekBar的Thumb來實現(xiàn)。以下是一個示例代碼,用于實現(xiàn)非線性滑動的SeekBar:
public class NonLinearSeekBar extends SeekBar {
public NonLinearSeekBar(Context context) {
super(context);
}
public NonLinearSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonLinearSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
float x = event.getX();
float width = getWidth();
float progress = getMax() * x / width;
if (progress < 0.2 * getMax()) {
setProgress((int) (0.2 * getMax()));
} else if (progress > 0.8 * getMax()) {
setProgress((int) (0.8 * getMax()));
} else {
setProgress((int) progress);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
setProgress(getProgress());
}
return true;
}
}
<com.example.myapplication.NonLinearSeekBar
android:id="@+id/nonLinearSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"/>
通過以上代碼,在非線性SeekBar中,當(dāng)Thumb滑動到進(jìn)度條的20%和80%之間時,將會停止滑動。你可以根據(jù)自己的需求調(diào)整這個比例來實現(xiàn)不同的非線性滑動效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。