动画导致垃圾收集器创建日志条目

时间:2012-08-17 14:55:46

标签: java android animation garbage-collection android-animation


我使用的是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;
}

2 个答案:

答案 0 :(得分: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,在这种情况下,似乎正在发生的事情。

答案 1 :(得分:0)

您在logcat中看到的条目告诉JVM执行了GC。如果启用GC详细,将打印此选项。这些是冗长的消息,在Java中你可以通过删除详细(不确定android)来禁用它们。

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