android - 如何实现自定义listview文字动画

标签 android android-animation android-custom-view

我正在 android 中创建购物应用程序。在我的应用中,我显示了自定义 ListView 中的项目列表。

如果客户选择一个项目,则所选项目文本从 ListView 移动到购物车图像中。如下图,

这种类型的动画是我的要求。 enter image description here

但我的动画 View 停在自定义行的末尾......就像这张图片。

enter image description here

我的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/

相关文章:

Android - 如何知道动画是否因为 View 的父级被隐藏而没有运行?

Android GestureDetector 无法使用 FrameLayout 检测到 onScroll 事件

android - 什么是应用程序错误:6 in Android licensing

android - 如何使用任何 "not Activity"中的铃声选择器(无 Activity)

Android 在RelativeLayout中填充左动画

java - Android自定义 View 更新 View 的指定部分

android - 我如何在具有许多绘图形状的自定义 View 上创建边框

安卓工作室 3.0 : Unable to delete file

android - 从 native 代码渲染位图 - nativeCreate 位图不会从内存中清除

java - 动画后移动 ImageView (更新位置)