2014-06-05 115 views
0

我已經嘗試了這幾次,但我無法解決它。 我有一個ScrollView有幾個視圖,它們被ObjectAnimator移動。當您按下按鈕時,視圖會在給定時間內垂直移動。 我玩PLAY和PAUSE,但是當我用手指移動滾動條時,我無法知道滾動條已移動的像素數。當我移動滾動視圖時移動的像素數

我想用我的手指移動滾動,脈衝播放,我希望滾動開始滾動而不是從頭開始。 我試過這個:

view.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       //Choose which motion action has been performed 
       switch(event.getAction()) 
       { 
       case MotionEvent.ACTION_DOWN: 
        //Get X, Y coordinates from the ImageView 
        X = (int) event.getX(); 
        Y = (int) event.getY(); 
        startX = event.getRawX(); 
        startY = event.getRawY(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        Z = (int) event.getX(); 
        S = (int) event.getY(); 
        K = (int) event.getRawX(); 
        P = (int) event.getRawY(); 
        V = (int) (event.getRawY() - startY); 
        break; 
       case MotionEvent.ACTION_UP: 
        A = (int) event.getX(); 
        E = (int) event.getY(); 
        break; 
       } 
       return true; 
      } 

});

當我舉起手指時它沒有給出座標。如何知道移動滾動條時移動的像素數量?這是一個Android應用程序。 在此先感謝!

+0

您已經採取了正確的方法,但所有這些變量是什麼? 'S','K','P','V','A','E','Z','X','Y'?你爲什麼需要它們?您只需計算x和y座標之間的差異即可獲得手指的移動量。 –

回答

2

您只需計算每次獲取ACTION_MOVE事件時x和y座標之間的差異。嘗試是這樣的:

public abstract class ScrollTouchListener implements View.OnTouchListener { 

    private double x = 0; 
    private double y = 0; 

    @Override 
    public boolean onTouch(View view, MotionEvent event) {  

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       // User started scrolling 
       x = event.getX(); 
       y = event.getY(); 
       onScrollStarted(x, y); 
       return true; 

      case MotionEvent.ACTION_UP: 
       // User stopped scrolling 
       x = event.getX(); 
       y = event.getY(); 
       onScrollEnded(x, y); 
       return true; 

      case MotionEvent.ACTION_MOVE: 
       // Finger was moved get new x and y coordinates 
       // and calculate the difference 
       double newX = event.getX(); 
       double newY = event.getY(); 

       double difX = x - newX; 
       double difY = y - newY; 

       onScroll(difX, difY); 

       x = newX; 
       y = newY; 
       return true; 

      default: 
       return false; 
     } 
    } 

    protected abstract void onScrollStarted(double x, double y); 
    protected abstract void onScroll(double deltaX, double deltaY); 
    protected abstract void onScrollEnded(double x, double y); 
} 

您可以使用它像這樣:

scrollView.setOnTouchListener(new ScrollTouchListener() { 
    @Override 
    protected void onScrollStarted(double x, double y) { 
     // Called when you start scrolling. 
     // x and y are the coordinates where the user started scrolling.  
    } 

    @Override 
    protected void onScroll(double deltaX, double deltaY) { 
     // Called while scrolling. 
     // deltaX and deltaY tell you how much the finger was moved. 
    } 

    @Override 
    protected void onScrollEnded(double x, double y) { 
     // Called when you stop scrolling. 
     // x and y are the coordinates where the user stopped scrolling. 
    } 
}); 
+0

謝謝!有用! :-) – user3711263

+0

也許這是一個很好的解決方案,但在我的情況下它不起作用。它計算從頂部,三角洲的偏移量,但它防止滾動。請參閱http://stackoverflow.com/questions/3327087/how-to-get-the-current-y-offset-of-a-scrollview。 – CoolMind

+0

@CoolMind設置一個觸摸監聽器並消耗觸摸事件當然意味着'ScrollView'不會滾動。這是有點點。 –

1

有我的解決方案,完美的作品,嘗試讀取代碼

public class CustomScrollView extends NestedScrollView 
{ 

private boolean mIsScrolling; 
private boolean mIsTouching; 
private OnScrollListener mOnScrollListener; 
private Runnable mScrollingRunnable; 

public interface OnScrollListener 
{ 
    void onScrollChanged(CustomScrollView scrollView, int x, int y, int oldX, int oldY); 

