Android翻译动画就像交换两个视图

toiithl6  于 2023-01-11  发布在  Android
关注(0)|答案(1)|浏览(129)

我尝试用Translate动画模拟交换两个视图(一个向上,一个向下)。
基本上,它交换再交换。
这是我的活动。

LinearLayout topView, belowView;
  TextView foo, bar;
  int viewHeight;
  boolean noSwap = true;
  private static int ANIMATION_DURATION = 3000;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slide);

    topView = (LinearLayout) findViewById(R.id.top_view);
    belowView = (LinearLayout) findViewById(R.id.below_view);

    foo = (TextView) findViewById(R.id.foo);
    bar = (TextView) findViewById(R.id.bar);

    ImageButton btnSwitch = (ImageButton) findViewById(R.id.switch_btn);

    ViewTreeObserver viewTreeObserver = foo.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
      viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
          foo.getViewTreeObserver().addOnGlobalLayoutListener(this);
          viewHeight = foo.getHeight();
          foo.getLayoutParams();
        }
      });
    }

    btnSwitch.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (noSwap) {
          //Log.d("Swap Status", "Not Swapping yet ? " + true);

          TranslateAnimation ta1 = new TranslateAnimation(0, 0, 0, viewHeight);
          ta1.setDuration(ANIMATION_DURATION);
          ta1.setFillAfter(true);
          topView.startAnimation(ta1);
          topView.bringToFront();

          belowView.setY(0);
          TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, -viewHeight);
          ta2.setDuration(ANIMATION_DURATION);
          ta2.setFillAfter(true);
          belowView.startAnimation(ta2);
          belowView.bringToFront();

          noSwap = false;
        } else {
          //Log.d("Swap Status", "Swapped already ? " + true);

          TranslateAnimation ta1 = new TranslateAnimation(0, 0, viewHeight, 0);
          ta1.setDuration(ANIMATION_DURATION);
          ta1.setFillAfter(true);
          topView.startAnimation(ta1);
          topView.bringToFront();

          belowView.setY(0);

          TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, viewHeight);
          ta2.setDuration(ANIMATION_DURATION);
          ta2.setFillAfter(true);
          belowView.startAnimation(ta2);
          belowView.bringToFront();

          noSwap = true;
        }
      }
    });
  }

这是布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}">

  <LinearLayout
      android:id="@+id/top_view"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <TextView
        android:id="@+id/foo"
        android:text="@string/foo"
        android:textSize="30sp"
        android:textColor="@android:color/holo_purple"
        android:padding="10dp"
        android:fontFamily="sans-serif-condensed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  </LinearLayout>

  <LinearLayout
      android:id="@+id/below_view"
      android:layout_below="@+id/top_view"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
    <TextView
        android:id="@+id/bar"
        android:text="@string/bar"
        android:textSize="30sp"
        android:textColor="@android:color/holo_red_light"
        android:padding="10dp"
        android:fontFamily="sans-serif-condensed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
  </LinearLayout>

  <ImageButton android:id="@+id/switch_btn"
      style="?android:borderlessButtonStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/ic_action_import_export"
      android:contentDescription="@string/swap"
      android:layout_alignParentBottom="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"/>

</RelativeLayout>

一切似乎都很好,直到我第三次点击Switch按钮时,顶部视图应该下降,底部视图应该上升。但底部视图的起始位置不正确。我尝试了几个位置设置为底部视图,但仍然无法正确。
我已经做了一个关于它的视频。

c90pui9n

c90pui9n1#

我想我是对的。这是工人阶级。

LinearLayout topView, belowView;
  TextView foo, bar;
  int viewHeight;
  boolean noSwap = true;
  private static int ANIMATION_DURATION = 300;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slide);

    topView = (LinearLayout) findViewById(R.id.top_view);
    belowView = (LinearLayout) findViewById(R.id.below_view);

    foo = (TextView) findViewById(R.id.foo);
    bar = (TextView) findViewById(R.id.bar);

    ImageButton btnSwitch = (ImageButton) findViewById(R.id.switch_btn);

    ViewTreeObserver viewTreeObserver = foo.getViewTreeObserver();
    if (viewTreeObserver.isAlive()) {
      viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
          foo.getViewTreeObserver().addOnGlobalLayoutListener(this);
          viewHeight = foo.getHeight();
          foo.getLayoutParams();
        }
      });
    }

    btnSwitch.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (noSwap) {
          TranslateAnimation ta1 = new TranslateAnimation(0, 0, 0, viewHeight);
          ta1.setDuration(ANIMATION_DURATION);
          ta1.setFillAfter(true);
          topView.startAnimation(ta1);
          topView.bringToFront();

          TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, -viewHeight);
          ta2.setDuration(ANIMATION_DURATION);
          ta2.setFillAfter(true);
          belowView.startAnimation(ta2);
          belowView.bringToFront();

          noSwap = false;
        } else {
          TranslateAnimation ta1 = new TranslateAnimation(0, 0, viewHeight, 0);
          ta1.setDuration(ANIMATION_DURATION);
          ta1.setFillAfter(true);
          topView.startAnimation(ta1);
          topView.bringToFront();

          TranslateAnimation ta2 = new TranslateAnimation(0, 0, -viewHeight, 0);
          ta2.setDuration(ANIMATION_DURATION);
          ta2.setFillAfter(true);
          belowView.startAnimation(ta2);
          belowView.bringToFront();

          noSwap = true;
        }
      }
    });
  }

相关问题