Thursday, 9 May 2013

Transition Drawable In Android


MainActivity.java

 public class MainActivity extends Activity {

private ImageView image;
private TransitionDrawable trans;

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

    image = (ImageView) findViewById(R.id.image);
    Resources res = this.getResources();
    trans = (TransitionDrawable) res.getDrawable(R.drawable.transition);

    image.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            image.setImageDrawable(trans);
            trans.reverseTransition(1000);
        }
    });
}  }



activity_main.xml
<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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
        />
    
</LinearLayout>
transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/ic_launcher"/>
    <item android:drawable="@drawable/hair_advice"/>

</transition>

No comments:

Post a Comment