Showing posts with label animation. Show all posts
Showing posts with label animation. Show all posts

Monday, May 14, 2012

TransitionAnimationExample


TransitionAnimationExample


This example explains how we can create transition effect to animate our photo gallery or screen.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it TransitionAnimationExample.
2.) Write following into main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/container"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
    <ListView
       android:id="@android:id/list"
       android:persistentDrawingCache="animation|scrolling"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layoutAnimation="@anim/layout_bottom_to_top_slide" />
    <ImageView
       android:id="@+id/picture"
       android:scaleType="fitCenter"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:visibility="gone" />
</FrameLayout>
3.) Create res/anim folder and add following files into it: layout_bottom_to_top_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
       android:delay="30%"
       android:animationOrder="reverse"
       android:animation="@anim/slide_right" />
slide_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
           android:duration="@android:integer/config_shortAnimTime" />
</set>
4.) Create Rotate3dAnimation.java in src folder and write following code:
package com.example.TransitionAnimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
public class Rotate3dAnimation extends Animation {
    private final float mFromDegrees;
    private final float mToDegrees;
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean mReverse;
    private Camera mCamera;
    public Rotate3dAnimation(float fromDegrees, float toDegrees,
        float centerX, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
    }
    @Override
    public void initialize(int width, int height, int parentWidth, intparentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t){
        final float fromDegrees = mFromDegrees;
        float degrees = fromDegrees + ((mToDegrees - fromDegrees) *interpolatedTime);
        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;
        final Matrix matrix = t.getMatrix();
        camera.save();
        if (mReverse) {
            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
        }
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}
5.) Add 2 png images in drawable folder.
6.) Run for output.
Steps:
1.) Create a project named TransitionAnimationExample and set the information as stated in the image.
Build Target: Android 2.1
Application Name: TransitionAnimationExample
Package Name: com.example. TransitionAnimation
Activity Name: TransitionAnimationActivity
Min SDK Version: 7
2.) Open TransitionAnimationActivity.java file and write following code there:
package com.example.TransitionAnimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class TransitionAnimationActivity extends Activity implements
        AdapterView.OnItemClickListenerView.OnClickListener {
    private ListView mPhotosList;
    private ViewGroup mContainer;
    private ImageView mImageView;
    private static final String[] PHOTOS_NAMES = new String[] {
            "Photo1",
            "Photo2",
    };
    private static final int[] PHOTOS_RESOURCES = new int[] {
            R.drawable.thumb,
            R.drawable.icon,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPhotosList = (ListView) findViewById(android.R.id.list);
        mImageView = (ImageView) findViewById(R.id.picture);
        mContainer = (ViewGroup) findViewById(R.id.container);
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, PHOTOS_NAMES);
        mPhotosList.setAdapter(adapter);
        mPhotosList.setOnItemClickListener(this);
        mImageView.setClickable(true);
        mImageView.setFocusable(true);
        mImageView.setOnClickListener(this);
        mContainer.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
    }
    private void applyRotation(int position, float start, float end) {
        // Find the center of the container
        final float centerX = mContainer.getWidth() / 2.0f;
        final float centerY = mContainer.getHeight() / 2.0f;
        final Rotate3dAnimation rotation =
        new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
        rotation.setDuration(500);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(position));
        mContainer.startAnimation(rotation);
    }
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        mImageView.setImageResource(PHOTOS_RESOURCES[position]);
        applyRotation(position, 0, 90);
    }
    public void onClick(View v) {
        applyRotation(-1, 180, 90);
    }
    private final class DisplayNextView implements Animation.AnimationListener {
        private final int mPosition;
        private DisplayNextView(int position) {
            mPosition = position;
        }
        public void onAnimationStart(Animation animation) {
        }
        public void onAnimationEnd(Animation animation) {
            mContainer.post(new SwapViews(mPosition));
        }
        public void onAnimationRepeat(Animation animation) {
        }
    }
    private final class SwapViews implements Runnable {
        private final int mPosition;
        public SwapViews(int position) {
            mPosition = position;
        }
        public void run() {
            final float centerX = mContainer.getWidth() / 2.0f;
            final float centerY = mContainer.getHeight() / 2.0f;
            Rotate3dAnimation rotation;
            if (mPosition > -1) {
                mPhotosList.setVisibility(View.GONE);
                mImageView.setVisibility(View.VISIBLE);
                mImageView.requestFocus();
                rotation = new Rotate3dAnimation(90, 180, centerX, centerY, 310.0f, false);
            } else {
                mImageView.setVisibility(View.GONE);
                mPhotosList.setVisibility(View.VISIBLE);
                mPhotosList.requestFocus();
                rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f,false);
            }
            rotation.setDuration(500);
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());
            mContainer.startAnimation(rotation);
        }
    }
}
3.) Compile and build the project.
Output
Share and Enjoy