Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android application demonstrating the usage of Android 4.4 transitions to achieve animations effects.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
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=".MainActivity" >
<LinearLayout
android:id="@+id/source_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="#eee"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" >
<TextView
style="@style/column_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Source" />
</LinearLayout>
<ScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_weight="2"
android:background="#eee" >
<LinearLayout
android:id="@+id/destination_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" >
<TextView
style="@style/column_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Destination" />
</LinearLayout>
</ScrollView>
</LinearLayout>
package com.eclipsesource.overlay;
public class Entry {
private final int imageResId;
private final String title;
private final int id;
public Entry( int imageResId, String title, int id ) {
this.imageResId = imageResId;
this.title = title;
this.id = id;
}
public int getImageResId() {
return imageResId;
}
public String getTitle() {
return title;
}
public int getId() {
return id;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:background="@android:color/background_light"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/entry_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/entry_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
public class ItemClickListener implements OnClickListener {
private class TransitionAdapter implements TransitionListener {
@Override
public void onTransitionStart( Transition transition ) {
}
@Override
public void onTransitionResume( Transition transition ) {
}
@Override
public void onTransitionPause( Transition transition ) {
}
@Override
public void onTransitionEnd( Transition transition ) {
}
@Override
public void onTransitionCancel( Transition transition ) {
}
}
private final Activity activity;
private final Entry entry;
private final ViewGroup rootView;
private final ViewGroup destContainer;
public ItemClickListener( Activity activity, Entry entry ) {
this.activity = activity;
this.entry = entry;
this.rootView = ( ViewGroup )activity.findViewById( R.id.root_layout );
this.destContainer = ( LinearLayout )activity.findViewById( R.id.destination_container );
}
@Override
public void onClick( View v ) {
final ViewGroup entryView = createEntryView();
ChangeBounds changeBounds = new ChangeBounds();
changeBounds.setReparent( true );
changeBounds.addListener( new TransitionAdapter() {
@Override
public void onTransitionEnd( Transition transition ) {
entryView.findViewById( entry.getId() ).setId( -1 );
}
} );
TransitionManager.beginDelayedTransition( rootView, changeBounds );
destContainer.addView( entryView );
}
private ViewGroup createEntryView() {
LayoutInflater layoutInflater = LayoutInflater.from( activity );
ViewGroup entryView = ( ViewGroup )layoutInflater.inflate( R.layout.entry, destContainer, false );
TextView textView = ( TextView )entryView.findViewById( R.id.entry_text );
textView.setText( entry.getTitle() );
ImageView imageView = ( ImageView )entryView.findViewById( R.id.entry_image );
imageView.setImageResource( entry.getImageResId() );
imageView.setId( entry.getId() );
return entryView;
}
}
public class MainActivity extends Activity {
private final ArrayList<Entry> entries;
public MainActivity() {
entries = new ArrayList<Entry>();
entries.add( new Entry( R.drawable.collections_go_to_today, "Calendar", 1 ) );
entries.add( new Entry( R.drawable.content_read, "Envelope", 2 ) );
entries.add( new Entry( R.drawable.location_web_site, "Location", 3 ) );
entries.add( new Entry( R.drawable.social_group, "Social Group", 4 ) );
}
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
createSourceItems();
}
private void createSourceItems() {
LinearLayout sourceContainer = ( LinearLayout )findViewById( R.id.source_container );
for( Entry entry : entries ) {
ImageView srcView = new ImageView( this );
srcView.setImageResource( entry.getImageResId() );
srcView.setId( entry.getId() );
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT );
params.topMargin = getResources().getDimensionPixelSize( R.dimen.activity_vertical_margin );
sourceContainer.addView( srcView, params );
ItemClickListener listener = new ItemClickListener( this, entry );
srcView.setOnClickListener( listener );
}
}
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.main, menu );
return true;
}
@Override
public boolean onMenuItemSelected( int featureId, MenuItem item ) {
boolean result = super.onMenuItemSelected( featureId, item );
if( item.getItemId() == R.id.action_clear ) {
LinearLayout destinationContainer = ( LinearLayout )findViewById( R.id.destination_container );
TransitionManager.beginDelayedTransition( destinationContainer );
int childCount = destinationContainer.getChildCount();
for( int i = 1; i < childCount; i++ ) {
destinationContainer.removeViewAt( 1 );
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment