我正在使用TabActivity
其中每个选项卡都是一个视图。我想在更改标签时创建滑动动画。每次动画开始时,logcat 都会给出一个GC
条目:
08-17 16:37:00.840: D/dalvikvm(2767): GC_CONCURRENT freed 1920K, 43% free 6852K/11975K, paused 12ms+14ms, total 72ms
08-17 16:37:01.235: D/dalvikvm(2767): GC_CONCURRENT freed 1480K, 40% free 7215K/11975K, paused 2ms+3ms, total 46ms
08-17 16:37:01.235: D/dalvikvm(2767): WAIT_FOR_CONCURRENT_GC blocked 18ms
08-17 16:37:05.715: D/dalvikvm(2767): GC_CONCURRENT freed 2092K, 43% free 6882K/11975K, paused 12ms+28ms, total 104ms
08-17 16:37:06.370: D/dalvikvm(2767): GC_CONCURRENT freed 779K, 34% free 7912K/11975K, paused 14ms+6ms, total 65ms
08-17 16:37:09.825: D/dalvikvm(2767): GC_FOR_ALLOC freed 1464K, 47% free 6455K/11975K, paused 19ms, total 19ms
08-17 16:37:09.865: D/dalvikvm(2767): GC_FOR_ALLOC freed 1K, 34% free 7988K/11975K, paused 19ms, total 19ms
08-17 16:37:09.865: I/dalvikvm-heap(2767): Grow heap (frag case) to 16.811MB for 944656-byte allocation
08-17 16:37:10.370: D/dalvikvm(2767): GC_CONCURRENT freed 1427K, 38% free 8706K/13959K, paused 2ms+5ms, total 41ms
每次单击选项卡时,都会出现两种动画:一种用于当前选项卡滑出,另一种用于新选项卡滑入。
这些是Animation
定义:
public Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
return inFromRight;
}
public Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
return outtoLeft;
}
编辑:这是选项卡更改的代码:( currentTab 是私有 int 类成员)
public void onTabChanged(String tabId) {
if (!initializatorFinished) {
getTabHost().setCurrentTab(currentTab);
return;
}
tab1 = (LinearLayout) findViewById(R.id.tabHome);
tab2 = (LinearLayout) findViewById(R.id.tabChanges);
tab3 = (LinearLayout) findViewById(R.id.tabTests);
tab4 = (ScrollView) findViewById(R.id.tabSettings);
tab5 = (LinearLayout) findViewById(R.id.tabInfo);
int currentTab = GetCurrentTab();
int newTab = Integer.parseInt(tabId);
if (currentTab == 1 || currentTab == 0) {
if (newTab == 2) {
tab1.setAnimation(outToLeftAnimation());
tab2.setAnimation(inFromRightAnimation());
}
else if (newTab == 4) {
tab1.setAnimation(outToLeftAnimation());
tab4.setAnimation(inFromRightAnimation());
}
else if (newTab == 5) {
tab1.setAnimation(outToLeftAnimation());
tab5.setAnimation(inFromRightAnimation());
}
}
else if (currentTab == 2) {
if (newTab == 1) {
tab2.setAnimation(outToLeftAnimation());
tab1.setAnimation(inFromRightAnimation());
}
else if (newTab == 4) {
tab2.setAnimation(outToLeftAnimation());
tab4.setAnimation(inFromRightAnimation());
}
else if (newTab == 5) {
tab2.setAnimation(outToLeftAnimation());
tab5.setAnimation(inFromRightAnimation());
}
}
else if (currentTab == 4) {
if (newTab == 1) {
tab4.setAnimation(outToLeftAnimation());
tab1.setAnimation(inFromRightAnimation());
}
else if (newTab == 2) {
tab4.setAnimation(outToLeftAnimation());
tab2.setAnimation(inFromRightAnimation());
}
else if (newTab == 5) {
tab4.setAnimation(outToLeftAnimation());
tab5.setAnimation(inFromRightAnimation());
}
}
else if (currentTab == 5) {
if (newTab == 1) {
tab5.setAnimation(outToLeftAnimation());
tab1.setAnimation(inFromRightAnimation());
}
else if (newTab == 2) {
tab5.setAnimation(outToLeftAnimation());
tab2.setAnimation(inFromRightAnimation());
}
else if (newTab == 4) {
tab5.setAnimation(outToLeftAnimation());
tab4.setAnimation(inFromRightAnimation());
}
}
else if (currentTab == 3) {
if (newTab == 1) {
tab3.setAnimation(outToLeftAnimation());
tab1.setAnimation(inFromRightAnimation());
}
else if (newTab == 2) {
tab3.setAnimation(outToLeftAnimation());
tab2.setAnimation(inFromRightAnimation());
}
else if (newTab == 4) {
tab3.setAnimation(outToLeftAnimation());
tab4.setAnimation(inFromRightAnimation());
}
else if (newTab == 5) {
tab3.setAnimation(outToLeftAnimation());
tab5.setAnimation(inFromRightAnimation());
}
}
SetCurrentTab(Integer.parseInt(tabId));
}
private void SetCurrentTab(int tab) {
this.currentTab = tab;
}
private int GetCurrentTab() {
return this.currentTab;
}