2010-11-11 4 views
20

J'essaie d'implémenter l'animation d'interpolation "hyperspace" décrite à http://developer.android.com/guide/topics/resources/animation-resource.html ("Ressources d'animation") - mais elle ne semble pas fonctionner correctement. Lorsque j'exécute l'application, j'obtiens une vue vide sous la barre de titre de l'application. Qu'est-ce que je fais mal?Exemple d'animation d'interpolation simple

Par exemple, voici mon code. J'ai créé res/Anim/hyperspace_jump.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <scale 
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
     android:fromXScale="1.0" 
     android:toXScale="1.4" 
     android:fromYScale="1.0" 
     android:toYScale="0.6" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:fillAfter="false" 
     android:duration="700" /> 
    <set 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:startOffset="700"> 
     <scale 
      android:fromXScale="1.4" 
      android:toXScale="0.0" 
      android:fromYScale="0.6" 
      android:toYScale="0.0" 
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:duration="400" /> 
     <rotate 
      android:fromDegrees="0" 
      android:toDegrees="-45" 
      android:toYScale="0.0" 
      android:pivotX="50%" 
      android:pivotY="50%" 
      android:duration="400" /> 
    </set> 
</set> 

J'ai aussi créé une mise en page/main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 

</LinearLayout> 

Enfin j'ai une activité:

package com.tomoreilly.geology; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView image = (ImageView) findViewById(R.id.ImageView01); 
     Animation hyperspaceJump = 
      AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 
     image.startAnimation(hyperspaceJump); 
    } 
} 

Pourtant, je ne vois aucune animation lorsque je lance l'application. Ai-je manqué des détails qui ne sont pas couverts dans l'exemple "Animation Resources"?

Merci, Tom

Répondre

-1

Je suppose que ViewFlipper est nécessaire. Parce que quand vous voulez animation entre animation, vous avez besoin de ViewFlipper pour faire de l'animation.

faire les choses suivantes. Je suppose que résoudre le problème.

Créer un nouveau fichier XML et ajoutez onglet ViewFlipper en elle ...

utilisation comprennent onglet pour inclure tous les autres mise en page lorsque vous voulez faire de l'animation.

et que d'utiliser le code suivant

flipper = (ViewFlipper) findViewById(R.id.flipper); 

Button button1 = (Button) findViewById(R.id.Button01); // Button in one activity 

Button button2 = (Button) findViewById(R.id.Button02); // Button in second activity 

// Other Methods 

private Animation inFromRightAnimation() { 

     // Animation inFromRight = new TranslateAnimation(
     /* 
     * Animation inFromRight = new ScaleAnimation(
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 

     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
    } 

    private Animation outToLeftAnimation() { 
     // Animation outtoLeft = new TranslateAnimation(
     /* 
     * Animation outtoLeft = new 
     * ScaleAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation outtoLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoLeft.setDuration(500); 
     outtoLeft.setInterpolator(new AccelerateInterpolator()); 
     return outtoLeft; 
    } 

    private Animation inFromLeftAnimation() { 
     // Animation inFromLeft = new TranslateAnimation(
     /* 
     * Animation inFromLeft = new 
     * ScaleAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation inFromLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     inFromLeft.setDuration(500); 
     inFromLeft.setInterpolator(new AccelerateInterpolator()); 
     return inFromLeft; 
    } 

    private Animation outToRightAnimation() { 
     // Animation outtoRight = new TranslateAnimation(
     /* 
     * Animation outtoRight = new 
     * ScaleAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, 
     * Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 
     * 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     */ 
     Animation outtoRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoRight.setDuration(500); 
     outtoRight.setInterpolator(new AccelerateInterpolator()); 
     return outtoRight; 
    } 
+0

pourquoi vous copier coller ci-dessus réponse ici? – Sameer

5

Votre imageview doit avoir une source définie soit dans le xml ou votre activité.

xml:

<ImageView android:id="@+id/ImageView01" 
    android:src="@drawable/someimage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</ImageView> 

activité:

ImageView image = (ImageView) findViewById(R.id.ImageView01); 
image.setImageResource(R.drawable.some_image); 
-1
package com.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.AccelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.ViewFlipper; 

public class TeeenAni extends Activity { 

ViewFlipper flipper; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView image = (ImageView) findViewById(R.id.ImageView01); 
    Animation hyperspaceJump = 
    AnimationUtils.loadAnimation(this, R.anim.); 
    image.startAnimation(hyperspaceJump); 
    flipper = (ViewFlipper) findViewById(R.anim.hyperspace_jump); 

    Button button1 = (Button) findViewById(R.id.Button01); 

    Button button2 = (Button) findViewById(R.id.Button02); 
} 
    private Animation inFromRightAnimation() { 


     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 

     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
    } 

    private Animation outToLeftAnimation() { 

     Animation outtoLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoLeft.setDuration(500); 
     outtoLeft.setInterpolator(new AccelerateInterpolator()); 
     return outtoLeft; 
    } 

    private Animation inFromLeftAnimation() { 

     Animation inFromLeft = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     inFromLeft.setDuration(500); 
     inFromLeft.setInterpolator(new AccelerateInterpolator()); 
     return inFromLeft; 
    } 

    private Animation outToRightAnimation() { 

     Animation outtoRight = new TranslateAnimation(
       Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
       0.0f, Animation.RELATIVE_TO_SELF, -1.0f, 
       Animation.RELATIVE_TO_SELF, 0.0f); 
     outtoRight.setDuration(500); 
     outtoRight.setInterpolator(new AccelerateInterpolator()); 
     return outtoRight; 
    } 

[R.anim.hyperspace_jump] [1] }

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
<ImageView android:id="@+id/ImageView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</ImageView> 

<ViewFlipper android:id="@+id/details" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 

<Button android:id="@+id/Button01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="Home"></Button> 
<Button android:id="@+id/Button02" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="Gallary"></Button> 

+4

S'il vous plaît ajouter un peu plus de substance à votre réponse que juste un peu de code. Ajoutez un peu de raisonnement à comment et pourquoi ce code résout le problème. – Matsemann

11

enter image description here

Pour ajouter une réponse différente, vous pouvez également essayer le Universal Tween Engine pour animer votre Android UIs. En effet, l'animation, ce qui nécessite un bon nombre de lignes dans son format XML, serait décrit comme ceci:

Timeline.createSequence() 
    // First, set your pivot (Tween.set() works instantly) 
    .push(Tween.set(image, ViewAccessor.PIVOT_XY).target(0.5f, 0.5f)) 

    // Then, animate your scale and rotation as you want 
    .push(Tween.to(image, ViewAccessor.SCALE_XY, 0.7f).target(1.4f, 0.6f)) 
    .beginParallel() 
     .push(Tween.to(image, ViewAccessor.SCALE_XY, 0.4f).target(0, 0)) 
     .push(Tween.to(image, ViewAccessor.ROTATION, 0.4f).target(-45)) 
    .end() 

    // Finally, start the animation! 
    .start(); 

Il peut être plus lisible lorsque vous avez un grand ensemble d'actions à la séquence.Le moteur est fortement optimisé pour android, et surtout pour les jeux, et n'alloue rien, pour fournir les meilleures performances.

Il est complètement open-source, fortement documenté, et publié avec une licence Apache-2.

Vous pouvez donner la Android demo essayer si vous voulez :)

+0

En passant, c'est une bibliothèque incroyable qui fonctionne magnifiquement à ce jour. – alvi