Umesh

Untitled Document

Tuesday, 6 November 2012

Animate android list items. Rotate Listview items

main.xml :


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello Umesh Suryawanshi"
    android:textColor="#959AE8"
    />
<ListView
    android:id = "@+id/umesh_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#EBC38F"
    />
</LinearLayout>


ListAnimation.java :



package com.umesh.ListAnimation;

import com.umesh.ListAnimation.*;
import com.umesh.ListAnimation.Rotate3dAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListAnimation extends Activity {

String[] items={"Umesh", "Suryawanshi", "Sunil", "Pradip", "Sachin",
         "Milind", "Puja", "Monika", "Diksha", "Mayur",
         "Shivam", "Deepali", "Lokesh", "Anshul", "Vasant",
         "Shivani", "Vishal", "Pravin", "Manisha", "Chetan",
         "Komal", "Vicky", "Tushar"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView flightlist = (ListView)findViewById(R.id.umesh_list);
        flightlist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
        //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
        //items));
        AnimationSet set = new AnimationSet(true);

        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(80);
        set.addAnimation(animation);

        animation = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
        );
        animation.setDuration(500);
        set.addAnimation(applyRotation(0,0,360));

        LayoutAnimationController controller =
                new LayoutAnimationController(set, 0.5f);
        controller.setDelay(1.0f);
        flightlist.setLayoutAnimation(controller);
        //flightlist.de
    }
   
 
    private Rotate3dAnimation applyRotation(int position, float start, float end) {
        // Find the center of the container
        final float centerX = 100.0f;//view.getWidth()/2.0f;
        final float centerY = 20.0f;//view.getHeight()/2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        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));

        return rotation;
    }
 
    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) {

        }

        public void onAnimationRepeat(Animation animation) {
        }
    }
}


Rotate3dAnimation.java  :



package com.umesh.ListAnimation;

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, int parentHeight) {
        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 * (1.0f - interpolatedTime));
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
        }
        camera.rotateX(degrees);
        camera.getMatrix(matrix);
        camera.restore();

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}





No comments:

Post a Comment