android - 获取Android中fling的时长

标签 android

Scroller类中的getDuration方法获取滚动的时长。

我们是否也可以使用这种方法来获取 fling 的持续时间?或者还有其他方法吗?

public final int getDuration ()

Returns how long the scroll event will take, in milliseconds.

Returns
The duration of the scroll in milliseconds.

最佳答案

Can we use this method to get the duration of fling as well? Or is there any other way to do that?

由于持续时间将在一次 throw 开始时计算,因此我们在 throw 期间使用该方法获得的持续时间无疑是 throw 的持续时间。

顺便说一句,这个持续时间取决于 throw 的初始速度。这是 Scroller 的代码 fragment ,您可以看到它是如何计算的:

    /**
     * Returns how long the scroll event will take, in milliseconds.
     * 
     * @return The duration of the scroll in milliseconds.
     */
    public final int getDuration() {
        return mDuration;
    }

   // Code in the fling() method which will be called based on a fling gesture
   mDuration = getSplineFlingDuration(velocity);

    // The essential method.
    private int getSplineFlingDuration(float velocity) {
        final double l = getSplineDeceleration(velocity);
        final double decelMinusOne = DECELERATION_RATE - 1.0;
        return (int) (1000.0 * Math.exp(l / decelMinusOne));
    }
    private double getSplineDeceleration(float velocity) {
        return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * mPhysicalCoeff));
    }

更新:

How can the duration be calculated at the start of a fling?

“在fling开始时”是指当Scroller的用户检测到fling手势时。如果他想使用Scroller的fling功能,他应该调用以下方法并传递适当的参数:

  public void fling(int startX, int startY, int velocityX, int velocityY,
            int minX, int maxX, int minY, int maxY) 

持续时间是在 upper 方法中计算的。它首先用 velocityX 和 velocityY 计算总速度,然后将速度传递给 getSplineFlingDuration() 方法来获得飞行持续时间。我最初粘贴了该方法的代码,我认为你不难理解。

关于android - 获取Android中fling的时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26247609/

相关文章:

android - 如何阻止 ICS 用户下载我的应用程序

android - 如何使用fragments在一个屏幕上打开两个webview

android - 在eclipse中更新到最新的android sdk后出现错误

android 在单击图像按钮时使用动画交换两个 View

Android Studio 3.0 和应用卡住

android - recyclerview 同时与其他 View 滚动

android - 从字符串android中拆分日期时间

android - 新的 'Cloud Firestore' 是否可以查询基于位置的距离?

Android - 运行进程和缓存后台进程的区别

Android 如何在这样的 webview 中显示链接?