MY mENU


Wednesday, 26 September 2012

Tween Animation In Android


The project describes how to implement tween animation and rotate the text in your application. A tween animation can perform a series of simple transformations (position, size, rotation, and transparency) on the contents of a View object. So, if you have a TextView object, you can move, rotate, grow, or shrink the text.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project TweenAnimationExample
2.) Open and insert following in main.xml:
 android:id="@+id/root" android:layout_width="fill_parent"android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
     android:id="@+id/animatedText" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/hello">
    
>
>
3.) Open and insert following in strings.xml:
>

     name="hello">TweenAnimationExample! Click here to animate the text.>
     name="app_name">TweenAnimationExample>
>
4.) Create, Open and insert following in anim/rotate.xml:
 xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0" android:toDegrees="360" android:toYScale="0.0"
android:pivotX="40%" android:pivotY="30%" android:duration="2000" />
5.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. TweenAnimationExample. Enter following information:
Project name: TweenAnimationExample
Build Target: Android 2.3.3
Application name: TweenAnimationExample
Package name: org.example.TweenAnimationExample
Create Activity: TweenAnimationExample
On Clicking Finish TweenAnimationExample code structure is generated with the necessary Android Packages being imported along with TweenAnimationExample.java. TweenAnimationExample class will look like following:
package org.example.TweenAnimationExample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TweenAnimationExample extends Activity {
        /** Called when the activity is first created. */
        @Override public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                final Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate);
                a.reset();
                final TextView rText = (TextView)findViewById(R.id.animatedText);
                LinearLayout layout = (LinearLayout) findViewById(R.id.root);
                layout.setOnClickListener(new OnClickListener() {
                        @Override public void onClick(View v) {
                                rText.startAnimation(a);
                        }
                });
        }
}
Output –The final output:

No comments:

Post a Comment