Android TextView Animation

Android allows you to animate a view easily. In this tip, I am going to show you how to animate a TextView by changing its current position, and by changing its opacity.
To animate the TextView by changing its current position, you can use the translationX and translationY methods of the ViewPropertyAnimator object. The ViewPropertyAnimator can be obtained by calling the aminate method of the TextView. The translationX method moves the TextView to a new X value(X axis) while the translationY moves the TextView to a new Y value (Y axis).

You can animate the TextView by changing its opacity. This task is done with the alpha method. Zero alpha value means completely transparent and one alpha value is completely opaque.



Now to have a workable example application, you need to create a new Android project. Then modify the activity_main.xml file to add a TextView as shown below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   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:id="@+id/text_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:textSize="20sp"
        />

</LinearLayout>


Here is the content of the MainActivity.java file. The TextView animation starts when the application launches.

  package com.example.andtip;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;

import android.widget.TextView;

import android.view.View;



public class MainActivity extends Activity {
TextView tv;
Point size;

@TargetApi (16)
   protected void onCreate(Bundle savedInstanceState) {
   //TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   context=this;
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.main, menu);      
       return true;
   }

   public void onStart(){
   super.onStart();
   tv=(TextView)findViewById(R.id.text_view);
   tv.setText("Animating the TextView...");  
   // get screen size
   Display display = getWindowManager().getDefaultDisplay();
   size = new Point();
   display.getSize(size);  
// animate the textview
   tv.animate().translationY(size.y/2).withStartAction(new Runnable(){
   public void run(){
   tv.animate().translationY(size.y/4).alpha(0.5f);
   }
  
   }
  
   );
  
   }
}
  


Android-TextView-Animation

Posted by: Dara | post date: 07-01-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.