我有一個非常奇怪的Android動畫問題,我嘗試了很多不同的方法和組件,但仍找不到任何解釋。在FrameLayout中快速切換和動畫視圖會混淆視圖
我有一個FrameLayout這是一個容器的意見,和按鈕。
這的FrameLayout應該一次只顯示一個視圖,當我點擊按鈕,我想的FrameLayout到顯示另一種觀點認爲,並在視圖啓動動畫從FrameLayout中刪除。
這裏的具體情況是我只使用兩個視圖,我想在這兩個視圖之間切換。
問題:
當我多次點擊按鈕的真快,然後停止點擊,兩個視圖顯示在同一時間一個上頂過對方,而不會消失。該容器仍然只包含一個視圖,雖然...絕對奇怪!
error screenshot http://i.minus.com/jDXyvUsE1LCOx.png
我可以用一個簡單的例子來重現此:
public class TestAnimActivity extends Activity implements OnClickListener {
private FrameLayout container;
private TextView current;
private TextView next;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
current = new TextView(this);
current.setText("View 1 YEAH");
next = new TextView(this);
next.setText(" View 2 DOH");
container = (FrameLayout) findViewById(R.id.container);
container.addView(current);
findViewById(R.id.button).setOnClickListener(this);
}
public void onClick(View v) {
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
current.startAnimation(outAnimation);
container.addView(next);
container.removeView(current);
TextView temp = current;
current = next;
next = temp;
}
}
正如你可以看到動畫正在對意見開始,而以前的仍在運行的方式,以及我這可能以某種方式成爲問題的根源。
如果我評論了動畫相關的代碼,它完美的作品:
// Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
// current.startAnimation(outAnimation);
如果我停下重用的意見,並創造新的看法相反,這也完美的作品:
// next = temp;
next = new TextView(this);
next.setText("View: " + new Random().nextInt());
但我不我想創建新的視圖:-)。
看起來這個問題與在視圖上多次啓動動畫時相關,同時向其父添加/刪除此視圖。
我先用ViewFlipper嘗試,然後ViewAnimator,然後擡頭的Android源代碼,並最終做這個手工重現該問題。
如果您想佈局能夠通過自己的測試,這是我main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me fast" />
</LinearLayout>
謝謝您的時間!
感謝您花時間回答這個問題。我有一個類似的問題,我正在更新一個畫廊,而重點項目正在使用動畫進行擴展。但是,每當我更新圖庫時,都會留下前一張圖像。 – Steve 2012-06-21 14:29:24