    void onEndScroll(CustomScrollView scrollView); 

    void onGoUp(); 

    void onGoDown(); 
} 

public CustomScrollView(final Context context) 
{ 
    super(context); 
} 

public CustomScrollView(final Context context, final AttributeSet attrs) 
{ 
    super(context, attrs); 
} 

public CustomScrollView(final Context context, final AttributeSet attrs, final int defStyleAttr) 
{ 
    super(context, attrs, defStyleAttr); 
} 

private int y = 0; 

@Override 
public boolean dispatchTouchEvent(MotionEvent iEv) 
{ 
    if (isEnabled()) 
    { 

     processEvent(iEv); 

     super.dispatchTouchEvent(iEv); 

     return true; //to keep receive event that follow down event 
    } 
    return super.dispatchTouchEvent(iEv); 
} 

private void processEvent(final MotionEvent iEv) 
{ 
    switch (iEv.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
      y = (int) iEv.getY(); 
      break; 

     case MotionEvent.ACTION_UP: 
      y = (int) iEv.getY(); 

      if (mIsTouching && !mIsScrolling) 
      { 
       if (mOnScrollListener != null) 
       { 
        //If you need to know when end of scrolling just uncomment 
        mOnScrollListener.onEndScroll(this); 
       } 
      } 

      mIsTouching = false; 
      break; 
     case MotionEvent.ACTION_MOVE: 
      mIsTouching = true; 
      mIsScrolling = true; 

      int newY = (int) iEv.getY(); 
      int difY = y - newY; 

      int MAX_VALUE = 200; 
      int MIN_VALUE = -200; 
      if (difY > MAX_VALUE) 
      { 
       AppUtils.printLog(Log.ERROR, "TAG", "difY down : " + difY + " , y : " + y + ", newY : " + newY); 
       if (mOnScrollListener != null) 
       { 
        mOnScrollListener.onGoDown(); 
       } 
       y = newY; 
      } 
      else if (difY < MIN_VALUE) 
      { 
       AppUtils.printLog(Log.ERROR, "TAG", "difY up : " + difY + " , y : " + y + ", newY : " + newY); 
       if (mOnScrollListener != null) 
       { 
        mOnScrollListener.onGoUp(); 
       } 
       y = newY; 
      } 

      break; 
    } 
} 

@Override 
protected void onScrollChanged(int iX, int iY, int iOldX, int iOldY) 
{ 
    super.onScrollChanged(iX, iY, iOldX, iOldY); 

    if (Math.abs(iOldX - iX) > 0) 
    { 
     if (mScrollingRunnable != null) 
     { 
      removeCallbacks(mScrollingRunnable); 
     } 

     mScrollingRunnable = new Runnable() 
     { 
      public void run() 
      { 
       if (mIsScrolling && !mIsTouching) 
       { 
        if (mOnScrollListener != null) 
        { 
         mOnScrollListener.onEndScroll(CustomScrollView.this); 
        } 
       } 

       mIsScrolling = false; 
       mScrollingRunnable = null; 
      } 
     }; 

     postDelayed(mScrollingRunnable, 200); 
    } 

    if (mOnScrollListener != null) 
    { 
     mOnScrollListener.onScrollChanged(this, iX, iY, iOldX, iOldY); 
    } 
} 

public OnScrollListener getOnScrollListener() 
{ 
    return mOnScrollListener; 
} 

public void setOnScrollListener(OnScrollListener mOnEndScrollListener) 
{ 
    this.mOnScrollListener = mOnEndScrollListener; 
} 
} 

使用方法:只需加此自定義視圖中的XML

<ui.customWidgets.CustomScrollView 
     android:id="@+id/scroll_frag_detail" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:overScrollMode="never" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:scrollbars="none" 
     app:behavior_overlapTop="192dp" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <!--Your code here--> 

     </LinearLayout> 

    </ui.customWidgets.CustomScrollView> 

代碼

mScrollView.setOnScrollListener(new CustomScrollView.OnScrollListener() 
    { 
     @Override 
     public void onScrollChanged(final CustomScrollView scrollView, final int x, final int y, final int oldX, final int oldY) 
     { 

     } 

     @Override 
     public void onEndScroll(final CustomScrollView scrollView) 
     { 

     } 

     @Override 
     public void onGoUp() 
     { 

     } 

     @Override 
     public void onGoDown() 
     { 

     } 
    });