【问题标题】:Translate (curve path) + Scale animation Android翻译(曲线路径)+缩放动画Android
【发布时间】:2017-12-05 04:47:31
【问题描述】:

我想在曲线中平移图像视图,同时在平移时对其进行缩放。

我经历过 多个帖子喜欢 https://stackoverflow.com/a/30254927/3518278 我不知道如何计算我的路径变量

还有 它在这里提到,android 5 提供了极坐标中的基本曲线 https://stackoverflow.com/a/26591870/3518278 但它们似乎没有效果。

我当前的代码是

View view;
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1
        animator.setDuration(5000); // 5 seconds duration from 0 to 1
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = ((Float) (animation.getAnimatedValue()))
                        .floatValue();
                // Set translation of your view here. Position can be calculated
                // out of value. This code should move the view in a half circle.
                img_fullscreen_drone.setTranslationX((float)(150.0 * Math.sin(value*Math.PI)));
                img_fullscreen_drone.setTranslationY((float)(150.0 * Math.cos(value*Math.PI)));
                img_fullscreen_drone.setScaleType(ImageView.ScaleType.CENTER);
                img_fullscreen_drone.animate().scaleX(0.5f).scaleY(0.5f).setDuration(5000).start();

            }
        });

        animator.start();

这会将我的视图转换为弧形,但在平移完成后开始缩放,我希望缩放和沿曲线平移同时发生。

如有任何帮助,我们将不胜感激

提前致谢

【问题讨论】:

    标签: android


    【解决方案1】:

    您的原始代码是在翻译图像的动画的每一帧重新开始缩放动画。您只需将缩放动画代码移出更新块,如下所示。

    请注意,您使用了两种略有不同的动画方法,即缩放和平移,这可能会让您感到困惑。

        View view;
        ValueAnimator animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1
        animator.setDuration(5000); // 5 seconds duration from 0 to 1
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = ((Float) (animation.getAnimatedValue()))
                        .floatValue();
                // Set translation of your view here. Position can be calculated
                // out of value. This code should move the view in a half circle.
                img_fullscreen_drone.setTranslationX((float)(150.0 * Math.sin(value*Math.PI)));
                img_fullscreen_drone.setTranslationY((float)(150.0 * Math.cos(value*Math.PI)));
            }
        });
    
        animator.start();
    
        img_fullscreen_drone.setScaleType(ImageView.ScaleType.CENTER);
        img_fullscreen_drone.animate().scaleX(0.5f).scaleY(0.5f).setDuration(5000).start();
    

    【讨论】:

    • 150 是原始发布者使用的任意值。
    • 当我点击它时,它把它取下来然后上升,当起始位置是它现在的位置时,我如何让它去动画
    【解决方案2】:

    在摆弄乔尔的答案后,我发现您也可以使用ViewPropertyAnimator 并以这种方式一次性写完:

    img_fullscreen_drone.setScaleType(ImageView.ScaleType.CENTER);
    img_fullscreen_drone.animate()
      .setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = ((Float) (animation.getAnimatedValue()));
            // Set translation of your view here. Position can be calculated
            // out of value. This code should move the view in a half circle.
            img_fullscreen_drone.setTranslationX((float)(150.0 * Math.sin(value*Math.PI)));
            img_fullscreen_drone.setTranslationY((float)(150.0 * Math.cos(value*Math.PI)));
        }})
      .scaleX(0f).scaleY(0f)
      .setDuration(5000)
      .start();
    

    未经测试,我认为它应该可以正常工作。

    【讨论】:

      【解决方案3】:

      首先制作一个动画组。所以像

      AnimatorSet set = new AnimatorSet();
      

      然后你必须将你的 2 个动画分开,这两个动画以动画师的形式进行翻译和缩放,你可以像这样

      用于翻译:

      ObjectAnimator translateAnimator = ObjectAnimator.ofInt(view, "y", toValue);
      

      你可以为规模做同样的事情。然后你们就可以一起玩了

      set.playTogether(translateAnimator, scaleAnimator);
      

      这应该一起缩放和翻译。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        • 2012-04-13
        • 2011-09-18
        • 1970-01-01
        相关资源
        最近更新 更多