Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add animation to a text view in android

Tags:

I have a TextView and I'm trying to add a fade in animation to it. My code is returning null and I don't understand why.

Here is my implementation

This is the fade_in.xml

    <alpha
            xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true"
            android:duration="1000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0"/>

and here is how im using it in the corresponding activity

    tv= (TextView)findViewById(R.id.textView);
//-- the below line is returning null
            animation = AnimationUtils.loadAnimation(this,R.anim.fade_in);

            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                tv.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    Intent it  = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(it);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            tv.startAnimation(animation);
like image 500
DeepakKUMARYadav Avatar asked Dec 04 '15 13:12

DeepakKUMARYadav


People also ask

Can we do animation in android?

On Android 4.4 (API level 19) and higher, you can use the transition framework to create animations when you swap the layout within the current activity or fragment. All you need to do is specify the starting and ending layout, and what type of animation you want to use.

What are the two different types of view animations?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .


1 Answers

Android TextView Annimation example

XML

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

<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
      android:fromXScale="1.0"
      android:fromYScale="1.0"
      android:toXScale="2.0"
      android:toYScale="2.0"
      android:duration="3000"></scale>
</set>

Code

private void RunAnimation() 
{
  Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
  a.reset();
  TextView tv = (TextView) findViewById(R.id.firstTextView);
  tv.clearAnimation();
  tv.startAnimation(a);
}

For More :

http://chiuki.github.io/advanced-android-textview/#/5

http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/

like image 135
Raza Poonja Avatar answered Oct 27 '22 04:10

Raza Poonja