Tween Animation in Android

Introduction

 
Tween animations are a specific type of animation related to rotation, sliding, and movement of an object. This article explains some common Tween animations.
Now we move to the code part of this article. In this article, we will use a single Activity_main.xml.
 
Code
 
The following is the code of Activity_main.xml,
  1. <RelativeLayout    
  2.     xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:tools="http://schemas.android.com/tools"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent"    
  6.     android:gravity="top"    
  7.     android:paddingBottom="@dimen/activity_vertical_margin"    
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"    
  9.     android:paddingRight="@dimen/activity_horizontal_margin"    
  10.     android:paddingTop="@dimen/activity_vertical_margin"    
  11.     tools:context=".MainActivity" >    
  12.     <ImageView    
  13.         android:id="@+id/imageView1"    
  14.         android:layout_width="wrap_content"    
  15.         android:layout_height="wrap_content"    
  16.         android:layout_alignParentTop="true"    
  17.         android:layout_centerHorizontal="true"    
  18.         android:layout_marginTop="149dp"    
  19.         android:src="@drawable/ic_launcher" />    
  20. </RelativeLayout>   
    Graphical Layout
     
    Graphical Layout
     
    In this activity we use an ImageView. We provide an image source to this ImageView from a drawable folder. We will apply all the animations on this Image. We will apply 6 types of animation, in other words Fade In/Out , Move, Slide Up, Zoom In/Out, Sequential, Clockwise/Anti Clockwise. We provide the option of all 6 animations using Menu. The user can select any specific animation from the menu list.
     
    So if we want to insert some item into the Menu then we should be inserting all the items into the menu.xml file. You can find your menu.xml file as follows: Go to res -> menu -> main.xml.
     
    If this file doesn't exist don't worry. You can create this file manually. Now we insert some item into this menu.xml file.
    1. <menu    
    2.     xmlns:android="http://schemas.android.com/apk/res/android" >    
    3.     <item    
    4.         android:id="@+id/fadeInOut"    
    5.         android:orderInCategory="100"    
    6.         android:title="@string/fade_String"/>    
    7.     <item    
    8.         android:id="@+id/Move"    
    9.         android:orderInCategory="100"    
    10.         android:title="@string/Move"/>    
    11.     <item    
    12.         android:id="@+id/slideUp"    
    13.         android:orderInCategory="100"    
    14.         android:title="@string/Slide"/>    
    15.     <item    
    16.         android:id="@+id/rotate360"    
    17.         android:orderInCategory="100"    
    18.         android:showAsAction="never"    
    19.         android:title="@string/rotate_String"/>    
    20.     <item    
    21.         android:id="@+id/zoomInOut"    
    22.         android:orderInCategory="100"    
    23.         android:title="@string/zoom_In_Out"/>    
    24.     <item    
    25.         android:id="@+id/sequential"    
    26.         android:orderInCategory="100"    
    27.         android:title="@string/Sequential"/>    
    28. </menu>   
      In the preceding code we provide all the options of animation that will be visible in the Menu list of our project. The Android:orderInCategory attribute dictates the order in which the menu items will appear within the menu when it is displayed. The menu items are arranged from left to right in ascending order of integer value. Now we move to the source code of this project.
       
      MainActivity.java file
       
      The following is the source code of the MainActivity.java file of this project,
      1. package com.example.tweenanimation;    
      2.     
      3. import android.os.Bundle;    
      4. import android.app.Activity;    
      5. import android.view.Menu;    
      6. import android.view.MenuItem;    
      7. import android.view.animation.Animation;    
      8. import android.view.animation.AnimationUtils;    
      9. import android.widget.ImageView;    
      10.     
      11. public class MainActivity extends Activity {    
      12.     
      13.     @Override    
      14.     protected void onCreate(Bundle savedInstanceState) {    
      15.         super.onCreate(savedInstanceState);    
      16.         setContentView(R.layout.activity_main);    
      17.     }    
      18.     
      19.     @Override    
      20.     public boolean onCreateOptionsMenu(Menu menu) {    
      21.         // Inflate the menu; this adds items to the action bar if it is present.    
      22.         getMenuInflater().inflate(R.menu.main, menu);    
      23.         return true;    
      24.     }    
      25.     
      26.     public boolean onOptionsItemSelected(MenuItem item) {    
      27.         super.onOptionsItemSelected(item);    
      28.         ImageView image = (ImageView) findViewById(R.id.imageView1);    
      29.         Animation animation;    
      30.         switch (item.getItemId()) {    
      31.     
      32.             case R.id.Move:    
      33.     
      34.                 animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);    
      35.                 image.startAnimation(animation);    
      36.                 return true;    
      37.             case R.id.slideUp:    
      38.     
      39.                 animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);    
      40.                 image.startAnimation(animation);    
      41.                 return true;    
      42.             case R.id.sequential:    
      43.     
      44.                 animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sequential);    
      45.                 image.startAnimation(animation);    
      46.                 return true;    
      47.             case R.id.zoomInOut:    
      48.     
      49.                 animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);    
      50.                 image.startAnimation(animation);    
      51.                 return true;    
      52.             case R.id.rotate360:    
      53.                 animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);    
      54.                 image.startAnimation(animation);    
      55.                 return true;    
      56.             case R.id.fadeInOut:    
      57.     
      58.                 animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);    
      59.                 image.startAnimation(animation);    
      60.                 return true;    
      61.         }    
      62.         return false;    
      63.     }    
      64.     
      65. }    
        Now I will explain the preceding code in the following sections.
         
        Code Section 1
        1. public boolean onCreateOptionsMenu(Menu menu) {    
        2.    // Inflate the menu; this adds items to the action bar if it is present.    
        3.    getMenuInflater().inflate(R.menu.main, menu);    
        4.    return true;    
        5. }   
        In preceding code we created a Menu and inserted the items into the menu in the menu.main file. The getMenuInflater class adds the item into the menu using the inflate method. After insertion of the item present in the main.xml file our menu will look like the following:
         
        menu
        1. public boolean onOptionsItemSelected(MenuItem item);   
        In this code we created an onOptionsItemSelected event for the menu. This event will awake when someone selects an item from the Menu list. When someone selects an option from the Menu list then that item will be saved into the item variable of the MenuItem (item).
        1. ImageView image = (ImageView)findViewById(R.id.imageView1);   
        In the preceding line of code we took the reference of ImageView1 into the image object of the imageview type. Now we will apply all the animation on this object. We will also create an object of Animation Class (animation) and use this object in a code section later.
         
        Code Section 2
        1. case R.id.Move:    
        2.           
        3.      animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);    
        4.      image.startAnimation(animation);    
        5.      return true;   
          This code will execute when someone selects the Move option from the menu. In the preceding code we selected move animation from the animation list (anim) and inserted all the information into the animation object and finally we applied this animation on the image using the startAnimation method. Move animation moves the image from the left to the right as shown below:
           
           
          Code Section 3
          1. case R.id.slideUp:    
          2.     
          3.      animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);    
          4.      image.startAnimation(animation);    
          5.      return true  
            The preceding code executes when someone selects the Slide Up option from the menu. In the preceding code we select Slide Up animation from the animation list (anim) and insert all the information into the animation object and finally we apply this animation on the image using the startAnimation method. This animation slides the image object.
             
             
            Code Section 4
            1. case R.id.sequential:    
            2.     
            3.     animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sequential);    
            4.     image.startAnimation(animation);    
            5.     return true;    
            This code will execute when someone selects the Sequential option from the menu. In the preceding code we selected sequential animation from the animation list (anim) and inserted all the information into an animation object and finally we applied this animation on the image using the startAnimation method. Sequential animation is a combination of various types of animation (in other words move and rotation).
             
             
            Code Section 5
              This code will execute when someone selects the Zoom In/out option from the menu. In the preceding code we selected Zoom in/out animation from the animation list (anim) and inserted all the information into the animation object and finally we appied this animation on the image using the startAnimation method. This animation first zooms in the image and then zooms out the image.
               
               
              Code Section 6
              1. case R.id.rotate360:    
              2.    
              3.      animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);    
              4.      image.startAnimation(animation);    
              5.      return true;   
                This code will execute when someone selects the Clockwise/Anti Clockwise option from the menu. In the preceding code we selected Clockwise/AntiClockwise animation from the animation list (anim) and inserted all the information into the animation object and finally we applied this animation on the image using the startAnimation method. This animation first rotates the image in a clockwise direction and then into an anticlockwise direction by 360 degrees.
                 
                Clockwise
                 
                Code Section 7
                1. case R.id.fadeInOut:    
                2.       
                3.     animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);    
                4.     image.startAnimation(animation);    
                5.     return true;   
                  This code will execute when someone selects the FadeIn/Out option from the menu. In the preceding code we selected FadeIn/Out animation from the animation list (anim) and inserted all the information into an animation object and finally we applied this animation on the image using the startAnimation method. This animation changes the transparency of the image.
                   
                  FadeIn


                  Similar Articles