为什么
现在Android提供了fling(velocity)函数,但如果需要fling(distance),fling滑动一段距离呢?
如果需要知道fling(velocity)花了多少毫秒呢?
just do it
根据Android源代码,我们可以通过高中数学来实现这些功能。直接上代码
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mMaximumVelocity = ViewConfiguration.get(context)
.getScaledMaximumFlingVelocity();
mMinimumVelocity = ViewConfiguration.get(context)
.getScaledMinimumFlingVelocity();
float ppi = context.getResources().getDisplayMetrics().density * 160.0f;
mPhysicalCoeff = SensorManager.GRAVITY_EARTH // g (m/s^2)
* 39.37f // inch/meter
* ppi
* 0.84f;
// double distanceEs = getSplineFlingDistance(1000);
// Log.e("getSplineFlingDistance", "getSplineFlingDistance " + distanceEs);
// int velocityRs = getVelocityByDistance(116.64459);
// Log.e("getVelocityByDistance", "getVelocityByDistance " + velocityRs);
}
private static final float INFLEXION = 0.35f; // Tension lines cross at (INFLEXION, 1)
// Fling friction
private static float mFlingFriction = ViewConfiguration.getScrollFriction();
private static float mPhysicalCoeff;
private static float DECELERATION_RATE = (float) (Math.log(0.78) / Math.log(0.9));
private double getSplineDeceleration(int velocity) {
return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff));
}
private static double getSplineDecelerationByDistance(double distance) {
final double decelMinusOne = DECELERATION_RATE - 1.0;
return decelMinusOne * (Math.log(distance / (mFlingFriction * mPhysicalCoeff))) / DECELERATION_RATE;
}
//通过初始速度获取最终滑动距离
private double getSplineFlingDistance(int velocity) {
final double l = getSplineDeceleration(velocity);
final double decelMinusOne = DECELERATION_RATE - 1.0;
return mFlingFriction * mPhysicalCoeff * Math.exp(DECELERATION_RATE / decelMinusOne * l);
}
//通过需要滑动的距离获取初始速度
public static int getVelocityByDistance(double distance) {
final double l = getSplineDecelerationByDistance(distance);
int velocity = (int) (Math.exp(l) * mFlingFriction * mPhysicalCoeff / INFLEXION);
return Math.abs(velocity);
}
//获取滑动的时间
/* Returns the duration, expressed in milliseconds */
private int getSplineFlingDuration(int velocity) {
final double l = getSplineDeceleration(velocity);
final double decelMinusOne = DECELERATION_RATE - 1.0;
return (int) (1000.0 * Math.exp(l / decelMinusOne));
}
使用实例在该项目中哦 https://github.com/smallnew/PullFling
来源:CSDN
作者:smallnewQaQ
链接:https://blog.csdn.net/tyro_smallnew/article/details/54613947