• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> Coding Tips: How to Use Custom Transition Animation in Android

Coding Tips: How to Use Custom Transition Animation in Android

September 29, 2011 by Lorensius Londa 5 Comments

Android has a set of Animation API that makes you easily create custom animations in your Android application. You can create animations programmatically via Java code or defined them in xml files inside /res/anim folder. Using your custom animation, you can override the default transition animation between activities to make your app looks more elegant. Overriding the default transition animation is easy, just create two custom animations and call the overridePendingTransition method of Activity class to use them.

How to use custom transition animation:

  • Create two animation files (xml), one for incoming activity (/anim/incoming.xml) and one for outgoing activity (/anim/outgoing.xml).
    Android Transition Animationincoming.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    	<translate
    		android:interpolator="@android:anim/accelerate_interpolator"
            android:fromXDelta="0"
            android:toXDelta="0"
            android:duration="300"/>
    
        <scale
    	    android:pivotX="50%"
    	    android:pivotY="50%"
    	    android:fromXScale="0"
    	    android:fromYScale="0"
    	    android:toXScale="1.0"
    	    android:toYScale="1.0"
    	    android:startOffset="300"
    	    android:duration="300" />
    </set>
    

    outgoing.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
    	<translate
            android:fromXDelta="0"
            android:toXDelta="-50%p"
            android:duration="300" />
    
        <scale
    	    android:pivotX="50%"
    	    android:pivotY="50%"
    	    android:fromXScale="1.0"
    	    android:fromYScale="1.0"
    	    android:toXScale=".1"
    	    android:toYScale=".1"
    	    android:startOffset="300"
    	    android:duration="400" />
    </set>
    
  • Call method overridePendingTransition (int enterAnim, int exitAnim) right after startActivity(intent) to override default transition animation between current activity and next activity.
    Intent intent = new Intent(this, NextActivity.class);
    
    startActivity(intent);
    overridePendingTransition (R.anim.incoming, R.anim.outgoing)
    
  • To use custom transition animation between current activity and previous activity (when user press the back key to get back to previous activity) override the onBackPressed () method and call the overridePendingTransition (int enterAnim, int exitAnim) method. 
    @Override
    public void onBackPressed() {
      super.onBackPressed();
      overridePendingTransition(R.anim.customanim1, R.anim.customanim2)
    }
    
Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. Coding Tips: How to Use Custom Font in Android
  2. Android Coding Tips: How to Create Options Menu on Child Activity inside an ActivityGroup
  3. How to Create Custom Window Title in Android
  4. Android Tips: How to Place Image or Logo at The Center of Actionbar

Filed Under: Android, Programming Tagged With: Android, custom animation, pending transition, transition animation

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Sidiq Permana says

    October 6, 2011 at 2:26 pm

    Great mas.. berarti kita bisa ngasih control anim yang serupa di widget kan mas?

    Reply
    • lorenz says

      October 9, 2011 at 11:03 am

      Maksudnya gimana diapply ke widget ?

      Reply
  2. andy says

    March 18, 2012 at 12:18 am

    hi, I want to know how to do this in java code ? thank you !

    Reply
  3. Hemant says

    November 3, 2012 at 12:42 pm

    It’s great thing for the beginners.

    Thanks a lot

    Reply
  4. Lukasz Dynokwski says

    December 14, 2012 at 7:23 pm

    Simple and great. Thanks !

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 355 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg359 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in