Android animation between activities

This is another post about animation in Android. In this tip, I am going to show you how to make an animation on the transition between activities. This goal can be achieved using the ActivityOptions class. The ActivityOptions class allows you to specify an animation where the new activity is scaled from a small originating area of the screen to its final full representation or where a thumbnail is scaled from a given position to the new activity window that is being started.

If you want create the animation where the new activity is scaled from a small original area, you need to use the makeScaleUpAnimation(View source, int x,int y, int stardWith, int startHeight) method to create an instance of the ActivityOptions class. This method has five arguments: a source view of the animation, the starting x and y coordinates, and the starting width and height of the small area.

If you want create the animation where the thumbnail is scaled from a given position, you will use the makeThumbnailScaleUpAnimation(View source, Bitmap thumbnail, int x,int y) method. The makeThumbnailScaleUpAnimation accepts four arguments: the source view of the animation, the image to be scaled, the x and y coordinates from which the image is scaled.

Now to have a workable application to demonstrate the methods above, create a new Android project and modify the activity_main.xml file to add a button as shown below. The button on the main activity will be click to open a second activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   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"
  
   >

   <Button
     android:id="@+id/bt"
     android:text="Click to open another activity"
     android:onClick="openActivity"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

</LinearLayout>


Then create another activity called SecondActivity (File->New->Other->Android->Android Activity). In the MainActivity class, you will add the openActivity method to open the second activity when the button is clicked. The animation is set on the transition between the activities.

public void openActivity(View view){
   Intent intent = new Intent(this, SecondActivity.class);
   //ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,0, view.getWidth(), view.getHeight());
   Bitmap thumbnail=BitmapFactory.decodeResource(getResources(), R.drawable.edoclogo);
   ActivityOptions options=ActivityOptions.makeThumbnailScaleUpAnimation(view, thumbnail, 50, 150);  
   startActivity(intent, options.toBundle());
   }

Android-animation-between-acti

Posted by: Dara | post date: 07-04-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.