-->

如何顺利动画视图向上/向下基于使用ObjectAnimator拖累运动?(How to smooth

2019-10-18 00:51发布

我创建具有占据屏幕的顶部1/3的MapFragment和占据屏幕的底部三分之二一个ListView的图。 ListView控件有一个“处理”的正上方,用户可以用它来拖动整个ListView的向上或向下的列表项。 一旦用户发布他们的手指离开屏幕的ListView应该动画的衣柜顶部或底部边框整个屏幕。 我有它至今工作,但动画并不顺利。 有一个明显的停顿,用户从屏幕上移开手指后,ListView控件开始朝着最接近边缘动画之前。 我使用的是nineoldandroids实施ObjectAnimator,使得动画在蜂窝前的设备。 有任何想法吗?

这里是我下面的onTouch IMPL:

public boolean onTouch(View v, MotionEvent event) {
    final LayoutParams listLp = (LayoutParams) mListFrame.getLayoutParams();
    final int topMargin = -mHandleShadow.getHeight();
    final int middleMargin = getResources().getDimensionPixelSize(R.dimen.map_handle_margin_top);
    final int bottomMargin = getView().getHeight() - mHandle.getHeight() - mHandleShadow.getHeight();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mInitY = (int) event.getRawY();
            mTopMargin = listLp.topMargin;

            break;
        case MotionEvent.ACTION_MOVE:
            int y = mTopMargin + (int) event.getRawY() - mInitY;

            if (y >= -mHandleShadow.getHeight() && y <= (mViewHeight - mHandle.getHeight() - mHandleShadow.getHeight())) {
                listLp.topMargin = y;
                mListFrame.setLayoutParams(listLp);
            }

            break;
        case MotionEvent.ACTION_UP:
            if ((mInitY > event.getRawY() && mClosestAnchor == Anchor.MIDDLE) || (listLp.topMargin < middleMargin && mClosestAnchor == Anchor.BOTTOM)) {
                ObjectAnimator animator = ObjectAnimator.ofInt(AnimatorProxy.wrap(mListFrame), "topMargin", topMargin);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.start();

                mClosestAnchor = Anchor.TOP;
            }
            else if ((mInitY < event.getRawY() && mClosestAnchor == Anchor.MIDDLE) || (listLp.topMargin > middleMargin && mClosestAnchor == Anchor.TOP)) {
                ObjectAnimator animator = ObjectAnimator.ofInt(AnimatorProxy.wrap(mListFrame), "topMargin", bottomMargin);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.start();

                mClosestAnchor = Anchor.BOTTOM;
            }
            else {
                ObjectAnimator animator = ObjectAnimator.ofInt(AnimatorProxy.wrap(mListFrame), "topMargin", middleMargin);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.start();

                mClosestAnchor = Anchor.MIDDLE;
            }

            break;
    }

    return true;
}

Answer 1:

读谷歌的切特·哈泽的答案,在这里发现了类似的问题后,终于解决了该问题。 https://stackoverflow.com/a/14780019/476005

每一帧上调整视图的保证金是过于昂贵,而且会一直断断续续,除非你正在移动的看法很简单。 因此,要解决这个问题,我做了我的OnTouch(...)方法如下:

  1. 在MotionEvent.ACTION_DOWN:到它的最大高度设置视图的高度。
  2. 在MotionEvent.ACTION_MOVE:为0的持续时间动画视图的“Y”属性设置为event.getRawY。
  3. 在MotionEvent.ACTION_UP:动画视图的“Y”属性视图的父母的优势之一。 在这里做最重要的事情是设置查看适当onAnimationEnd的高度。

我希望这有助于大家。



文章来源: How to smoothly animate a view up/down based on a drag motion using ObjectAnimator?