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