Thursday, January 26, 2012

Implement Fade-In transition effect







- Download and copy the graphs to /res/drawable.









Create /res/anim/fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android" />
<item android:drawable="@drawable/developer" />
</transition>


Modify main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/starttransition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Transition" />
<ImageView
android:id="@+id/mytransition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@anim/fadein" />
</LinearLayout>


AndroidFadeInFadeOutActivity.java
package com.exercise.AndroidFadeInFadeOut;

import android.app.Activity;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidFadeInFadeOutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView myImage = (ImageView)findViewById(R.id.mytransition);
final TransitionDrawable myTransitionDrawable = (TransitionDrawable)myImage.getDrawable();

Button startTransition = (Button)findViewById(R.id.starttransition);
startTransition.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myTransitionDrawable.startTransition(1000);
}});
}
}


Download the files.

next:
- Implement Fade-Out transition effect using TransitionDrawable.reverseTransition()

1 comment:

Gagandeep Singh said...

Hi, Thanks for this article.. but where exactly is this "transition" tag.. I could not find even when If I change my compileSDKVersion to 23. I looked around on Google Docs.. and found out that this tag is only used to define custom transitions but this is from the context of "Scenes and Transitions" ...

I can't run your anim xml file.. because of unresolved transition tag
Thanks!