【问题标题】:How to implement Custom listview text animation如何实现自定义列表视图文本动画
【发布时间】:2015-01-17 18:42:41
【问题描述】:

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

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

这种类型的动画是我的要求。

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

我的 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 已经占用了更多长度..

我的问题,

我的动画文本视图不能超出自定义行的范围。

那么如何将自定义行文本视图移动到图像的末尾(购物车图像)?

【问题讨论】:

  • 这是因为视图仅在其边界内进行动画处理。在所有内容之上创建一个空的RelativeLayout,以便为您的视图设置动画。
  • 再次阅读我的评论。
  • RelativeLayout 将跨越整个屏幕,因此该行可以从您想要的任何位置开始动画。
  • 您应该创建该行的位图并为其设置动画,而不是动画视图。我认为这应该可以解决您的问题。
  • 如果您知道这一点,请再次阅读我的建议。该位图与视图没有任何界限,我们可以在我们想要的地方制作动画

标签: android android-animation android-custom-view


【解决方案1】:

在 TextView 的父视图以及覆盖您要在其中设置动画的范围的任何祖父视图上设置 android:clipChildren="false"

【讨论】:

    【解决方案2】:

    目标是将视图从一个位置动画到另一个位置,因此首先我们需要获取两个点。您可以执行以下操作:

    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 的位置),我们需要从一个点到下一个点进行动画处理。我们可以通过在窗口层或列表视图上方的框架布局上放置一个新的文本视图来做到这一点。

    这里我们添加到窗口层:

    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()
    

    【讨论】:

    • 真的很有帮助.. 谢谢@Mark Hetherington
    • 你能解释一下 view.getMeasuredWidth() 和 newTextView 中的视图对象吗?
    【解决方案3】:

    我相信this 是您所关心的。您可以使用称为View Overlay 的东西。您需要的类似于第一个动画,其中按钮从框架落到屏幕底部。

    总结如下

        final Button button = (Button) findViewById(R.id.button);
    
        button.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // Our button is added to the parent of its parent, the most top-level layout
                final ViewGroup container = (ViewGroup) button.getParent().getParent();
                container.getOverlay().add(button);
    
                ObjectAnimator anim = ObjectAnimator.ofFloat(button, "translationY", container.getHeight());
    
               //You dont need rotation
                ObjectAnimator rotate = ObjectAnimator.ofFloat(button, "rotation", 0, 360);
                rotate.setRepeatCount(Animation.INFINITE);
                rotate.setRepeatMode(Animation.REVERSE);
                rotate.setDuration(350);
    
                /*
                 * Button needs to be removed after animation ending
                 * When we have added the view to the ViewOverlay, 
                 * it was removed from its original parent.
                 */
                anim.addListener(new AnimatorListener() {
    
                    @Override
                    public void onAnimationStart(Animator arg0) {
                    }
    
                    @Override
                    public void onAnimationRepeat(Animator arg0) {
                    }
    
                    @Override
                    public void onAnimationEnd(Animator arg0) {
                        container.getOverlay().remove(button);
                    }
    
                    @Override
                    public void onAnimationCancel(Animator arg0) {
                        container.getOverlay().remove(button);
                    }
                });
    
                anim.setDuration(2000);
    
                AnimatorSet set = new AnimatorSet();
                set.playTogether(anim, rotate);
                set.start();
            }
        });
    

    在您的情况下,您需要正确识别容器。它应该是活动的根视图。此外,示例中的按钮是查看您将制作动画的视图(歌曲名称)。最后根据您的要求进行翻译。

    【讨论】:

    • 但它最低 api 级别 18+.. :(
    • 我在旧 API 中寻找支持,但在支持库中没有得到任何支持。
    • 最后一件事。就像 user3249477 之前所说的,您创建了一个相对父布局。然后,您使用 getDrawingCache 方法在单击时获取视图的位图副本,将其分配给新的 ImageView,将其添加到父级并在屏幕上为该 ImageView 设置动画。
    猜你喜欢
    • 2012-08-10
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多