0
我試圖實現一個在2佈局之間交叉淡入淡出的動畫,並在它們之間進行平移。第一個佈局是主佈局,第二個佈局是帶有textView和紅色背景的簡單線性佈局(如果您願意,可以使用經典的「錯誤!」屏幕......)。我的目標是動畫mainLayout和wrongLayout之間的快速轉換,使程序在顯示錯誤版面的同時等待一段時間,然後自動返回到mainLayout。事實證明,這非常困難,我嘗試過使用監視器,Thread.sleep()等,但我得到的是程序在開始動畫之前等待,然後在沒有任何停留的情況下執行它。在2個佈局之間進行動畫交叉淡入淡出
我的代碼如下:
在主要方法 -
LinearLayout wrongLayout = (LinearLayout) findViewById(R.id.wrong_layout);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
int animationDuration = getResources().getInteger(
android.R.integer.config_longAnimTime);
crossfade(wrongLayout, mainLayout, animationDuration);
/* This is where I want it to wait for 1 second */
crossfade(mainLayout, wrongLayout, animationDuration);
和交叉淡入淡出方法 -
private void crossfade(View fadeInLayout, final View fadeOutLayout,
int animationDuration) {
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
fadeInLayout.setAlpha(0f);
fadeInLayout.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
fadeInLayout.animate()
.alpha(1f)
.setDuration(animationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
fadeOutLayout.animate()
.alpha(0f)
.setDuration(animationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
fadeOutLayout.setVisibility(View.GONE);
}
});
}
非常感謝......