gpt4 book ai didi

java - 如何检测Android中的触摸速度

转载 作者:搜寻专家 更新时间:2023-11-01 01:52:44 25 4
gpt4 key购买 nike

我在我的应用程序中添加了一个可以通过触摸移动的可绘制对象,并且根据手指移动速度,我想执行不同的操作。

我检查了事件,我只能使用 MotionEvent.ACTION_MOVE 来检测是否有运动。所以我保存最后一次移动的时间戳并得到实际值,我计算移动的增量距离并使用公式

speed=distance/time

但是,速度值显示各种数字,从 0 到大约。 6 但是我用手指是慢还是快都没有关系。我应该修改什么以获得触摸移动的速度?

我使用以下代码:

 @Override
public boolean onTouch(View view, MotionEvent motionEvent)
{

final int X = (int) motionEvent.getRawX();
final int Y = (int) motionEvent.getRawY();
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
oldTimeStamp=System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
speed=0;
textView.setText("speed: " + speed);
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
timeStamp= System.currentTimeMillis();
long diff=timeStamp-oldTimeStamp;
double dist=Math.sqrt(Math.pow(_xDelta,2)+Math.pow(_yDelta,2));
double speed=dist/diff;
textView.setText("speed: " + speed);
oldTimeStamp=timeStamp;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;

}
layoutRoot.invalidate();
return true;
}

最佳答案

@Override
public boolean onTouchEvent(MotionEvent event) {
int index = event.getActionIndex();
int action = event.getActionMasked();
int pointerId = event.getPointerId(index);

switch(action) {
case MotionEvent.ACTION_DOWN:
if(mVelocityTracker == null) {
// Retrieve a new VelocityTracker object to watch the velocity of a motion.
mVelocityTracker = VelocityTracker.obtain();
}
else {
// Reset the velocity tracker back to its initial state.
mVelocityTracker.clear();
}
// Add a user's movement to the tracker.
mVelocityTracker.addMovement(event);
break;
case MotionEvent.ACTION_MOVE:
mVelocityTracker.addMovement(event);
// When you want to determine the velocity, call
// computeCurrentVelocity(). Then call getXVelocity()
// and getYVelocity() to retrieve the velocity for each pointer ID.
mVelocityTracker.computeCurrentVelocity(1000);
// Log velocity of pixels per second
// Best practice to use VelocityTrackerCompat where possible.
Log.d("", "X velocity: " +
VelocityTrackerCompat.getXVelocity(mVelocityTracker,
pointerId));
Log.d("", "Y velocity: " +
VelocityTrackerCompat.getYVelocity(mVelocityTracker,
pointerId));
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// Return a VelocityTracker object back to be re-used by others.
mVelocityTracker.recycle();
break;
}
return true;
}

Tracking Movement

在 View onDetachedFromWindow 上调用 mVelocityTracker.recycle()

关于java - 如何检测Android中的触摸速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20913326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com