![](/img/trans.png)
[英]Slide transition animation in a ActivityGroup on Android
[英]Android: activity transition/animation in 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
,提供了一些有用的方法。 使用上面的代码,我添加什么/在哪里启用动画?
我需要在不久前实现带有页面转换的向导。 我使用了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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.