【问题标题】:Android Scale Animation - Reverse IssueAndroid 缩放动画 - 反向问题
【发布时间】:2015-11-27 23:16:03
【问题描述】:

我设法为我的图像添加了一个缩放动画,使其从原始尺寸增长到更大的尺寸。但是,我需要添加另一个动画代码副本,使其缩小到其原始大小。我正在尝试在布尔值为真时循环动画。

我对参数进行了一些调整,但无法使其发挥作用。到目前为止,这是我的代码:

class AnimateButton extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            Boolean isGlowing = true; //Make it run forever
            while (isGlowing) {
                scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
                scal_grow.setDuration(1500);
                scal_grow.setFillAfter(true);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btn_layer.setAnimation(scal_grow);
                    }
                });

                try {
                    Thread.sleep(1500);
                } catch (Exception e) { }

                     //Add a reverse animation such that it goes back to the original size

            }
        return null;
     }
}

我应该做些什么改变?

【问题讨论】:

  • 您是否正在创建 AsyncTask 来为按钮设置动画?
  • 是的,我是。确保它在后台运行@RamiJemli

标签: java android android-intent mobile android-animation


【解决方案1】:

在 android 中,除了 UIThread(主线程)之外的任何其他线程都不会发生动画和 UI 更新。
删除 AsyncTask 并尝试使用 ViewPropertyAnimator,它在性能方面比 ScaleAnimation 更好。另外,它只有一行。

缩放:

btn_layer.animate().scaleX(1.2f).scaleY(1.2f).setDuration(1500).start();

取消缩放:

btn_layer.animate().scaleX(0.8f).scaleY(0.8f).setDuration(1500).start();

更新

PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(1500);
anim.start();

【讨论】:

  • 这两行我一个接一个地使用,但是我没有看到按钮改变
  • 你确定吗?我 100% 确定它们有效。第一个是缩放,第二个是取消缩放。
  • 您可能希望在取消缩放之前等待,不要同时进行
  • 您想在什么时候精确地取消缩放按钮?
  • 只是一个平滑的缩放和取消缩放动画,总时间跨度约为 1.5 秒
【解决方案2】:

我绝对建议不要使用旧的 android.view.animation 动画,而是使用 Property Animations。旧的视图动画效果不佳,并且不能像您期望的那样在视图上工作。

使用属性动画,这个脉冲动画非常简单。您可以像这样在 XML 中定义它:

<animator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:valueFrom="1"
    android:valueTo="1.2"
    android:valueType="floatType"
    android:repeatCount="infinite"
    android:repeatMode="reverse"/>

当你应用这个时,动画将通过反转无限重复。

在 Java 中它看起来像这样:

    PropertyValuesHolder scalex = PropertyValuesHolder.ofFloat(View.SCALE_X, 1.2f);
    PropertyValuesHolder scaley = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1.2f);
    ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(btn_layer, scalex, scaley);
    anim.setRepeatCount(ValueAnimator.INFINITE);
    anim.setRepeatMode(ValueAnimator.REVERSE);
    anim.setDuration(1500);
    anim.start();

这将为您提供更令人期待的体验,并为您处理线程和时间安排。当你想停止动画时,你只需调用s.cancel()就可以了。

【讨论】:

  • 这会做动画,但它会缩小并缩放到右侧。基本上不是我心目中的动画。不过我会试着玩一下它
  • 视图上设置的重力是多少?如果它是 GRAVITY_RIGHT 或 GRAVITY_END,或者您将它的布局与右侧对齐,则会发生这种情况,因为这实际上是在更改视图的属性,其中视图动画仅更改绘制的内容(即触摸目标将关闭)
  • @rharter 这可以使用一个 ObjectAnimator 来实现。
  • 不确定你的意思,@RamiJemli,我在我的例子中使用了 ObjectAnimator。
  • 抱歉,我指的是 ObjectAnimtor 的一个实例。不需要两个和一个 AnimatorSet。
【解决方案3】:

要取消缩放,您可以使用:

 ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
 unscal_grow .setDuration(1500);
 unscal_grow .setFillAfter(true);

一种缩放和取消缩放的方法:

 private void scaleAndUnscale() {
    ScaleAnimation scal_grow = new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
    scal_grow.setDuration(1500);
    scal_grow.setFillAfter(true);

    image1.startAnimation(scal_grow);

    scal_grow.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            ScaleAnimation unscal_grow = new ScaleAnimation(1.2f, 1.0f, 1.2f, 1.0f, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_SELF, (float) 0.5);
            unscal_grow.setDuration(1500);
            unscal_grow.setStartOffset(1500);
            unscal_grow.setFillAfter(true);

            image1.startAnimation(unscal_grow);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

【讨论】:

  • 似乎无法取消比例
  • 我检查了它并且它有效。关于睡眠,我不认为你的 AsyncTask 动画解决方案是好的。您可能希望使用带有 postDelayed 的 Handler,因此它将在 UI 线程上运行,无需休眠。
  • 其实一个更简单的解决方案,如果假设运行一次,就是让unscale动画在onAnimationEnd中运行
  • 我希望它在布尔值为真时运行。我已经说过那个伙伴:)我可以得到你尝试过的完整解决方案吗?我似乎无法让它工作
  • 我已经添加了一个方法来实现 scale unscale
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多