簡體   English   中英

withEndAction()觸發onEndAnimation()函數?

[英]withEndAction() triggering onEndAnimation() function?

這是我的代碼(簡體)

public class StockAdapter extends ArrayAdapter<Stock> implements Animator.AnimatorListener
{
    //Global variable - StockAdapter adapter;
    //Global varaiable - View animatingView;
     View.OnTouchListener stockRowOnTouch=new View.OnTouchListener() {
         @Override
         public boolean onTouch(View view, MotionEvent motionEvent) {
             switch (action) {
                 case MotionEvent.ACTION_DOWN:
                   //code to get the cordintated\s
                  case MotionEvent.ACTION_UP:
                    // get the coodinates
                    if(row is swiped) //determined by the coordinates
                    {
                      animatingView=view;
                      view.animate().translationX(itemWidth).alpha(1).setDuration(500).setListener(adapter);

    }
}

 @Override
    public void onAnimationEnd(Animator animator) {
 animatingView.setTranslationX(0);
        animatingView.animate().alpha(0.4f).setDuration(4000).withEndAction(new Runnable() {
            @Override
            public void run() {
                //After the animation duration delete the stock


            }
        });
}
}

我要做什么

  1. 當將行滑動足夠遠時,便完成了動畫處理,其中動畫在500毫秒內移出了窗口。
  2. 在這段時間之后,該行(視圖)再次轉換回窗口,並對視圖進行一些更改。
  3. 然后,另一個動畫運行4秒鍾,其中視圖必須在該持續時間內消失。 4秒后,我將從列表視圖中刪除該行,並調用notifydatasetchanged()



    我怎么做

  4. 對於第一個動畫(在500毫秒內平移窗口中的行),我設置了一個偵聽器StockAdapter類對象本身。

  5. 然后在onAnimationEnd()函數中,我已將視圖轉換回其原始位置,然后運行淡入淡出動畫,但這一次使用了withEndAction

怎么了

  1. 滑動后,第一個動畫將完美運行,並進入onAnimationEnd()函數,並且可以看到對該視圖所做的更改。
  2. 然后淡入淡出動畫也開始運行( withEndAction() ),並在4秒鍾后從列表視圖中刪除該行並對其進行刷新。
  3. 現在,此動畫將繼續運行,直到沒有數據可放置到該位置為止



在驗證日志時, withEndAction()似乎正在觸發onEndAnimation() 這是某種錯誤嗎?

注意

我簽出了ViewViewPropertyAnimatorViewRootImpl的源代碼。 不建議withEndAction再次調用相同的偵聽器。 如果我錯了,有人可以指出這一點嗎? 的函數調用停止在postDelayed()的方法RunQueue類的ViewRootImpl

您需要在onAnimationEnd()重置動畫偵聽器,以避免觸發與您先前在onTouchListener()設置的偵聽器相同的偵聽器。 從官方文檔尚不清楚,但是View.animate()將嘗試為您的視圖重用同一ViewPropertyAnimator ,包括您先前設置的偵聽器。

下面的代碼應該可以解決問題:

...
animatingView.animate().alpha(0.4f).setDuration(4000).setListener(null).withEndAction(...)
...

問題:

.setListener(adapter)

由於您在4秒鍾后將Animator.AnimatorListener應用於animatingView's ViewPropertyAnimator ,它將執行您的withEndAction 但在記住您將其設置為animatingView的偵聽器之后 ,出現了問題,由於您使用的是與相同的引用 ,因此將一次又一次執行onAnimationEnd animatingViewViewPropertyAnimator

解:

刪除該行后,您需要取消withEndAction內部的動畫,以防止該動畫再次調用接口方法

樣品:

@Override
public void onAnimationEnd(Animator animator) {
 animatingView.setTranslationX(0);
        animatingView.animate().alpha(0.4f).setDuration(4000).withEndAction(new Runnable() {
            @Override
            public void run() {
                //After the animation duration delete the stock
                animatingView.animate().cancel(); //will cancel the animation and listener

            }
        });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM