【问题标题】:Android: activity transition/animation in ActivityGroupAndroid:ActivityGroup 中的活动转换/动画
【发布时间】:2011-08-25 17:18:18
【问题描述】:

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

【问题讨论】:

  • 嗨!你解决了这个问题吗?我也面临这个问题,不知道如何解决!

标签: android animation android-activity transition activitygroup


【解决方案1】:

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

Wizard 具有 startPage 方法,该方法被调用以进行实际的活动转换:

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

【讨论】:

  • 非常感谢您的回复!
  • 但是有一个问题,因为你先用setContentView,然后做动画,结果是,新的activity/screen首先显示,然后立即变黑,然后从右边滑入。
  • 那或多或少是实验性代码。所以它不是太光滑。您可以使用activityWindow.setVisibility(View.GONE) 并修改动画以首先将可见性更改为View.VISIBLE
猜你喜欢
  • 2013-02-15
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
相关资源
最近更新 更多