2013-04-14 214 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