0

我使用 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,提供了一些有用的方法。使用上面的代码,我应该添加什么/在哪里启用动画?

4

1 回答 1

3

前段时间我需要实现带有页面转换的向导。我使用了 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
        }
    }               
}

WizardhadstartPage方法被调用来进行实际的活动转换:

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();
        }
    }
}
于 2011-05-15T00:18:23.653 回答