2013-04-14 86 views
0

我在使用Scroller以編程方式滾動ScrollView時遇到問題,因此到目前爲止沒有涉及觸摸手勢。只要傳感器的數據在一定範圍內,我想以特定的速度向下滾動ScrollView。所以基本上我想在數據第一次進入有效範圍時開始滾動,然後不打擾滾動過程,直到數據再次超出範圍。我不想將onSensorChanged直接連接到scrollBy(),因爲它可能無法在其他設備上正常工作。這裏就是我這麼遠:如何使用滾動條自動滾動滾動視圖?

在我的onCreate

tx = new TextView(ShowLyrics.this); 
mainscrollarea = (ScrollView) findViewById (R.id.mainscrollarea); 
scroller = new Scroller(getApplicationContext(), new LinearInterpolator()); 
tx.setScroller(scroller); 

在我onSensorChanged:

if(integratedAngle - scrollTolerance > pointzero){ //this is checking for the data range and works so far 
    if(!scrollingDown){ 
     scrollText("down"); 
     } 
    } 

和滾動文字功能:

void scrollText(String direction){ 

    if(direction.matches("down")){ 
     scrollingUp = false; 
     scrollingDown = true;   
     scroller.forceFinished(true); 
     int currY = mainscrollarea.getScrollY(); 
     int endY = tx.getHeight(); 
     int distance = endY - currY;    
     scroller.startScroll(0, currY, 0, -distance, 5000); 

    } 

    if(direction.matches("up")){ 
    //nothing yet 
    } 
} 

所以現在我已經硬編碼了5秒鐘,但沒有任何反應。 onSensorChanged中Scroller的getCurrY的Log.d()僅吐出0。如果有人能指引我正確的方向,我會感激。

回答

0

我有點像你一樣自動滾動。除了我依靠用戶輸入(當用戶用手指靠近屏幕邊緣時,我開始以特定速度滾動)。

我使用的runnable與scroller的功能相同。

private final DragScroller mDragScroller; 

/** inner class */ 
private class DragScroller implements Runnable { 
    private SCROLL_DIRECTION mDirection; 
    private boolean   mIsFinished = true; 

    DragScroller(Context context) { 
    } 

    void start(SCROLL_DIRECTION direction) { 
     mState = STATE.DRAG_SCROLL; 
     mIsFinished = false; 
     mDirection = direction; 
     post(this); 
    } 

    @Override 
    public void run() { 
     if (mIsFinished) { 
      return; 
     } 
     if (mDirection.equals(SCROLL_DIRECTION.UP)) { 
      // check if the touch is still in the correct area... 
      if (!isOverThreshold(0, mTempY, mDragScrollThreshold)) { 
       scrollTo(0, ensureScrollBoundaries(getScrollY() - mDragScrollSpeed)); 
       post(this); 
      } else { 
       forceFinish(); 
      } 
     } else { 
      // check if the touch is still in the correct area... 
      if (!isOverThreshold(getHeight(), mTempY, mDragScrollThreshold)) { 
       scrollTo(0, ensureScrollBoundaries(getScrollY() + mDragScrollSpeed)); 
       post(this); 
      } else { 
       forceFinish(); 
      } 
     } 
    } 

    public boolean isFinished() { 
     return mIsFinished; 
    } 

    public void forceFinish() { 
     mIsFinished = true; 
    } 
} 

很簡單,就是通過啓動:mDragScroller.start(SCROLL_DIRECTION.UP);,可以通過mDragScroller.forceFinish();

編輯

基於您的評論停下來,你要使用的持續時間的速度。這是有問題的,因爲滾動的結果速度取決於您在給定時間內必須滾動的距離。簡短的數學示例:在1分鐘內滾動600px意味着每秒滾動10px,這並不壞(取決於滾動,文本或圖像的內容......),但如果您靠近邊緣並且只需滾動60px,則由此產生的速度取決於給定的1分鐘的持續時間,意味着非常慢的每秒1px。

考慮到這個例子,你應該根據你的滾動速度而不是總持續時間,而是以每秒像素爲單位。

是的,沒有必要使用Scroller進行編程滾動。只是可以自動調用它的runnable,直到它應該停止並且速度可以根據需要進行調整...

+0

謝謝,我將閱讀有關runnables並現在嘗試它。 – julesmules

+0

如果我正確理解了這一點,那麼您會反覆調用scrollTo,它具有固定的滾動速度。我想實現自動滾動速度的用戶選擇,所以我需要改變這一點,這就是爲什麼我想使用startScroll函數讓我指定滾動的持續時間。另外我看不到你說的Scroller包含在你的課堂中。 – julesmules

+0

哦,呃,我只是用滾輪來反應sorry sorry ......抱歉,我一定記得錯了。變量'mDragScrollSpeed'的值爲10,這很慢,但當然,您可以根據您提到的用戶輸入更改值。我會稍微更新我的答案... – WarrenFaith