androidcross-fade

Best way to cross-fade ImageViews


I am working on this project for my class.
You can click an image of Bart Simpson and it fades out while Homer Simpson fades in.

What I need help with is to make it possible to click Homer and return to Bart.

My code is

Activity_xml

   <?xml version="1.0" encoding="utf-8"?>
   <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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eric.layoutdemo2.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/bart"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:src="@drawable/bart"
        android:onClick="fade"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/homer"
        android:layout_alignTop="@+id/bart"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:alpha="0"
        android:src="@drawable/homer"
       />
     </RelativeLayout>

Main Activity

    import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.ImageView;


   public class MainActivity extends ActionBarActivity {

       public void fade(View view) {

        ImageView bart = (ImageView) findViewById(R.id.bart);

        ImageView homer = (ImageView) findViewById(R.id.homer);


        bart.animate().alpha(0f).setDuration(2000);

        homer.animate().alpha(1f).setDuration(2000);
        bart.bringToFront();
    }

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




    }

Solution

  • Simple, you have to define an OnClickListener to homer object and when it triggered call fade function.

    public class MainActivity extends ActionBarActivity implements View.OnClickListener{
        ImageView homer, bart;
    
        private void fade(ImageView v1, ImageView v2) {
            v1.animate().alpha(0f).setDuration(2000);
            v2.animate().alpha(1f).setDuration(2000);
            v2.bringToFront();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            bart = (ImageView) findViewById(R.id.bart);
            homer = (ImageView) findViewById(R.id.homer);
    
            bart.setOnClickListener(this);
            homer.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v){
            switch(v.getId()){
                case R.id.bart:
                    fade(bart, homer);
                         break;
    
                case R.id.homer:
                    fade(homer, bart);
                         break;
            }
        }
    }