Showing posts with label Android code sample: Animation. Show all posts
Showing posts with label Android code sample: Animation. Show all posts

Tuesday, December 16, 2014

Swipe to Slide animated activity switching

Last post show how to detect Swipe to start another activity. In this post, animation of Slide-in/Slide-out are added in switching activity.


Create xml files of animation in /res/anim/ folder.

/res/anim/slide_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

</set>

/res/anim/slide_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

</set>

/res/anim/slide_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%p" />

</set>

/res/anim/slide_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />

</set>

Modify onFling() of MyGestureListener of MainActivity.java in last post, to call overridePendingTransition() after startActivity().
        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2, 
                float velocityX, float velocityY) {
            
         /*
         Toast.makeText(getBaseContext(), 
          event1.toString() + "\n\n" +event2.toString(), 
          Toast.LENGTH_SHORT).show();
         */
         
         if(event2.getX() < event1.getX()){
          Toast.makeText(getBaseContext(), 
           "Swipe left - startActivity()", 
           Toast.LENGTH_SHORT).show();
          
          //switch another activity
             Intent intent = new Intent(
               MainActivity.this, SecondActivity.class);
             startActivity(intent);
             overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
         }

            return true;
        }

Modify onFling() of My2ndGestureListener of SecondActivity.java in last post, to call overridePendingTransition() after finish().
        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2, 
                float velocityX, float velocityY) {
            
         /*
         Toast.makeText(getBaseContext(), 
          event1.toString() + "\n\n" +event2.toString(), 
          Toast.LENGTH_SHORT).show();
         */
         
         if(event2.getX() > event1.getX()){
          Toast.makeText(getBaseContext(), 
           "Swipe right - finish()", 
           Toast.LENGTH_SHORT).show();
          
          finish();
          overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
         }

            return true;
        }

For /res/layout/activity_main.xml and AndroidManifest.xml refer to last post.

download filesDownload the files.

Thursday, September 18, 2014

Apply animation on buttons to start activity

Example to apply Animation on Buttons, and implement AnimationListener to startActivity() when onAnimationEnd() called. Notice that when a animation is running, the buttons still can accept click to start other animation, and create more AnimationListener to start more than one second activity when ended. (It's further work on former exercise "Apply animation on Button")


Create animation xml in /res/anim folder.

/res/anim/anim_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <alpha 
        android:fromAlpha="1.0" 
        android:toAlpha="0.1" 
        android:duration="500" 
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>

/res/anim/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="500"
        android:startOffset="0"
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>

/res/anim/anim_scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="3.0"
        android:fromYScale="1.0"
        android:toYScale="3.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500" 
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>

/res/anim/anim_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="500"
        android:repeatCount="1"
        android:repeatMode="reverse"/>
</set>

/res/layout/main.xml, layout of main activity(AndroidAnimButtonsActivity.java).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/translate"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Translate" />
    <Button
        android:id="@+id/alpha"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Alpha" />
    <Button
        android:id="@+id/scale"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Scale" />
    <Button
        android:id="@+id/rotate"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Rotate" />

</LinearLayout>

AndroidAnimButtonsActivity.java
package com.exercise.AndroidAnimButtons;

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

public class AndroidAnimButtonsActivity extends Activity {
 
 static String KEY_ANIM = "TARGET_ANIM";
 static String Target_Translate = "Translate";
 static String Target_Alpha = "Alpha";
 static String Target_Scale = "Scale";
 static String Target_Rotate = "Rotate";
 String target_op = Target_Translate; //dummy default 
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Animation animTranslate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
        final Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        final Animation animScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        
        Button btnTranslate = (Button)findViewById(R.id.translate);
        Button btnAlpha = (Button)findViewById(R.id.alpha);
        Button btnScale = (Button)findViewById(R.id.scale);
        Button btnRotate = (Button)findViewById(R.id.rotate);
        
        btnTranslate.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    target_op = Target_Translate;
    arg0.startAnimation(animTranslate);
   }});
        
        btnAlpha.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    target_op = Target_Alpha;
    arg0.startAnimation(animAlpha);
   }});
        
        btnScale.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    target_op = Target_Scale;
    arg0.startAnimation(animScale);
   }});
        
        btnRotate.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    target_op = Target_Rotate;
    arg0.startAnimation(animRotate);
   }});
        
        animTranslate.setAnimationListener(animationListener);
        animAlpha.setAnimationListener(animationListener);
        animScale.setAnimationListener(animationListener);
        animRotate.setAnimationListener(animationListener);
    }
    
    AnimationListener animationListener = new AnimationListener(){

  @Override
  public void onAnimationStart(Animation animation) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onAnimationEnd(Animation animation) {
   Intent intent = new Intent(
     AndroidAnimButtonsActivity.this, 
     SecondActivity.class);
        intent.putExtra(KEY_ANIM, target_op);
        startActivity(intent);
  }

  @Override
  public void onAnimationRepeat(Animation animation) {
   // TODO Auto-generated method stub
   
  }};
}

