Android 2个图像,当我点击其中一个时交换

Android 2个图像,当我点击其中一个时交换,android,Android,标题上写着什么。首先,我创建了一个从一个图像切换到另一个图像的方法,它可以正常工作。但是,当我添加第二个方法时,为了反转操作并切换回第一个方法,没有任何变化。它甚至不会从第一个图像还原到第二个图像。代码如下: public void fade(View view) { ImageView laxus = (ImageView) findViewById(R.id.laxus_shirt); ImageView laxos2 = (ImageView) findViewById(

标题上写着什么。首先,我创建了一个从一个图像切换到另一个图像的方法,它可以正常工作。但是,当我添加第二个方法时,为了反转操作并切换回第一个方法,没有任何变化。它甚至不会从第一个图像还原到第二个图像。代码如下:

public void fade(View view) {

    ImageView laxus = (ImageView) findViewById(R.id.laxus_shirt);
    ImageView laxos2 = (ImageView) findViewById(R.id.laxus2);
    laxus.animate().alpha(0f).setDuration(1000);
    laxos2.animate().alpha(1f).setDuration(1000);

}

public void fadetoblack(View view) {

    ImageView laxusius = (ImageView) findViewById(R.id.laxus_shirt);
    ImageView laxosios2 = (ImageView) findViewById(R.id.laxus2);
    laxosios2.animate().alpha(0f).setDuration(1000);
    laxusius.animate().alpha(1f).setDuration(1000);

}

提前谢谢。

您需要调用
start()
来触发动画

laxus.animate().alpha(0f).setDuration(1000).start();
laxos2.animate().alpha(1f).setDuration(1000).start();
我建议您使用Util方法来执行交叉衰落。像下面这样

public static void crossFade(View incomingView, View outGoingView, int outGoingViewVisibility) {
 outGoingView.setAlpha(1);

 ViewCompat.animate(outGoingView).alpha(0).setListener(new ViewPropertyAnimatorListener() {
  @Override
  public void onAnimationStart(View view) {

  }

  @Override
  public void onAnimationEnd(View view) {
   view.setVisibility(outGoingViewVisibility);
  }

  @Override
  public void onAnimationCancel(View view) {

  }
 }).start();

 incomingView.setVisibility(View.VISIBLE);
 incomingView.setAlpha(0);
 ViewCompat.animate(incomingView).setListener(null).alpha(1).start();
}

也许可以选择使用
ViewSwitcher
。 添加一个带有2个图像视图的
视图切换器

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/switcher"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/laxus"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>


                <ImageView
                    android:id="@+id/laxus2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </ViewSwitcher>`
当您想在
imageview
之间切换时,调用方法
viewSwitcher.showNext()
viewSwitcher.showPrevious()
要验证显示的视图,请使用
viewSwitcher.getCurrentView()

    ViewSwitcher viewSwitcher=(ViewSwitcher)findViewById(R.id. switcher); // initiate a ViewSwitcher
    Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load in  animation
    Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load out animation
    viewSwitcher.setInAnimation(in); // set in Animation for ViewSwitcher
    viewSwitcher.setOutAnimation(out); // set out Animation for ViewSwitcher