我正在 android 中创建购物应用程序。在我的应用中,我显示了自定义 ListView 中的项目列表。
如果客户选择一个项目,则所选项目文本从 ListView 移动到购物车图像中。如下图,
这种类型的动画是我的要求。
但我的动画 View 停在自定义行的末尾......就像这张图片。
我的animation.xml代码,
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true"
>
<translate
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:duration="1000"
/>
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0" />
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.0"
android:toYScale="0.2" >
</scale>
</set>
注意:如果有人想要更多代码,我稍后会发布..因为这个 qtn 已经占用了更多的长度..
我的问题,
我的动画 TextView 不能超出自定义行的界限。
那么如何将自定义行 TextView 移动到图像的末尾(购物车图像)?
最佳答案
我们的目标是将 View 从一个位置动画化到另一个位置,因此首先我们需要获得两个点。您可以执行以下操作:
int[] screenLocation = new int[2];
textView.getLocationOnScreen(screenLocation);
int startX = screenLocation[0];
int startY = screenLocation[1];
int[] screenLocationB = new int[2];
cartView.getLocationOnScreen(screenLocationB);
int endX = screenLocationB[0];
int endY = screenLocationB[1];
一旦你有了 TextView 的起始位置和你希望它结束的位置(cartview 的位置),我们需要从一个点到下一个点制作动画。我们可以通过在窗口层或 ListView 上方的框架布局上放置一个新的 TextView 来做到这一点。
这里我们添加到窗口层:
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.width = view.getMeasuredWidth();
layoutParams.height = view.getMeasuredHeight();
activity.addContentView(newTextView, layoutParams);
接下来我们设置起始位置。
newTextView.setTranslationX(startX);
newTextView.setTranslationY(startY);
最后我们制作动画。
newTextView.animate().setDuration(300)
.translationX(endX).translationY(endX).start()
关于android - 如何实现自定义listview文字动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002961/