Saturday, September 17, 2016

Tween Animation in Android - Mini App

Hi .... Dear All ....  Today I am uploading a superb example of Tween Animation.
In this example i have added multiple buttons which shows you different type of Animation.
Tween Animation is base on different properties like 
• Orientation
• Scale
• Position
• Opacity
on the basis of these attributes Tween animation is categorized as
• Translate Animation
• Rotate Animation
• Alpha Animation
• Scale Animation  
Lets see an example 
A. Main Output screen will be like 


First Button Click 


Second Button Clock 


Third Button Click 


You can check different kind of animation by clicking on buttons
B.   XML Files 
B-1
@ You have to add one xml file to see the rotate effect. for this 
create a new directory in res folder named as anim -> rotateanim.xml is as 
<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:shareInterpolator="true">
    <rotate        android:fromDegrees="0"        android:toDegrees="360"

        android:duration="5000"

             android:pivotX="300"        android:pivotY="300"        />

</set>

The main activity layout file is as 

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

<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:id="@+id/layout"    
         tools:context="com.exam.ravi.animexppt.MainActivity">

    <TextView        android:id="@+id/rotatetext"    
             android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Text Will be Rotated" /> 
          <Button
             android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button1"

        android:text="Rotate Text"        android:onClick="startAnimation"/>
    <TextView        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/scrolltext"        android:lines="1"

        android:text="Will be scrolled to the Left"

        android:scrollHorizontally="true"/>
    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button2"        android:text="Animate to Left + Bounce"

        android:onClick="startAnimation"/>
    <TextView        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/fadeout"        android:lines="1"

        android:text="Will be fade out/in"

        android:scrollHorizontally="true"/>
    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"        android:id="@+id/button3"
  
             android:text="Fade Animation"

        android:onClick="startAnimation"/>
    <Button        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/button4"

        android:text=" Remove Component"

        android:onClick="startAnimation"/>
</LinearLayout>

C. Java Source code is as 
MainActivity.java    
package com.exam.ravi.animexppt;

import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity 
          implements Animation.AnimationListener {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   public void startAnimation(View view)
   {
      switch(view.getId())
      {
          case R.id.button1:
          {
              Animation animation1 = AnimationUtils.loadAnimation(this, 
                                          R.anim.rotateanim);

              animation1.setAnimationListener(this);
              View animatedView1 = findViewById(R.id.rotatetext);
              animatedView1.startAnimation(animation1);
              break;
          }
          case R.id.button2:
          {
              Paint paint = new Paint();
              TextView animatedView2 = (TextView) findViewById(R.id.scrolltext);
              float measureTextCenter= paint.measureText(animatedView2.
                                            getText().toString());
              Animation animation2 =new TranslateAnimation(0f,
                                         -measureTextCenter,0.0f,0.0f);
                   animation2.setDuration(5000);
              animation2.setInterpolator(new BounceInterpolator());
              animatedView2.startAnimation(animation2);
              break;
          }
          case R.id.button3:
          {
              TextView animatedView3 = (TextView) findViewById(R.id.fadeout);
              float from =  1.0f;
              float to =0.0f;
              if(animatedView3.getVisibility()==View.INVISIBLE)
              {
                  from=to;
                  to=1.0f;
              }
              Animation animation3 =new AlphaAnimation(from,to);
              animation3.setDuration(5000);
              animation3.setAnimationListener(this);
              animatedView3.startAnimation(animation3);
              break;
          }
          case R.id.button4:
          {
              ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
              Animation animation4 =new AlphaAnimation(0.0f,1.0f);
              animation4.setDuration(5000);
              LayoutAnimationController controller = new 
                                          LayoutAnimationController(animation4,0);
              layout.setLayoutAnimation(controller);
              View button = findViewById(R.id.button3);
              try              {
                   if(button!=null)
                       layout.removeView(button);
              }
              catch (Exception ex)
              {
                  ex.printStackTrace();
              }
              break;
          }
          default: break;
      }
   }


    @Override

    public void onAnimationStart(Animation animation) {

    }

    @Override

    public void onAnimationEnd(Animation animation) {

    }

    @Override

    public void onAnimationRepeat(Animation animation) {

    }
}

No comments:

Post a Comment