2015-12-04 11 views
12

Ich habe eine TextView und ich versuche, eine Einblendung Animation hinzufügen. Mein Code gibt null zurück und ich verstehe nicht warum.So fügen Sie eine Animation zu einer Textansicht in Android

Hier ist meine Implementierung

Dies ist die 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"/> 

ist und hier ist, wie im es in der entsprechenden Aktivität

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); 
+0

Bitte geben Sie das Fehlerprotokoll. –

+0

Bitte geben Sie hier den Logcat-Fehler ein. –

Antwort

10

Android Textview Annimation Beispiel

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); 
} 

Weitere:

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

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

0

Sie benötigen setAnimation in Ihrem Textview mit

Beispiel:

tv.setAnimation(animation); 
+0

Ich habe das auch versucht..aber immer noch sagt es das gleiche verursacht durch: java.lang.NullPointerException: Versuch, virtuelle Methode 'void android.widget.TextView.setAnimation (android.view.animation.Animation) aufzurufen 'auf einer Null – DeepakKUMARYadav

1

Ist Ihr Textview-ID richtig ?? Überprüfen Sie zunächst, ob Sie Ihre Textansichts-ID richtig in Ihrer App erhalten.

+0

Basierend auf Kommentar in anderer Antwort - nein, ist es nicht korrekt: "java.lang.NullPointerException: Versuch, virtuelle Methode aufzurufen' void android.widget.TextView.setAnimation (android.view.animation.Animation) ' auf einer Null "das sollte die akzeptierte Antwort sein – WFranczyk

+0

@WFranczyk Was willst du sagen ?? –

1

Sie können Animationen aus der AnimationUtils-Klasse in Android laden und in einer Textansicht in Android festlegen.

textview.startAnimation(AnimationUtils.loadAnimation(c, android.R.anim.fade_in)); 

und Sie können Animation stoppen, mit

textview.clearAnimation(); 
0

Verwenden Animator/AnimatorSet-Animation ist Legacy-Code

Verwandte Themen