2011-01-14 15 views

risposta

56
ll = new LinearLayout(this); 
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    tv = new TextView(this); 
    tv.setText("Animation"); 

    moveLefttoRight = new TranslateAnimation(0, 200, 0, 0); 
    moveLefttoRight.setDuration(1000); 
    moveLefttoRight.setFillAfter(true); 

    button = new Button(this); 
    button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    button.setText("PressMe"); 
    button.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      tv.startAnimation(moveLefttoRight); 
     } 

    }); 

    ll.addView(tv); 
    ll.addView(button); 
    setContentView(ll); 

è un modo per farlo.

+0

Hi ..Grazie per la tua risposta. Ho provato con l'ur sopra l'esempio. Ma mostra solo la parola "Animazione" senza alcuna mossa. – sanjay

+1

@Sudhakar. Mi dispiace mancare di 'Animation.setDuration (1000)' e 'Animation.setFillAfter (true)'. – techiServices

+0

Ottimo!. Funziona benissimo. Grazie per il tuo prezioso codice .. – sanjay

29

spostare un'immagine da sinistra a destra e da destra a sinistra, utilizzando Android TranslateAnimation

enter image description here

ImageView img_animation = (ImageView) findViewById(R.id.img_animation); 

    TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f, 
      0.0f, 0.0f);   // new TranslateAnimation(xFrom,xTo, yFrom,yTo) 
    animation.setDuration(5000); // animation duration 
    animation.setRepeatCount(5); // animation repeat count 
    animation.setRepeatMode(2); // repeat animation (left to right, right to left) 
    //animation.setFillAfter(true);  

    img_animation.startAnimation(animation); // start animation 

you can find more details from here

-1

Aggiungere questo codice cartella R.anim

<?xml version="1.0" encoding="utf-8"?> 
<set 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/linear_interpolator" 
android:fillAfter="true"> 

<translate 
    android:fromXDelta="0%p" 
    android:toXDelta="100%p" 
    android:duration="800" /> 
</set> 
Problemi correlati