gpt4 book ai didi

android - 如何使用 ObjectAnimator 基于拖动 Action 平滑地向上/向下设置 View 动画?

转载 作者:行者123 更新时间:2023-11-29 14:32:38 24 4
gpt4 key购买 nike

我创建了一个 View ,其中有一个 MapFragment 占据了屏幕的顶部 1/3,还有一个 ListView 占据了屏幕的底部 2/3。 ListView 在其列表项的正上方有一个“句柄”,用户可以使用它向上或向下拖动整个 ListView。一旦用户将手指从屏幕上松开,ListView 应该动画到整个屏幕的壁橱顶部或底部边框。到目前为止我可以正常工作,但动画并不流畅。在用户从屏幕上松开手指后,在 ListView 开始向最近的边缘设置动画之前,有一个明显的停顿。我正在使用 ObjectAnimator 的 nineoldandroids 实现,以便动画可以在 Honeycomb 之前的设备上运行。有任何想法吗?

下面是我的 onTouch 实现:

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;
}

最佳答案

阅读 Google 的 Chet Haase 对此处发现的类似问题的回答后,终于解决了这个问题。 https://stackoverflow.com/a/14780019/476005

在每一帧上调整 View 的边距成本太高,而且总是会卡顿,除非您移动的 View 非常简单。因此,为了解决这个问题,我在我的 OnTouch(...) 方法中执行了以下操作:

  1. 在 MotionEvent.ACTION_DOWN 上:将 View 的高度设置为其最大高度。
  2. 在 MotionEvent.ACTION_MOVE 上:将 View 的“y”属性动画化为 event.getRawY,持续时间为 0。
  3. 在 MotionEvent.ACTION_UP 上:将 View 的“y”属性动画化到 View 的父级边缘之一。这里要做的重要事情是在 onAnimationEnd 中适本地设置 View 的高度。

希望对大家有所帮助。

关于android - 如何使用 ObjectAnimator 基于拖动 Action 平滑地向上/向下设置 View 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15984679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com