Sunday, November 3, 2013

Example of ViewAnimator

android.widget.ViewAnimator is a subclass of FrameLayout container that will perform animations when switching between its views.



<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=".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="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/prev"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="previous" />

        <Button
            android:id="@+id/next"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="next" />
    </LinearLayout>

    <ViewAnimator
        android:id="@+id/viewanimator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView 1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="- Button 4 -" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LinearLayout 4" />
        </LinearLayout>
    </ViewAnimator>

</LinearLayout>


package com.example.androidviewanimator;

import android.os.Bundle;
import android.app.Activity;
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.ViewAnimator;

public class MainActivity extends Activity {
 
 Button buttonPrev, buttonNext;
 ViewAnimator viewAnimator;
 
 Animation slide_in_left, slide_out_right;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  buttonPrev = (Button)findViewById(R.id.prev);
  buttonNext = (Button)findViewById(R.id.next);
  viewAnimator = (ViewAnimator)findViewById(R.id.viewanimator);
  
  slide_in_left = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
  slide_out_right = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
  
  viewAnimator.setInAnimation(slide_in_left);
  viewAnimator.setOutAnimation(slide_out_right);
  
  buttonPrev.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    viewAnimator.showPrevious();
   }});
  
  buttonNext.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    viewAnimator.showNext();
   }});
 }

}





more related example:

3 comments:

vestanbul said...

great post. thank you.

Thiago said...

Thanks for sharing. This post is simple and useful

GS Chaudhary said...

Simply cool.. !