herozhou1314 / android 技术... / Android: activity transition/animation ...

0 0

   

Android: activity transition/animation in ActivityGroup

2012-04-09  herozhou1...

 use ActivityGroup to manage activities, I want to add animation when change activity, the code I used to change to next activity is:

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

And inside 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 is just an ActivityGroup, provides some useful methods. With the above code, what/where do I add to enable animation?



I needed to implement wizard with page transitions sometime ago. And I used ActivityGroup approach. It had Wizard (inherited from AcitivtyGroup) and WizardPage (inherited from Activity).WizardPage had code that handled animations, while Wizard was responsible to call those animations in appropriate times.

WizardPage class:

/**
 * 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
       
}
   
}              
}

Wizard had startPage method which was called to make the actual activity transitions:

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();
       
}
   
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。如发现有害或侵权内容,请点击这里 或 拨打24小时举报电话:4000070609 与我们联系。

    猜你喜欢

    0条评论

    发表

    请遵守用户 评论公约

    类似文章
    喜欢该文的人也喜欢 更多