/res/layout/second.xml, layout of the second activity(SecondActivity.java).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/anbutton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="Translate" />

</LinearLayout>

SecondActivity.java
package com.exercise.AndroidAnimButtons;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class SecondActivity extends Activity {

 Animation anim;
 Button anButton;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.second);
  Intent intent = getIntent();
  String op = intent.getStringExtra(AndroidAnimButtonsActivity.KEY_ANIM);
  if (op.equals(AndroidAnimButtonsActivity.Target_Translate)){
   anim = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.anim_translate);
  }else if (op.equals(AndroidAnimButtonsActivity.Target_Alpha)){
   anim = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.anim_alpha);
  }else if (op.equals(AndroidAnimButtonsActivity.Target_Scale)){
   anim = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.anim_scale);
  }else if (op.equals(AndroidAnimButtonsActivity.Target_Rotate)){
   anim = AnimationUtils.loadAnimation(SecondActivity.this, R.anim.anim_rotate);
  }
  
  anButton = (Button)findViewById(R.id.anbutton);
  anButton.setText(op);
  
  anButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){

   @Override
   public void onGlobalLayout() {

    anButton.startAnimation(anim);
    
    //deprecated in API level 16
    anButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
       //for API Level >= 16
       //anImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
   }});
  
  anButton.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    anButton.startAnimation(anim);
   }});
 }

}

Modify AndroidManifest.xml to add SecondActivity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exercise.AndroidAnimButtons"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidAnimButtonsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>


download filesDownload the files.


Tuesday, August 5, 2014

Animation translate both X and Y

This example show how to implement animation of translate on both X(horizontal) and Y(vertical) together.


Create XML files slide_down.xml and slide_down2.xml under the folder /res/anim/, show two approachs to define translate on X and Y.

slide_down.xml, One <Set> have two <translate>, for X and Y separately.
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:fromYDelta="0"
        android:toYDelta="100%p" />
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p" />

</set>

slide_down2.xml, One <Set> have one <translate>, have X and Y together.
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100%p"
        android:toYDelta="100%p" />
    
</set>

/res/layout/activity_main.xml, to have two LinearLayouts and ImageViews to contain the animation.
<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: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="com.example.androidslideanimation.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="#D0D0D0" >
        
        <LinearLayout
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="fill_parent"
         android:orientation="vertical"
         android:background="#C0C0C0" >
         
            <ImageView
             android:id="@+id/imagea"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@drawable/ic_launcher"
             android:background="#A0A0A0" />
            
        </LinearLayout>
        
        <LinearLayout
         android:layout_width="0dp"
         android:layout_weight="2"
         android:layout_height="fill_parent"
         android:orientation="vertical"
         android:background="#B0B0B0" >
         
            <ImageView
             android:id="@+id/imageb"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@drawable/ic_launcher"
             android:background="#A0A0A0" />
            
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

MainActivity.java
package com.example.androidslideanimation;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

 ImageView imageA, imageB;
 Animation animationASlideDown, animationBSlideDown;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  imageA = (ImageView) findViewById(R.id.imagea);
  imageB = (ImageView) findViewById(R.id.imageb);

  animationASlideDown = AnimationUtils.loadAnimation(this,
    R.anim.slide_down);
  animationASlideDown.setDuration(3000);
  animationASlideDown.setAnimationListener(animationASlideDownListener);
  imageA.startAnimation(animationASlideDown);
  
  animationBSlideDown = AnimationUtils.loadAnimation(this,
    R.anim.slide_down2);
  animationBSlideDown.setDuration(3000);
  animationBSlideDown.setAnimationListener(animationBSlideDownListener);
  imageB.startAnimation(animationBSlideDown);

 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  imageA.clearAnimation();
  imageB.clearAnimation();
 }
 
 AnimationListener animationASlideDownListener = new AnimationListener() {

  @Override
  public void onAnimationStart(Animation animation) {}

  @Override
  public void onAnimationEnd(Animation animation) {
   imageA.startAnimation(animationASlideDown);
  }

  @Override
  public void onAnimationRepeat(Animation animation) {}
 };
 
 AnimationListener animationBSlideDownListener = new AnimationListener() {

  @Override
  public void onAnimationStart(Animation animation) {}

  @Override
  public void onAnimationEnd(Animation animation) {
   imageB.startAnimation(animationBSlideDown);
  }

  @Override
  public void onAnimationRepeat(Animation animation) {}
 };

}


