Thursday, April 25, 2013

Start Animation in Activity start

Last exercise show how to "Implement Animation while switching Activity, using overridePendingTransition()", after startActivity() or finish(). If you want run animation when your activity start, you can startAnimation() with your custom Animation in onCreate() or onResume() depends on what you expect.


Modify from last exercise.

Create /res/anim/rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/linear_interpolator">
   <rotate
       android:fromDegrees="0"
       android:toDegrees="360"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="1000"
       android:startOffset="0"
       android:repeatCount="1"
       android:repeatMode="reverse" />
</set>


Modify activity_main.xml to add more view to animate.
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/screen"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/switchbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch Activity2" />
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <ImageView
        android:id="@+id/bigicon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />
        
</LinearLayout>


MainActivity.java
package com.example.androidactivityanimation;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
 
 LinearLayout screen;
 Button switchButton;
 ImageView bigIcon;
 ImageView icon;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.activity_main);
  
  screen = (LinearLayout)findViewById(R.id.screen);
  bigIcon = (ImageView)findViewById(R.id.bigicon);
  icon = (ImageView)findViewById(R.id.icon);
  
  switchButton = (Button)findViewById(R.id.switchbutton);
  switchButton.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, Activity2.class);
    startActivity(intent);
    overridePendingTransition(R.anim.right_in, R.anim.left_out);
   }});
  
  Animation animRightIn = AnimationUtils.loadAnimation(this, R.anim.right_in);
  Animation animRotateIn_icon = AnimationUtils.loadAnimation(this, R.anim.rotate);
  
  screen.startAnimation(animRightIn);
  icon.startAnimation(animRotateIn_icon);
 }

 @Override
 protected void onResume() {
  super.onResume();
  Animation animRotateIn_big = AnimationUtils.loadAnimation(this, R.anim.rotate);
  bigIcon.startAnimation(animRotateIn_big);
 }

}



download filesDownload the files.

1 comment:

Unknown said...

Very useful thank you!