【问题标题】:Problems with Views and animations视图和动画的问题
【发布时间】:2016-08-15 18:12:52
【问题描述】:

我有线性布局,我想在点击时相互替换。 开始时:Linear Layout A 可见,Linear Layout B 不见了 我希望在单击 A 时消失而 B 可见,反之亦然。 没有动画,一切正常,但是当我在单击 B 后设置动画时 B 消失了,但是 A 不可见,尽管如果我单击它的位置,日志给我它是可见的 这是代码,任何帮助将不胜感激

private void switchRowItems(final LinearLayout toBeHiddenRow,final LinearLayout toBeShownRow){
    toBeHiddenRow.animate()
            .rotation(toBeHiddenRow.getHeight()/2)
            .alpha(0.0f)
            .setDuration(300)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    toBeHiddenRow.clearAnimation();
                    toBeHiddenRow.setVisibility(View.GONE);
                    toBeShownRow.clearAnimation();
                    toBeShownRow.setVisibility(View.VISIBLE);
                }
            });
    //toBeShownRow.clearAnimation();
   // toBeShownRow.setVisibility(View.VISIBLE);

}

点击检查器很简单:

 if (llRowTwoItemOne.getVisibility() == View.VISIBLE) {
        Log.d("llRowTwoItemOne","visible");
    } else {
        Log.d("llRowTwoItemOne","not visible");
    }

【问题讨论】:

    标签: android animation view android-linearlayout


    【解决方案1】:

    我是这样做的:

    1. 在 res/anim 资源目录中创建 xml 文件。让我们称之为 myanimation.xml 并写在那里:
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    
    <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:duration="300">
        </rotate>
    
        <alpha
            android:fromAlpha="1.0"
            android:toAlpha="0.0"
            android:duration="200">
        </alpha>
    </set>
    

    你可以看到它是你需要的一组动画:旋转和alpha。

    1. 然后我在switchRowItem函数中这样写:

      private void switchRowItems(final LinearLayout toBeHiddenRow, final LinearLayout toBeShownRow){
      
      Animation anim = AnimationUtils.loadAnimation(this, R.anim.myanimation);
      anim.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
      
          }
      
          @Override
          public void onAnimationEnd(Animation animation) {
      
              toBeHiddenRow.setVisibility(View.GONE);
              toBeShownRow.setVisibility(View.VISIBLE);
          }
      
          @Override
          public void onAnimationRepeat(Animation animation) {
          }
      });
      toBeHiddenRow.startAnimation(anim);
      
      }
      

    就是这样。它工作得很好。希望这是您的要求。

    【讨论】:

    • 非常感谢它的工作,但我如何设置旋转 llRow.getHeight()/2 ??
    • @eshteghelcompany,不客气。我不确切知道这一点。如果只有旋转动画,那么您可以为此目的使用 RotateAnimation 类。但是你有一组动画。所以它使事情变得复杂。也许这是另一个帖子的问题? =)
    • 太棒了!只是不明白旋转方法到底是做什么的。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多