Java 数字选择器中的投掷事件
我正在自定义视图中处理垂直数字选择器。现在,正在处理onTouchEvent方法的picker in Action\u up的“fling”事件,但对此一无所知 这是我的代码:Java 数字选择器中的投掷事件,java,android,android-layout,Java,Android,Android Layout,我正在自定义视图中处理垂直数字选择器。现在,正在处理onTouchEvent方法的picker in Action\u up的“fling”事件,但对此一无所知 这是我的代码: protected Paint textPaint; private int textColor; private float text_size; private final int min_size; private int mValue; private int mMaxValue; private int mMi
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();
}
}
受保护的油漆文本油漆;
私有int文本颜色;
私有浮动文本大小;
私有最终整数最小值;
私有价值;
私有值;
私人投资价值;
私人速度追踪器mVelocityTracker;
私人漂浮物;
私人住宅;
私人内部最大流动性;
私人int touchSlop;
私有布尔滚动;
私人超级市场;
私人身高;
公共VerticalNumberPicker(上下文、属性集属性){
超级(上下文,attrs);
TypedArray a=context.getTheme().ActainStyledAttributes(attrs,R.styleable.TunerRuler,0,0);
最小大小=(int)dp2px(getResources(),100);
试一试{
text\u size=a.getDimension(R.styleable.VerticalNumberPicker\u android\u textSize,0.0f);
textColor=a.getColor(R.styleable.VerticalNumberPicker_android_textColor,Color.parseColor(“#ffffffff”);
mValue=a.getInt(R.styleable.VerticalNumberPicker\u nowvalue,120);
mMinValue=a.getInt(R.styleable.VerticalNumberPicker\u minvalue,0);
mMaxValue=a.getInt(R.styleable.VerticalNumberPicker_maxvalue,250);
}最后{
a、 回收();
}
ViewConfiguration=ViewConfiguration.get(上下文);
touchlop=configuration.getScaledTouchSlop();
mmimimumflingvelocity=configuration.getScaledMinimumFlingVelocity();
maximumFlingVelocity=configuration.getScaledMaximumFlingVelocity();
flingScrollerY=新的过卷器(上下文);
itemheight=getHeight()/3;
init(上下文);
}
私有void init(上下文){
Typeface custom_font=Typeface.createFromAsset(context.getAssets(),“font/ralway extrallight.ttf”);
textPaint=新的textPaint();
textPaint.setColor(textColor);
textPaint.setTypeface(自定义字体);
textPaint.setTextSize(文本大小);
textPaint.setAntiAlias(true);
}
公共布尔onTouchEvent(运动事件){
如果(!isEnabled()){
返回false;
}
if(mVelocityTracker==null){
mVelocityTracker=VelocityTracker.get();
}
mVelocityTracker.addMovement(事件);
int action=event.getActionMasked();
开关(动作){
case MotionEvent.ACTION\u移动:
float currentMoveY=event.getY();
int deltaMoveY=(int)(lastDownEventY-currentMoveY);
if(滚动的(Math.abs(deltaMoveY)>touchlop)){
如果(!滚动){
deltaMoveY=0;
//按editem=-1;
scrollingY=true;
getParent().RequestDisallowWinterCeptTouchEvent(true);
}
lastDownEventY=当前移动;
如果(deltaMoveY>0){
如果(mValue+1>mMaxValue)
mValue=mMinValue;
其他的
mValue+=1;
}
else if(deltaMoveY<0){
if(mValue-1mmimimumflingvelocity){
//flingY(初始速度y);
}else if(mValue!=-1){
float positionY=event.getY();
如果(!滚动){
int itemPos=屏幕上的获取位置(位置Y);
int relativePos=itemPos-1;
if(relativePos==0){
//选择项();
}否则{
smoothScrollBy(相对论);
}
}else if(滚动){
//完成滚动();
scrollingY=false;
}
}
mVelocityTracker.recycle();
mVelocityTracker=null;
Log.i(“up”,“onTouchEvent:”);
}
返回true;
}
受保护的void onDraw(画布){
float textHeight=textPaint.descent()+textPaint.ascent();
canvas.drawText(String.valueOf(mValue),(getWidth()-textPaint.measureText(String.valueOf(mValue)))/2.0f,(getHeight()-textHeight)/2.0f,textPaint);
}
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
//Paint.FontMetrics fm=textPaint.getFontMetrics();
//浮动文本高度=fm.top+fm.bottom+fm.ascend;
int width=(int)textPaint.measureText(“444”);
设置测量尺寸(宽度、高度测量等级);
}
屏幕上的专用int GetPositionOn(浮点x){
返回(整数)(x/项目高度);
}
私有空平滑滚动比(int i){
int deltaMoveY=项目高度*i;
deltaMoveY=GetRelativeInBond(deltaMoveY);
//previousScrollerY=Integer.MIN\u值;
startScroll(0,getScrollY(),0,deltaMoveY);
使无效();
}
私有整数GetRelativeInBond(整数y){
int scrollY=getScrollY();
返回getInBoundsY(scrollY+y)-滚动;
}
私有int getInBoundsY(int x){
if(x<0){
x=0;
}如果(x>((i