数字选择器中的事件

时间:2016-06-06 14:28:41

标签: java android android-layout

我正在自定义视图中使用垂直数字选择器。现在,正在努力工作" fling" onTouchEvent方法的Action_up中的选择器事件但不知道它。

这是我的代码:

protected Paint textPaint;
private int textColor;
private float text_size;
private final int min_size;
private int mValue;
private int mMaxValue;
private int mMinValue;
private VelocityTracker mVelocityTracker;
private float lastDownEventY;
private int mMinimumFlingVelocity;
private int maximumFlingVelocity;
private int touchSlop;
private boolean scrollingY;
private OverScroller flingScrollerY;
private int itemheight;

public VerticalNumberPicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TunerRuler, 0, 0);

    min_size = (int) dp2px(getResources(), 100);

    try {
        text_size = a.getDimension(R.styleable.VerticalNumberPicker_android_textSize, 0.0f);
        textColor = a.getColor(R.styleable.VerticalNumberPicker_android_textColor, Color.parseColor("#FFFFFF"));
        mValue = a.getInt(R.styleable.VerticalNumberPicker_nowvalue, 120);
        mMinValue = a.getInt(R.styleable.VerticalNumberPicker_minvalue, 0);
        mMaxValue = a.getInt(R.styleable.VerticalNumberPicker_maxvalue, 250);

    } finally {
        a.recycle();
    }

    ViewConfiguration configuration = ViewConfiguration.get(context);
    touchSlop = configuration.getScaledTouchSlop();
    mMinimumFlingVelocity = configuration.getScaledMinimumFlingVelocity();
    maximumFlingVelocity = configuration.getScaledMaximumFlingVelocity();
    flingScrollerY = new OverScroller(context);
    itemheight = getHeight() / 3;

    init(context);
}

private void init(Context context) {
    Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/Raleway-ExtraLight.ttf");
    textPaint = new TextPaint();
    textPaint.setColor(textColor);
    textPaint.setTypeface(custom_font);
    textPaint.setTextSize(text_size);
    textPaint.setAntiAlias(true);

}

public boolean onTouchEvent(MotionEvent event) {

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

    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);

    int action = event.getActionMasked();
    switch (action) {
        case MotionEvent.ACTION_MOVE:
            float currentMoveY = event.getY();
            int deltaMoveY = (int) (lastDownEventY - currentMoveY);

            if(scrollingY || (Math.abs(deltaMoveY) > touchSlop)) {
                if(!scrollingY) {
                    deltaMoveY = 0;
                    //pressedItem = -1;
                    scrollingY = true;
                    getParent().requestDisallowInterceptTouchEvent(true);
                }

                lastDownEventY = currentMoveY;

                if(deltaMoveY > 0) {
                    if(mValue + 1 > mMaxValue)
                        mValue = mMinValue;
                    else
                        mValue += 1;
                }
                else if(deltaMoveY < 0){
                    if(mValue - 1 < mMinValue)
                        mValue = mMaxValue;
                    else
                        mValue -= 1;
                }

                invalidate();
                Log.i("move", "onTouchEvent: " + lastDownEventY + " "  + deltaMoveY);
            }
            break;
        case MotionEvent.ACTION_DOWN:
            if(!flingScrollerY.isFinished()) {
                flingScrollerY.forceFinished(true);
            } else {
                scrollingY = false;
            }

            lastDownEventY = event.getX();
            invalidate();
            Log.i("down", "onTouchEvent: ");
            break;
        case MotionEvent.ACTION_UP:
            VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, maximumFlingVelocity);
            int initialVelocityY = (int) velocityTracker.getYVelocity();

            if(scrollingY && Math.abs(initialVelocityY) > mMinimumFlingVelocity) {
                //flingY(initialVelocityY);
            } else if (mValue != -1) {
                float positionY = event.getY();
                if(!scrollingY) {

                    int itemPos = getPositionOnScreen(positionY);
                    int relativePos = itemPos - 1;

                    if (relativePos == 0) {
                        //selectItem();
                    } else {
                        smoothScrollBy(relativePos);
                    }

                } else if(scrollingY) {
                    //finishScrolling();
                    scrollingY = false;
                }
            }

            mVelocityTracker.recycle();
            mVelocityTracker = null;
            Log.i("up", "onTouchEvent: ");
    }

    return true;
}

protected void onDraw(Canvas canvas) {
    float textHeight = textPaint.descent() + textPaint.ascent();
    canvas.drawText(String.valueOf(mValue), (getWidth() - textPaint.measureText(String.valueOf(mValue))) / 2.0f, (getHeight() - textHeight) / 2.0f, textPaint);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //Paint.FontMetrics fm = textPaint.getFontMetrics();
    //float textHeight = fm.top + fm.bottom + fm.ascent;
    int width = (int) textPaint.measureText("444");
    setMeasuredDimension(width, heightMeasureSpec);
}

private int getPositionOnScreen(float x) {
    return (int) (x / itemheight);
}

private void smoothScrollBy(int i) {
    int deltaMoveY = itemheight * i;
    deltaMoveY = getRelativeInBound(deltaMoveY);

    //previousScrollerY = Integer.MIN_VALUE;
    flingScrollerY.startScroll(0, getScrollY(), 0, deltaMoveY);
    invalidate();
}
private int getRelativeInBound(int y) {
    int scrollY = getScrollY();
    return getInBoundsY(scrollY + y) - scrollY;
}

private int getInBoundsY(int x) {

    if(x < 0) {
        x = 0;
    } else if(x > ((itemheight) * (mMaxValue-mMinValue))) {
        x = ((itemheight) * (mMaxValue-mMinValue));
    }
    return x;
}

private int measure(int measureSpec){
    int result;
    int mode = MeasureSpec.getMode(measureSpec);
    int size = MeasureSpec.getSize(measureSpec);
    if(mode == MeasureSpec.EXACTLY){
        result = size;
    }else{
        result = min_size;
        if(mode == MeasureSpec.AT_MOST){
            result = Math.min(result, size);
        }
    }
    return result;
}

public static float dp2px(Resources resources, float dp) {
    final float scale = resources.getDisplayMetrics().density;
    return  dp * scale + 0.5f;
}

public int getValue() {
    return mValue;
}

public void setValue(int value){
    //this.mValue = value;
    if(value > mMaxValue)
        mValue = mMinValue;
    else if(value < mMinValue)
        mValue = mMaxValue;
    else
        mValue = value;

    invalidate();
}

public int getMinValue() {
    return mMinValue;
}

public void setMinValue(int minValue) {
    if (mMinValue == minValue) {
        return;
    }
    if (minValue < 0) {
        throw new IllegalArgumentException("minValue must be >= 0");
    }
    mMinValue = minValue;
    if (mMinValue > mValue) {
        mValue = mMinValue;
    }
    invalidate();
}

public int getMaxValue() {
    return mMaxValue;
}

public void setMaxValue(int maxValue) {
    if (mMaxValue == maxValue) {
        return;
    }
    if (maxValue < 0) {
        throw new IllegalArgumentException("maxValue must be >= 0");
    }
    mMaxValue = maxValue;
    if (mMaxValue < mValue) {
        mValue = mMaxValue;
    }
    invalidate();
}

}

任何人都可以给我一些提示或示例吗?

0 个答案:

没有答案