Android ViewFlipper Animation example

ViewFlipper is a subclass of ViewAnimator that makes it easy to animate a bunch of views that are added to it. It only displays one child view at a time and you can also set it to rotate the views automatically. Just keep in mind that if you have duration set for your animation alpha then the flipper duration should be greater than that otherwise the view will change even before it completely comes into focus.

Android ViewFlipper Animation example Android ViewFlipper new View Android ViewFlipper auto flip

String Resource - strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ViewFlipper</string>
    <string name="menu_settings">Settings</string>

    <color name="bisque">#ffe4c4</color>
    <color name="snow">#cdc9c9</color>
    <color name="honeydew">#f0fff0</color>
    
</resources>

Animation Resource - slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="-100%" android:toXDelta="0%"/>
    <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

Animation Resource - slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="-100%" android:toXDelta="0%"/>
    <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

Animation Resource - slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/>
    <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

Animation Resource - slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="100%"/>
    <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

Layout Resource for the Main Activity

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <RelativeLayout android:id="@+id/topLayout"
  android:layout_width="match_parent" android:layout_height="48dp"
  android:layout_alignParentTop="true">

  <View android:layout_width="match_parent" android:layout_height="2dp"
   android:layout_alignParentBottom="true" android:layout_marginLeft="3dp"
   android:layout_marginRight="3dp" android:background="?android:attr/dividerVertical" />

  
  <Button android:id="@+id/previousView" android:layout_width="wrap_content"
   android:layout_height="wrap_content" android:layout_alignParentLeft="true"
   android:layout_alignParentTop="true" 
   android:text="Previous" android:layout_alignParentBottom="true" />

  <View
      android:id="@+id/viewDivider1"
      android:layout_width="2dp"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentTop="true"
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_toRightOf="@id/previousView"
      android:background="?android:attr/dividerVertical" />
  
  <Button android:id="@+id/nextView" android:layout_width="wrap_content"
   android:layout_height="match_parent" android:layout_alignParentRight="true"
   android:layout_alignParentTop="true"
   android:text="Next" android:layout_alignParentBottom="true" />
  
  <View
      android:id="@+id/viewDivider2"
      android:layout_width="2dp"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_alignParentTop="true"
      android:layout_marginBottom="3dp"
      android:layout_marginTop="3dp"
      android:layout_toLeftOf="@id/nextView"
      android:background="?android:attr/dividerVertical" />
  
  <ToggleButton
      android:id="@+id/toggleButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="ToggleButton" 
      android:textOff="Start Flipper"
         android:textOn="Stop Flipper" 
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true" 
         android:layout_toRightOf="@+id/viewDivider1" 
         android:layout_toLeftOf="@+id/viewDivider2"/>
  
  

 </RelativeLayout>
 
    <ViewFlipper
        android:id="@+id/myViewFlipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/topLayout">

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@color/bisque" />
    
    <AnalogClock
        android:id="@+id/analogClock2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@color/honeydew" />
    
    <AnalogClock
        android:id="@+id/analogClock3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:background="@color/snow" />
    
    </ViewFlipper>
    
</RelativeLayout>

Main Activity

package com.as400samplecode;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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.ToggleButton;
import android.widget.ViewFlipper;

public class MainActivity extends Activity implements OnClickListener{

 private ToggleButton myToggleButton;
 private Button previousView, nextView;
 private Animation slide_in_left, slide_in_right, slide_out_left, slide_out_right;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //get references to our buttons and toogle button
        myToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        previousView = (Button) findViewById(R.id.previousView);
        nextView = (Button) findViewById(R.id.nextView);
        
        //attach onClick listeners to the buttons
        myToggleButton.setOnClickListener(this);
        previousView.setOnClickListener(this);
        nextView.setOnClickListener(this);
        
        //create animations
        slide_in_left = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
        slide_in_right = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
        slide_out_left = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);
        slide_out_right = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

 @Override
 public void onClick(View v) {
  
  //get reference to the view flipper
  ViewFlipper myViewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
  //set the animation for the view that enters the screen
  myViewFlipper.setInAnimation(slide_in_right);
  //set the animation for the view leaving th screen
  myViewFlipper.setOutAnimation(slide_out_left);
  
  switch (v.getId()) {
  case R.id.nextView:
   //show the next view
   myViewFlipper.showNext();
   break;
   
  case R.id.previousView:
   //show the next view
   myViewFlipper.setInAnimation(slide_in_left);
   myViewFlipper.setOutAnimation(slide_out_right);
   myViewFlipper.showNext();
   break;
   
  case R.id.toggleButton:
   
   if(myToggleButton.isChecked()){
    //set flipper interval
    myViewFlipper.setFlipInterval(6000);
    //start flipping the views
    myViewFlipper.startFlipping();
         }
         else{
          //stop flipping the views
          myViewFlipper.stopFlipping();
         }
   break;
   
  }
 }
    
}

Reference


15 comments :

  1. So stop pacing the aisles cheap fifa 14 coins and counting the miles. Instead, climb more fifa 14 coins mountains, eat more ice creams, go barefoot more often, swim more rivers, watch more sunsets, laugh more and cry less. Life must be lived as we go along. Then the station will come soon cheap fifa 14 coins enough.

    ReplyDelete
  2. goog explnation.
    http://appsdevelopment-for-mobiles.blogspot.com/

    ReplyDelete
  3. Hi, you might want to fix the animation xml names as you have the file
    slide_in_left.xml twice!

    ReplyDelete
  4. Thanks for giving me the useful information. I think I need it!

    - Happy Wheels
    - FNAF World
    - FNAF

    ReplyDelete
  5. We are offering website design or E-Commerce website in affordable price.......

    worpress plugins

    ReplyDelete
  6. Let’s keep out sites for your child! click:
    brain games | puzzle games | tetris | happy wheels | agario | abcya | fnaf 4 | super mario games
    To play for free!

    ReplyDelete
  7. Hi! Thanks for the great information you have provided! You have touched on crucuial points!
    * google mail

    ReplyDelete
  8. The game exciting and adventurous on our site.
    tank trouble | tank trouble 2 | io games | slitherio | learn to fly | learn to fly 2

    If you love PvP style games, then you will surely spend hours of fun playing this game!
    strike force heroes

    strike force heroes 4

    ReplyDelete
  9. thank your contributions are recorded in the article! I learned a lot.
    thanks for the article, I'm always follow you!

    ReplyDelete
  10. Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!
    wings.io | minecraft 2 | wingsio game geometry dash

    ReplyDelete
  11. This is such a great resource that you are providing and you give it away for free. I love
    seeing that understand the value of providing a quality resource for free. :) :)
    bloons tower defense 5

    ReplyDelete
  12. Thanks for your post! it contains quite a lot of things to learn! it's great that I known this site!
    geometry dash | facebook sign up

    ReplyDelete