2014-10-08 42 views
1

Scroller類中的getDuration方法獲取滾動的持續時間。在Android中獲取投擲時間

我們可以使用這種方法來獲得fling的持續時間嗎?或者還有其他方法可以做到嗎?

public final int getDuration() 

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

Returns 
The duration of the scroll in milliseconds. 
+0

Uhhh我不確定這一點,但如果我沒有記錯的話,你可以減去兩個事件的開始時間,你會有持續時間。 – 2014-10-08 01:14:09

+0

'getDuration()'可用於「滾動」和「一甩」動作 – pskink 2014-10-08 06:48:43

回答

0

我們可以用這個方法來獲取一扔的時間呢?或者是 有沒有其他方法可以做到這一點?

由於持續時間將在一次投擲開始時計算,所以我們在投擲時使用該方法獲得的持續時間無疑是投擲的持續時間。

順便說一下,這個持續時間取決於一次投擲的初始速度。這裏是滾輪的代碼段,你可以看到它是如何計算的:

/** 
    * 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)); 
    } 

更新:

如何持續時間在一扔開始計算?

「在一個一扔開始」指的時候滾輪的用戶檢測一扔gesture.Then他應該叫適當的參數下面的方法,如果他想用一個滾輪的功能一扔:

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

持續時間在上方法內計算。它首先用velocityX和velocityY計算總速度,然後將速度傳遞給getSplineFlingDuration()方法以獲得投擲持續時間。我將最初粘貼該方法的代碼,並且我認爲這並不難理解。

+0

如何在一次投擲開始時計算持續時間?如果您能詳細說明,我們將不勝感激。 – RMS1 2014-10-08 06:18:55

+0

@ RMS1請參閱我的更新。 – 2014-10-08 07:02:20