是否可以为seekbar.setmax()设置long值?

时间:2014-04-26 16:54:58

标签: android

我想为seekbar.setmax设置long值,是否可能?

如果不是,还有什么其他选择?

由于

2 个答案:

答案 0 :(得分:1)

如果你在多年之间滑动时不需要精确度(我认为当有超过2000个可能的位置时,要达到你想要的第二个是至关重要的 - {{1}在我看来,选项真的很难)你可以将选择的精度扩展到合理的度量。

这会让你选择使用搜索栏的值,比如天。您可以声明日历并将其设置为开始日期,然后使用日历Integer.MAX_VALUES方法将日期添加到日历中。 您只需计算开始和结束之间的差异,然后您可以通过更改搜索栏进度来决定更改日历的哪个字段。

答案 1 :(得分:0)

由于LongClick与SeekBar的EventMotion(ACTION_MOVE)冲突,我们可以定义LongClick的接口以解决问题。

//支持长按事件垂直摆放的SeekBar

// VerticalSeekBar具有longclick行为

public class VerticalSeekBar extends SeekBar {

    private Drawable mThumb;
    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;
    private LongClickListener longClickListener;
    private long downTime;
    private boolean hasLongTouch = false;//是否已经执行长按操作
    private boolean possibleLongTouch = true;//可能是长按
    private float lastY;
    private float lastX;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1 && !hasLongTouch) {
                hasLongTouch = true;
                if (longClickListener != null) longClickListener.onLongClick();
            }
        }
    };


    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener l) {
        mOnSeekBarChangeListener = l;
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);

        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    void onProgressRefresh(float scale, boolean fromUser) {
        Drawable thumb = mThumb;
        if (thumb != null) {
            setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);
            invalidate();
        }
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);
        }
    }

    private void setThumbPos(int w, Drawable thumb, float scale, int gap) {
        int available = w - getPaddingLeft() - getPaddingRight();

        int thumbWidth = thumb.getIntrinsicWidth();
        int thumbHeight = thumb.getIntrinsicHeight();

        int thumbPos = (int) (scale * available + 0.5f);


        int topBound, bottomBound;
        if (gap == Integer.MIN_VALUE) {
            Rect oldBounds = thumb.getBounds();
            topBound = oldBounds.top;
            bottomBound = oldBounds.bottom;
        } else {
            topBound = gap;
            bottomBound = gap + thumbHeight;
        }
        thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);
    }

    public void setThumb(Drawable thumb) {
        mThumb = thumb;
        super.setThumb(thumb);
    }

    void onStartTrackingTouch() {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onStartTrackingTouch(this);
        }
    }

    void onStopTrackingTouch() {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onStopTrackingTouch(this);
        }
    }

    private void attemptClaimDrag() {
        if (getParent() != null) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float y = event.getY();
        float x = event.getX();

        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setPressed(true);
                onStartTrackingTouch();
                hasLongTouch = false;// 是否已经执行过长按事件
                possibleLongTouch = true;//是否可能为长按模式
                lastY = y;
                lastX = x;
                downTime = System.currentTimeMillis();//记录按下的时间
                if (!handler.hasMessages(1)) {//如果消息队列中已有消息 则不在重新发送
                    handler.sendEmptyMessageDelayed(1, 800);
                }
                break;

            case MotionEvent.ACTION_MOVE:
                attemptClaimDrag();
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));

                onStopTrackingTouch();
                //移动时如果x|y滑动了一段距离 则不可能为长按事件 即将 possibleLongTouch置为false
                if (lastX != 0 && lastY != 0 && (Math.abs(y - lastY) > 5 || Math.abs(x - lastX) > 5)) {
                    possibleLongTouch = false;
                    handler.removeMessages(1);

                }
                lastY = y;
                lastX = x;
                break;
            case MotionEvent.ACTION_UP:
                onStopTrackingTouch();
                setPressed(false);
                lastX = 0;
                lastY = 0;
                handler.removeMessages(1);
                if (System.currentTimeMillis() - downTime > 800 && possibleLongTouch) {
                    if (!hasLongTouch) {//如果已经执行过长按操作  则不需要再次执行
                        hasLongTouch = true;
                        if (longClickListener != null) {
                            longClickListener.onLongClick();
                        }
                    }
                    return true;
                }
                break;

            case MotionEvent.ACTION_CANCEL:
                onStopTrackingTouch();
                setPressed(false);
                handler.removeMessages(1);
                lastX = 0;
                lastY = 0;
                break;
        }
        return true;
    }

    // 定义长按接口
    // define longclick interface
    public interface LongClickListener {
        void onLongClick();
    }

    public void setLongClickListener(LongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }
}