download filesDownload the files.

Monday, August 4, 2014

Animation of sliding horizontal and vertical

I have a old example of "Implement slide-in and slide-out animation", slide horizontally using Android provided android.R.anim.slide_in_left and android.R.anim.slide_out_right. It's modified to implement vertical slide down animation.



Create /res/anim/slide_down.xml to define our vertical animation.
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:fromYDelta="0"
        android:toYDelta="100%p" />

</set>


/res/layout/activity_main.xml, to add extra ImageView.
<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: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="com.example.androidslideanimation.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/image2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/image3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:visibility="invisible" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="#D0D0D0" >

        <ImageView
            android:id="@+id/image4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:background="#B0B0B0" />

    </LinearLayout>

</LinearLayout>


MainActivity.java
package com.example.androidslideanimation;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

 ImageView image1, image2, image3;
 Animation animationSlideInLeft, animationSlideOutRight;
 ImageView curSlidingImage;
 
 ImageView image4;
 Animation animationSlideDown;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  image1 = (ImageView) findViewById(R.id.image1);
  image2 = (ImageView) findViewById(R.id.image2);
  image3 = (ImageView) findViewById(R.id.image3);

  animationSlideInLeft = AnimationUtils.loadAnimation(this,
    android.R.anim.slide_in_left);
  animationSlideOutRight = AnimationUtils.loadAnimation(this,
    android.R.anim.slide_out_right);
  animationSlideInLeft.setDuration(1000);
  animationSlideOutRight.setDuration(1000);
  animationSlideInLeft.setAnimationListener(animationSlideInLeftListener);
  animationSlideOutRight
    .setAnimationListener(animationSlideOutRightListener);

  curSlidingImage = image1;
  image1.startAnimation(animationSlideInLeft);
  image1.setVisibility(View.VISIBLE);
  
  //Vertical slide on image4
  image4 = (ImageView) findViewById(R.id.image4);

  animationSlideDown = AnimationUtils.loadAnimation(this,
    R.anim.slide_down);
  animationSlideDown.setDuration(3000);
  animationSlideDown.setAnimationListener(animationSlideDownListener);
  
  image4.startAnimation(animationSlideDown);

 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  image1.clearAnimation();
  image2.clearAnimation();
  image3.clearAnimation();
  image4.clearAnimation();
 }
 
 AnimationListener animationSlideDownListener = new AnimationListener() {

  @Override
  public void onAnimationStart(Animation animation) {}

  @Override
  public void onAnimationEnd(Animation animation) {
   image4.startAnimation(animationSlideDown);
  }

  @Override
  public void onAnimationRepeat(Animation animation) {}};

 AnimationListener animationSlideInLeftListener = new AnimationListener() {

  @Override
  public void onAnimationEnd(Animation animation) {
   // TODO Auto-generated method stub

   if (curSlidingImage == image1) {
    image1.startAnimation(animationSlideOutRight);
   } else if (curSlidingImage == image2) {
    image2.startAnimation(animationSlideOutRight);
   } else if (curSlidingImage == image3) {
    image3.startAnimation(animationSlideOutRight);
   }
  }

  @Override
  public void onAnimationRepeat(Animation animation) {}

  @Override
  public void onAnimationStart(Animation animation) {}
 };

 AnimationListener animationSlideOutRightListener = new AnimationListener() {
  @Override
  public void onAnimationEnd(Animation animation) {
   // TODO Auto-generated method stub
   if (curSlidingImage == image1) {
    curSlidingImage = image2;
    image2.startAnimation(animationSlideInLeft);
    image1.setVisibility(View.INVISIBLE);
    image2.setVisibility(View.VISIBLE);
    image3.setVisibility(View.INVISIBLE);
   } else if (curSlidingImage == image2) {
    curSlidingImage = image3;
    image3.startAnimation(animationSlideInLeft);
    image1.setVisibility(View.INVISIBLE);
    image2.setVisibility(View.INVISIBLE);
    image3.setVisibility(View.VISIBLE);
   } else if (curSlidingImage == image3) {
    curSlidingImage = image1;
    image1.startAnimation(animationSlideInLeft);
    image1.setVisibility(View.VISIBLE);
    image2.setVisibility(View.INVISIBLE);
    image3.setVisibility(View.INVISIBLE);
   }
  }

  @Override
  public void onAnimationRepeat(Animation animation) {}

  @Override
  public void onAnimationStart(Animation animation) {}
 };
}


download filesDownload the files.



I fail to set RepeatMode and RepeatCount to make it animate repeatly by using Java code or XML, so I implement AnimationListener to restart when onAnimationEnd is called.