0

活動過渡/動畫我用的ActivityGroup管理活動,我想添加動畫時更改活動, 我用來改變下一個活動的代碼是:安卓:在的ActivityGroup

Intent intent = new Intent(getParent(), AnotherActivity.class); 
TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
parentActivity.startChildActivity("AnotherActivity", intent); 

而且裏面startChildActivity

Window window =getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
if (window != null) { 
    View view = window.getDecorView(); 
    mIdList.add(Id); 
    setContentView(view); 
} 

TabGroupActivity只是一個ActivityGroup,提供了一些有用的方法。通過上面的代碼,我可以在什麼地方添加動畫?

+0

嗨!你解決了這個問題嗎? Iţm也面臨這一點,不知道如何解決它! – adrian 2012-06-28 21:59:30

回答

2

我需要在一段時間之前實現帶有頁面轉換的嚮導。我使用了ActivityGroup方法。它有Wizard(從AcitivtyGroup繼承)和WizardPage(繼承自Activity)。 WizardPage有處理動畫的代碼,而Wizard負責在適當的時間調用這些動畫。

WizardPage類:

/** 
* Called to animate appearance of this activity 
* as if somebody clicked next on previous activity 
* and ended up to this activity. 
* 
* Animation: <---- 
*/ 
void onAnimateOpenAsNext() 
{   
    animateTransition(android.R.attr.activityOpenEnterAnimation); 
} 

/** 
* Called to animate appearance of this activity 
* as if somebody clicked back on previous activity 
* and ended up to this activity. 
* 
* Animation: ----> 
*/ 
void onAnimateOpenAsPrev() 
{ 
    animateTransition(android.R.attr.activityCloseEnterAnimation); 
} 

/** 
* Called to animate disappearance of this acitivity 
* when "next" button was clicked 
* 
* Animation: <-- 
*/ 
void onAnimateCloseOnNext() 
{ 
    animateTransition(android.R.attr.activityOpenExitAnimation); 
} 


/** 
* Called to animate disappearance of this activity 
* when "prev" button was clicked 
* 
* Animation: --> 
*/ 
void onAnimateCloseOnPrev() 
{  
    animateTransition(android.R.attr.activityCloseExitAnimation); 
} 

private void animateTransition(int animAttributeId) 
{  
    TypedValue animations = new TypedValue();  
    Theme theme = this.getTheme(); 

    theme.resolveAttribute(android.R.attr.windowAnimationStyle, animations, true);  
    TypedArray animationArray = obtainStyledAttributes(animations.resourceId, 
                 new int[] {animAttributeId}); 

    int animResId = animationArray.getResourceId(0, 0); 
    animationArray.recycle(); 

    if(animResId != 0) 
    { 
     try 
     { 
      Animation anim = AnimationUtils.loadAnimation(this, animResId);    
      getWindow().getDecorView().startAnimation(anim); 
     } 
     catch(Resources.NotFoundException ex) 
     { 
      //didn't find animation resource, ignore error 
     } 
    }    
} 

WizardstartPage方法,該方法被稱爲使實際活動的轉變:

public void startPage(int i) 
{ 
    int prevIndex = getCurrentPageIndex(); 
    m_pageIndex = i;   

    WizardPage currentPage = getCurrentPage(); 
    if(currentPage != null) 
    { 
     if(prevIndex <= i) 
     { 
      currentPage.onAnimateCloseOnNext(); 
     } 
     else 
     { 
      currentPage.onAnimateCloseOnPrev(); 
     } 
    } 

    LocalActivityManager manager = getLocalActivityManager();   

    m_startingActivity = true; 
    Window activityWindow = manager.startActivity(Integer.toString(i), m_pageIntens.get(i)); 
    m_startingActivity = false; 

    setContentView(activityWindow.getDecorView()); 


    currentPage = getCurrentPage(); 
    if(currentPage != null) 
    { 
     if(prevIndex <= i) 
     { 
      getCurrentPage().onAnimateOpenAsNext(); 
     } 
     else 
     { 
      getCurrentPage().onAnimateOpenAsPrev(); 
     } 
    } 
} 
+0

非常感謝您的回覆! – hzxu 2011-05-15 01:08:59

+1

但是,有一個問題,首先使用setContentView,然後執行動畫,結果是,首先顯示新的活動/屏幕,然後立即變黑,然後從右側滑入。 – hzxu 2011-05-15 02:28:03

+0

這是或多或少的實驗性代碼。所以它不太平滑。您可以使用'activityWindow.setVisibility(View.GONE)'並修改動畫以首先將可見性更改爲'View.VISIBLE'。 – inazaruk 2011-05-15 10:04:30