Save this file transition.xml
in res/drawable
folder of your project.
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image1"/>
<item android:drawable="@drawable/image2"/>
</transition>
The image1 and image2 are the two images that we want to transition and they should be put in your res/drawable
folder too.
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image1"/>
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image_view);
transitionDrawable = (TransitionDrawable)
ContextCompat.getDrawable(this, R.drawable.transition);
birdImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
birdImageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(1000);
}
});
}