本文介绍了安卓:活性转换/动画的ActivityGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的ActivityGroup管理活动,我想添加动画时更改活动,在code我用来改变接下来的活动是:

I 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);

和里面的 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 ,提供了一些有用的方法。有了上面code,有什么/我在哪里补充,使动漫?

TabGroupActivity is just an ActivityGroup, provides some useful methods. With the above code, what/where do I add to enable animation?

推荐答案

我需要一段时间前实现与向导页面过渡。而且我用的ActivityGroup方法。它有向导(从继承 AcitivtyGroup )和一个WizardPage (从活动继承)。 一个WizardPage 有这样的处理动画code,而向导负责调用这些动画在适当的时候。

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 类:

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

向导 STARTPAGE 方法,该方法被调用,以实际活动的转换:

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

这篇关于安卓:活性转换/动画的ActivityGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:46