2013-12-16 41 views
-1

我需要重複void x次。我如何做到這一點?謝謝! P.S.例如,x = 3,我需要開始動畫3次。 P.S.2。對不起,我的英文不好如何重複某些方法

public void animation (View v){ 
     view = new ImageView(getApplicationContext()); 
     view.setImageResource(R.drawable.lemon_prig); 
     view.setLayoutParams(new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT)); 
     fl = (FrameLayout)findViewById(R.id.frameLayout1); 
     fl.addView(view); 
     Random random = new Random(); 
     TranslateAnimation anim = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, random.nextFloat(), 
       Animation.RELATIVE_TO_PARENT, random.nextFloat(), 
       Animation.RELATIVE_TO_PARENT, -0.05f, 
       Animation.RELATIVE_TO_PARENT, 1.5f); 
     anim.setDuration(1800); 
     anim.setAnimationListener(new Animation.AnimationListener() { 
      public void onAnimationStart(Animation paramAnimation) { } 
      public void onAnimationRepeat(Animation paramAnimation) { } 
      public void onAnimationEnd(Animation paramAnimation) { 
       fl.post(new Runnable() { 
        public void run() { 
         runOnUiThread(new Runnable() { 
          public void run() { 
           fl.removeAllViews(); 
          } 
         }); 
        } 
       }); 
      } 
     }); 
     view.startAnimation(anim); 
    } 
    public void limonplus(View v){ 
     count=count + i; 
     animation(v); 
    } 
+0

先學編程! –

回答

-1

你可以使用遞歸來完成這個(假設你將不會被執行此方法數千次連續):

public void animation (View v, int x){ 

    if(x == 0) 
     return; 

    //method code here 

    animation(v, x-1); 
} 
+2

尾遞歸在非函數式語言中通常是一個壞主意。它在形式上等同於迭代,它不涉及堆棧開銷和基本情況的複雜性。 – chrylis

2
for (int i=0; i<x; i++){ 
    animation(v); // v is some View here. 
} 
+0

太簡單了。林小姐。非常感謝你 – user3104809

相關問題