2013-10-13 155 views
1

我有一個框架佈局內的視圖。我正在跟蹤視圖的ontouch事件,並且我希望視圖能夠在移動手指時以平滑滾動的方式垂直滾動。目前視圖滾動,但我無法得到正確的計算(也許是邏輯),滾動非常不連貫。這與滑動抽屜類似,對於我的目的,滑動抽屜不起作用。Android平滑垂直滾動觸摸

希望對此問題有任何幫助。謝謝!

cardImage.setOnTouchListener(new OnTouchListener() { 

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

      float startY = 0; 
      float moveY = 0; 
      float velocityX=0, velocityY=0; 
      int index = event.getActionIndex(); 
      int action = event.getActionMasked(); 
      int pointerId = event.getPointerId(index); 

      switch(event.getAction()) 
      { 
       case MotionEvent.ACTION_DOWN: 
        startY = event.getRawY(); 
        isTouchUp = false;      

        if(mVelocityTracker == null) { 
         // Retrieve a new VelocityTracker object to watch the velocity of a motion. 
         mVelocityTracker = VelocityTracker.obtain(); 
        } 
        else { 
         // Reset the velocity tracker back to its initial state. 
         mVelocityTracker.clear(); 
        } 
        // Add a user's movement to the tracker. 
        mVelocityTracker.addMovement(event); 

        previousRawY = startY; 

        return true; 
       case MotionEvent.ACTION_MOVE: 
        moveY = event.getRawY(); 
        isTouchUp = false; 

        mVelocityTracker.addMovement(event); 
        // When you want to determine the velocity, call 
        // computeCurrentVelocity(). Then call getXVelocity() 
        // and getYVelocity() to retrieve the velocity for each pointer ID. 
        mVelocityTracker.computeCurrentVelocity(1000); 
        // Log velocity of pixels per second 
        // Best practice to use VelocityTrackerCompat where possible. 
          velocityX = VelocityTrackerCompat.getXVelocity(mVelocityTracker, pointerId); 
          velocityY = VelocityTrackerCompat.getYVelocity(mVelocityTracker, pointerId); 


        if(Math.abs(velocityX) + 100 > Math.abs(velocityY)) { 
         // right left 
        } 
        else { 
         // up down       
         if(action != MotionEvent.ACTION_OUTSIDE) {       
          if(moveY > previousRawY) { 
           // Moving down 


           for(float i=0;i<moveY-previousRawY;i++) { 
            cardCurrentOffset += (moveY-previousRawY)/(moveY-previousRawY); 

            cardImage.setTranslationY(cardCurrentOffset); 
           } 
          } 
          else { 
           // Moving Up 

           if(cardImage.getY() > topHeaderLayout.getBottom()) { 

            for(float i=0;i<previousRawY-moveY;i++) { 
             cardCurrentOffset -= (previousRawY-moveY)/(previousRawY-moveY); 

             cardImage.setTranslationY(cardCurrentOffset); 
            } 
           } 
          } 

          previousRawY = moveY; 
         } 
        } 

        return true;  
       case MotionEvent.ACTION_UP: 
        isTouchUp = true; 
        return true; 
       case MotionEvent.ACTION_CANCEL: 
        // Return a VelocityTracker object back to be re-used by others. 
        mVelocityTracker.recycle(); 
        return true; 
       case MotionEvent.ACTION_OUTSIDE: 
        return false; 
       default: 
        return false; 
      } 
     } 
    });  
+0

爲什麼不只是使用'ScrollView'? – ssantos

+0

我真的想避免使用ScrollView,如果使用手動滾動計算邏輯,它更適合應用程序。 – user2701882

回答

0

我觀察到dinamycally如利潤率變化的性質,是不順利,因爲我想。而不是我使用TranslateAnimations;訣竅是在滾動過程中應用動畫,並在觸摸時更改真實的邊距。

0

試試這個(你可以在頂部/底部添加一些檢查或甩爲好):

class ScrollingTextView extends TextView { 
    private GestureDetector mDetector; 

    public ScrollingTextView(Context context) { 
     super(context); 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < 30; i++) { 
      sb.append("Line #").append(i).append("\n"); 
     } 
     setTextSize(24); 
     setText(sb); 
     setTextColor(0xffeeeeee); 
     mDetector = new GestureDetector(mListener); 
    } 

    private OnGestureListener mListener = new SimpleOnGestureListener() { 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      scrollBy(0, (int) distanceY); 
      return true; 
     } 
    }; 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     mDetector.onTouchEvent(event); 
     return true; 
    } 
} 

,以測試其添加在Activity.onCreate如下:

View v = new ScrollingTextView(this); 
setContentView(v);