Video http://www.jikexueyuan.com/course/341.html
activity_main.xml
<FrameLayout 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" android:id="@+id/root" tools:context="com.example.card2d.MainActivity" > <ImageView android:id="@+id/ivA" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/background_1"/> <ImageView android:id="@+id/ivB" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/background_2"/> </FrameLayout>
MainActivity.java
public class MainActivity extends Activity { //declare variable private ImageView imageA; private ImageView imageB; private ScaleAnimation sato0=new ScaleAnimation(1,0,1,1, Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f); private ScaleAnimation sato1=new ScaleAnimation(0,1,1,1, Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); //call the initialization method initView(); //Register listener, anonymous inner class findViewById(R.id.root).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //We should let A execute when the image is clicked //Start the animation and let it load image A //Determine whether imageA is displayed if(imageA.getVisibility()==View.VISIBLE){ //Let imageA load the first animation imageA.startAnimation (sato0); } else{ // let imageB load the second animation imageB.startAnimation (sato0); } } }); } //Show picture A and hide picture B private void showImageA(){ imageA.setVisibility(View.VISIBLE); imageB.setVisibility(View.INVISIBLE); } //Hide picture A and show picture B private void showImageB(){ imageA.setVisibility(View.INVISIBLE); imageB.setVisibility(View.VISIBLE); } //Write an initialization method to let A display private void initView(){ // get object reference imageA=(ImageView)findViewById(R.id.ivA); imageB=(ImageView)findViewById(R.id.ivB); // let image A display showImageA(); //Add 0.5 seconds of execution time to the animation sato0.setDuration(500); sato1.setDuration(500); //Add a listener event to the first animation, and execute the second animation when the first animation is completed sato0.setAnimationListener (new Animation.AnimationListener () //The animation starts to execute trigger @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } //The animation repeats the execution trigger @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } //The animation stops executing and triggers @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub //Listen when the first animation stops, stop it and let it execute the second one / / Determine whether the first animation is displayed if(imageA.getVisibility()==View.VISIBLE){ //Because this is the end animation listener, if the first animation is displayed, //then we need to hide it and then perform the second animation //Clear the first animation imageA.setAnimation(null); // call the second animation showImageB(); //Add animation effect to showB at the same time imageB.startAnimation (sato1); } else{//If the second animation is being executed //Clear the second animation imageB.setAnimation(null); //call the first animation showImageA(); //Add animation effect to showA at the same time imageA.startAnimation (sato1); } } }); } }