2017-02-23 46 views
0

我試圖使用ViewDragHelper動畫我的appBar,當我拖動它時它應該開始動畫並下去。Android - 動畫視圖並改變其真實位置(和另一個視圖跟隨行爲依賴)

在上面我有另一種看法(粉紅色在the example),「跟隨」我的appbar與行爲(頂視圖取決於AppBarLayout)。

As you can see here appbar是動畫,但「真實」的位置不會改變,顯然,我的頂部框架不會移動。

爲了動畫我用下面的代碼:

@Override 
public void onViewReleased(final View releasedChild, float xvel, float yvel) { 
    super.onViewReleased(releasedChild, xvel, yvel); 
    if (releasedChild.getTop() > childHeight) { 
     Log.e("TAG", "SHOULD SCROLL " + releasedChild.getTop() + " > " + childHeight); 
     final int offset = height - releasedChild.getTop() - 100; 
     ViewCompat.animate(releasedChild).translationY(offset).setDuration(1000).setInterpolator(new OvershootInterpolator()).start(); 
    } else { 
     Log.e("TAG", "SHOULD NOT SCROLL " + releasedChild.getTop() + " < " + childHeight); 
     eleasedChild.offsetTopAndBottom(-releasedChild.getTop()); 
    } 
    if (mDragFrameLayoutController != null) { 
     mDragFrameLayoutController.onDragDrop(false); 
    } 
} 

我和許多其他動畫嘗試:

TranslateAnimation scale = new TranslateAnimation(0.0f, 0.0f, 0.f, offset); 
scale.setInterpolator(new OvershootInterpolator()); 
scale.setDuration(3000L); 
scale.setFillEnabled(true); 
scale.setFillAfter(true); 
scale.setAnimationListener(new Animation.AnimationListener() { 

ObjectAnimator moveY = ObjectAnimator.ofInt(releasedChild, "y", releasedChild.getTop(), offset); 
AnimatorSet as = new AnimatorSet(); 
as.play(moveY); 
as.start(); 

所有這些片斷給我相同的結果。

我發現移動視圖使用偏移但這並不動畫,它只是向下移動的唯一途徑...

releasedChild.offsetTopAndBottom(offset); 

任何想法?謝謝。

回答

0

我終於找到了解決辦法:

我viewDragHelper我推翻了以下方法:

 @Override 
     public int getViewVerticalDragRange(View child) { 
      return getMeasuredHeight() - child.getMeasuredHeight(); 
     } 


     @Override 
     public void onViewReleased(View releasedChild, float xvel, float yvel) { 
      super.onViewReleased(releasedChild, xvel, yvel); 
      if(yvel>0) { 
       mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), getMeasuredHeight()-releasedChild.getMeasuredHeight()); 
      } else { 
       mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), 0); 
      } 
      invalidate(); 
     } 

然後在我的自定義視圖我推翻了以下方法:

@Override 
    public void computeScroll() { 
     super.computeScroll(); 
     if (mDragHelper.continueSettling(true)) { 
      ViewCompat.postInvalidateOnAnimation(this); 
     } 
    } 

而且一切工作得很好! 我希望這可以幫助未來的人。

相關問題