我可以打开动画编程抽屉布局?抽屉、布局、动画

2023-09-06 14:31:05 作者:So-SaD..

我用下面的库中创建的应用程序的抽屉: http://developer.android.com/training/implementing-navigation/nav-drawer.html

I created the app drawer by using the following library: http://developer.android.com/training/implementing-navigation/nav-drawer.html

我想打开应用程序时显示导航抽屉与动画。 我该怎么办呢?

I want to show the Navigation Drawer with animation when opening the app. How can I do that?

推荐答案

predraw监​​听器,又名安路

下面是predraw侦听器示例。它会从字面上只要它能这也许有点太快了启动动画。您可能希望做这样的组合可运行的第二个图所示。我不会让这两个结合起来,只有分开。

Here is the predraw listener example. It will literally start the animation as soon as it can which maybe a little too fast. You might want to do a combination of this with a runnable shown second. I will not show the two combined, only separate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    // Building NavDrawer logic here. Just a method call would be best.
    ...

    ViewTreeObserver vto = drawer.getViewTreeObserver();
    if (vto != null) vto.addOnPreDrawListener(new ShouldShowListener(drawer));
}

private static class ShouldShowListener implements OnPreDrawListener {

    private final DrawerLayout drawerLayout;

    private ShouldShowListener(DrawerLayout drawerLayout) {
        this.drawerLayout= drawerLayout;
    }

    @Override
    public boolean onPreDraw() {
        if (view != null) {
            ViewTreeObserver vto = view.getViewTreeObserver();
            if (vto != null) {
                vto.removeOnPreDrawListener(this);
            }
        }

        drawerLayout.openDrawer(Gravity.LEFT);
        return true;
    }
}

PostDelay Runnable接口,又名生活危险

// Delay is in milliseconds
static final int DRAWER_DELAY = 200;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    // Building NavDrawer logic here. Just a method call would be best.
    ...
    new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY);
}

private Runnable openDrawerRunnable() {
    return new Runnable() {

        @Override
        public void run() {
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    }
}

警告

如果他们的应用程序在第一次的 BOOM!阅读这篇博客文章以获取更多信息的 http://corner.squareup.com/2013/12/android-main-thread-2.html 。做最好的办法是使用predraw听众或删除您的可运行在的onPause。

If they rotate on the start of the app for the first time BOOM! Read this blog post for more information http://corner.squareup.com/2013/12/android-main-thread-2.html. Best thing to do would be to use the predraw listener or remove your runnable in onPause.

 
精彩推荐