java - 我如何与动画互动?

标签 java android android-animation

我有一个在屏幕上滑动的动画,当动画正在播放时我希望它在被点击后消失!

我尝试过使用 OnClickListener() 和 OnClickMethods,它们都只在动画播放完毕后才起作用!

    TranslateAnimation animation = new TranslateAnimation(answer,  answer, 0, height * -1); // xfrom , xto, y from,y to
    animation.setDuration(5000);
    Floater0.startAnimation(animation);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (AdPlayer0.isPlaying()) {

                answer = rn.nextInt(width) + 1; //useless
                TranslateAnimation animation0 = new TranslateAnimation(answer, answer, 0, height * -1); // xfrom , xto, y from,y to
                animation0.setDuration(5000);
                animation0.setFillEnabled(true);

                Floater1.startAnimation(animation0);
                Floater1.setVisibility(View.VISIBLE);
                Floater0.startAnimation(animation);
            }
        }
    });

我正在阅读答案 here并提到了一些有趣的事情:

Animations do NOT effect the 'physical' location of the views on screen. If you want it to actually move then you need to adjust the properties of the view.

我现在已经测试了上面的引用,它似乎是真的!我还研究了如何在动画结束后调整 View 的位置属性,但是我想在动画结束之前(播放时)更改其位置!

有任何变通建议吗?理想情况下,我想在播放动画时更改 View 的物理位置!

最佳答案

A TranslationAnimation仅移动屏幕上的像素,因此 OnClickListener 不会对其进行动画处理。使用 ViewPropertyAnimator相反,并设置一个 OnClickListener 然后它应该工作。

代码看起来像这样:

Floater1.animate().setDuration(5000).translationX(answer).translationY(height * -1);

查看 docs 中的所有可用方法.

编辑/更新:

.translationX(float toX)

意思是 -> 将 View 动画化到 x 轴上的这一点。

动画总是从 View 的当前位置开始,因此您不需要 fromX。您还可以使用其他方法:

 .translationXBy(float byX)
 .translationYBy(float byY)

通过这些,您可以将 View BY 浮点值,而不是TO 屏幕上的某个点。

关于java - 我如何与动画互动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37683833/

相关文章:

java - 用于测试和主要的不同 Maven 编译器版本

android - 无法测试和部署用于推理的 deeplabv3-mobilenetv2 tensorflow-lite 分割模型

java - 如何修复更改类似数组列表值的 android Setters?

Android复选框在更改前监听点击

java - 有没有办法在执行时减慢翻译动画的速度?

java - 如何在android中创建平滑的高度变化动画

java - 企业 Java Web 应用程序通常需要访问文件系统吗?

java - fragment 中更改主题(setTheme)类android Utils "this"错误

android - 查看翻转(日历翻转)3d 动画 android

java - 如何在 Spring Boot 应用程序中使用 Apache Tailer?