【问题标题】:How to animate current Activity (not transition)?如何为当前活动设置动画(不是过渡)?
【发布时间】:2019-02-28 01:53:00
【问题描述】:

我想知道如何为当前 Activity 设置动画,而不是在 Activity 之间进行转换。例如,我想将活动稍微向左移动,然后将其向后移动,使其看起来有点摇晃效果。有任何想法吗?谢谢

【问题讨论】:

  • 您是否尝试过使用 id android.R.id.content 为视图设置动画?
  • 为什么要为 Activity 设置动画,您可以在 Activity 中为父布局设置动画。

标签: android android-animation


【解决方案1】:

终于找到了解决办法:

    Animation animation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, -0.05f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    animation.setDuration(100);
    animation.setInterpolator(new AccelerateInterpolator());
    animation.setRepeatCount(1);
    animation.setRepeatMode(Animation.REVERSE);

    View viewActivity = findViewById(android.R.id.content);
    if (viewActivity != null)
        viewActivity.startAnimation(animation);

【讨论】:

    【解决方案2】:

    你需要像下面这样创建动画实例

     Animation startAnimation = AnimationUtils.loadAnimation(context, R.anim.shaking_animation);
    

    那么你需要用你的父布局开始那个动画

       parent_layout.startAnimation(startAnimation);
    

    你可以在这里shaking / wobble view animation in android获得摇动动画

    终于可以像下面这样清除动画了

    parent_layout.clearAnimation();
    

    【讨论】:

      【解决方案3】:

      基本思想是访问活动的根布局,然后在其上做一些动画,例如如果你使用 DataBindung 它就像这样(但不是你想要的那种动画的最佳主意):

          binding.getRoot().animate().rotation(-5).setDuration(500).setListener(new AnimatorListenerAdapter() {
              @Override
              public void onAnimationEnd(Animator animation) {
                  binding.getRoot().animate().rotation(5).setDuration(1000).setListener(new AnimatorListenerAdapter() {
                      @Override
                      public void onAnimationEnd(Animator animation) {
                          binding.getRoot().animate().rotation(0).setDuration(500);
                      }
                  });
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-07
        • 1970-01-01
        • 2022-01-13
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 2021-11-12
        • 2016-04-11
        相关资源
        最近更新 更多