2013-12-15 35 views
0

我有一個遊戲,我需要讓蘋果下降,但我有一個問題。 (動畫是相同的)當我點擊按鈕1蘋果下降,但當我再次點擊按鈕時,1蘋果消失,並再次下降。我怎麼能做到這一點,當我點擊按鈕,我已經倒下了,當再次點擊時,第一個蘋果不會被抹掉。現在,我有這樣的代碼我需要一次啓動1-100個動畫

public void limonplus(View v){ 
     final ImageView floatingImage = (ImageView)findViewById(R.id.imageView1); 
     Random random = new Random(); 
     TranslateAnimation anim = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, random.nextFloat(), 
     Animation.RELATIVE_TO_PARENT, random.nextFloat(), 
     Animation.RELATIVE_TO_PARENT, 0f, 
     Animation.RELATIVE_TO_PARENT, 1f);   
     anim.setDuration(1800); 
     floatingImage.startAnimation(anim);   
    } 
+0

在你的方法,你有預定義的視圖操作。 嘗試在每次點擊按鈕時使用新的視圖,只需以編程方式進行設置即可。 –

+0

@VortexHeatkiller好的,謝謝。你能否說我該如何做到這一點 – user3104809

回答

0

它是如何工作: 首先,創建的ImageView的新實例。然後你開始動畫和動畫完成 - 你從父母的佈局中移除圖像。 下面是活動代碼:

public class MyActivity extends Activity implements View.OnClickListener { 

Button button; 
FrameLayout fl; 
ImageView view; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button = (Button)findViewById(R.id.button_drop); 
    button.setOnClickListener(this); 
} 

public void limonplus(){ 
    view = new ImageView(getApplicationContext()); 
    view.setImageResource(R.drawable.image_resource); 
    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, 0f, 
      Animation.RELATIVE_TO_PARENT, 1f); 
    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); 
} 

@Override 
public void onClick(View view) { 
    Log.d("myLog","Dropped"); 
    limonplus(); 
} 
} 

下面是main.xml文件的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:id="@+id/parent" 
android:layout_height="match_parent"> 
<Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Drop" 
     android:id="@+id/button_drop" android:layout_gravity="left|top" android:layout_alignParentLeft="true" 
     android:layout_marginLeft="0dp" android:layout_alignParentTop="true" android:layout_marginTop="0dp"/> 
<FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:id="@+id/frameLayout1"> 
</FrameLayout> 
</RelativeLayout> 

希望,它解決您的問題:)

+0

你應該解釋你的代碼是如何工作的 - 並且特別試圖解釋爲什麼OP的原始代碼不能。 –

+0

我已經解釋了爲什麼原始代碼在註釋中不起作用。對於我的代碼,當然,現在將通過解釋來編輯。 –

相關問題