Reputation: 83
What the title says. At first, I created a method to swap from one image to the other one, and it works. When I add the 2nd method though, to reverse the action and swap back to the first one, nothing changes. It does not even revert from the first image to the second one. Below is the code:
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);
}
Thank you in advance.
Upvotes: 0
Views: 48
Reputation: 429
Maybe it will be an option to use ViewSwitcher
.
Add a viewSwitcher
with 2 ImageViews.
<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>`
On Activity onCreate
add code
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
When you want to switch between ImageViews
call method viewSwitcher.showNext();
or viewSwitcher.showPrevious();
To verify which view is displayed use viewSwitcher.getCurrentView();
Upvotes: 0
Reputation: 28093
You need to call start()
to trigger the animation.
laxus.animate().alpha(0f).setDuration(1000).start();
laxos2.animate().alpha(1f).setDuration(1000).start();
I would suggest you to use Util method to perform crossfading. Something like below
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();
}
Upvotes: 1