2011-09-19 4 views
0

J'ai suivi ce tutoriel (http://stuffthathappens.com/blog/2008/11/13/android-animation-101/) et j'ai dessiné un beau texte tournant sur une toile. Tout fonctionne parfaitement lorsque mon activité principale utiliseComment puis-je inclure une vue personnalisée dans une mise en page XML existante sur Android?

setContentView(new SplashScreenAnimation(this)); 

Voici SplashScreenAnimation:

public class SplashScreenAnimation extends View { 
    private static final String QUOTE = "Nobody uses Java anymore. It's this big heavyweight ball and chain."; 

    private Animation anim; 

    public SplashScreenAnimation(Context context) { 
     super(context); 
    } 

    private void createAnim(Canvas canvas) { 
     anim = new RotateAnimation(0, 360, canvas.getWidth()/2, canvas 
       .getHeight()/2); 
     anim.setRepeatMode(Animation.REVERSE); 
     anim.setRepeatCount(Animation.INFINITE); 
     anim.setDuration(10000L); 
     anim.setInterpolator(new AccelerateDecelerateInterpolator()); 

     startAnimation(anim); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     // Creates the animation the first time 
     if (anim == null) { 
      createAnim(canvas); 
     } 

     Path circle = new Path(); 

     int centerX = canvas.getWidth()/2; 
     int centerY = canvas.getHeight()/2; 
     int r = Math.min(centerX, centerY); 

     circle.addCircle(centerX, centerY, r, Direction.CW); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLUE); 
     paint.setTextSize(30); 
     paint.setAntiAlias(true); 

     canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint); 
    } 
} 

Cependant, je veux combiner ce texte animé et ont deux boutons en dessous de lui, j'ai donc un RelativeLayout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/RelativeLayout1" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:background="@drawable/splash_screen"> 
<LinearLayout android:orientation="vertical" 
    android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:layout_alignParentBottom="true" android:id="@+id/bottomLinearLayout"> 
    <Button android:layout_gravity="center_horizontal" android:id="@+id/playButton" 
    android:text="Button" android:layout_height="wrap_content" 
    android:layout_width="wrap_content"></Button> 
    <Button android:layout_height="wrap_content" 
    android:layout_width="wrap_content" android:id="@+id/optionsButton" 
    android:text="Button" android:layout_gravity="center_horizontal" 
    android:layout_marginBottom="30sp"></Button> 
</LinearLayout> 


<LinearLayout android:id="@+id/topLinearLayout" 
    android:orientation="vertical" android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" android:layout_width="fill_parent"> 
</LinearLayout> 

</RelativeLayout> 

Comment puis-je ajouter cette vue personnalisée au LinearLayout ci-dessus? (topLinearLayout - c'est le dernier)

J'ai essayé beaucoup de choses différentes, et je continue de me retrouver avec des fermetures de force.

Ce genre d'approche a été principalement ce que j'ai essayé. Je l'ai essayé le gonfler, etc.

LinearLayout item = (LinearLayout)findViewById(R.id.topLinearLayout); 
SplashScreenAnimation child = new SplashScreenAnimation(this); 
item.addView(child); 

J'ai ajouté tout le code de classe SplashScreenAnimation:

((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this)) 

Il fonctionne !! Pourquoi les autres méthodes d'ajout manuel ne fonctionneront-elles pas? Cela a-t-il quelque chose à voir avec le fait que l'animation doit commencer par onDraw?

+1

qu'avez-vous essayé? avez-vous essayé '((LinearLayout) findViewById (R.id.topLinearLayout)). addView (nouveau SplashScreenAnimation (this))' –

+0

Sherif QUI FONCTIONNE !!!!! – jducreux

+0

hehe ur bienvenue .. dois-je l'ajouter comme une réponse? et vous acceptez? et tout le monde est heureux? : P –

Répondre

1

Essayez cette ligne ajouter la vue:

((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this)) 
1

Vous devriez pouvoir l'inclure comme n'importe quelle autre vue dans votre mise en page.

Effectuez les opérations suivantes puis définissez votre point de vue du contenu à l'ensemble de la mise en page XML:

<LinearLayout android:id="@+id/topLinearLayout" 
    android:orientation="vertical" android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" android:layout_width="fill_parent"> 
     <com.YOURPACKAGENAME.SplashScreenAnimation 
      android:id="@+id/animation" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content"/> 
</LinearLayout> 
+0

Oui, j'ai essayé cela. J'obtiens la force ferme :( – jducreux

+0

Est-ce que cette approche fonctionnerait si c'était un SurfaceView à la place? – trans

1

Il est assez facile, et il vous suffit d'inclure le nom complet du paquet de la classe extension de la classe Vue:

<LinearLayout 
    android:id="@+id/topLinearLayout" 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_width="fill_parent"> 

     <mypackage.SplashScreenAnimation 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent"/> 

</LinearLayout> 
+0

Oui j'ai essayé ceci, j'obtiens la force ferme :( – jducreux

Questions connexes