0


我正在使用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;
}
4

2 回答 2

2

如果您试图避免过多的分配,您可以在 onCreate 中创建一次这些动画,然后稍后简单地引用它们,而不是每次都创建一个新动画。

像这样的东西:

private Animation mInFromRight;
private Animation mInFromLeft;

public Animation inFromRightAnimation() {
    return mInFromRight;
}

public Animation outToLeftAnimation() {
    return mInFromLeft;
}

@Override
protected void onCreate ( Bundle savedInstanceState ){
    super.onCreate(savedInstanceState);
    mInFromRight = 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);
    mInFromRight.setDuration(500);

    mInFromLeft = 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);
    mInFromLeft.setDuration(500);

    //...other oncreate code
}

除此之外,如果您在调用动画时仍然得到 GC,那将超出您的控制范围。android 框架在某处分配在动画过程中被销毁的对象。

GC 是正常的、必要的并且是预期会发生的,但是你密切关注事情是件好事。大多数情况下,过多的 GC 是由于框架外发生了一些愚蠢的事情,比如不必要地在循环中分配新对象。但是框架也需要 GC,在这种情况下,这似乎就是正在发生的事情。

于 2012-08-17T15:00:50.120 回答
0

您在 logcat 中看到的条目表明 JVM 执行了 GC。如果启用了 GC 详细,这将被打印出来。这些是详细消息,在 Java 中,您可以通过删除详细信息(不确定 android)来禁用它们。

原因可能是您的动画占用了更多内存,这迫使 dalvik 执行 GC。

于 2012-08-17T14:59:50.410 回答