NavigationView缓慢打开/跳帧(Android Design Lib)

问题描述 投票:5回答:3

我正在使用Android Design Library提供的NavigationView。我发现在添加了一些表现很差的项目之后。在第一次发射时,需要一秒左右才能打开第一次。这是UI的屏幕截图。

我将尝试发布一些相关的代码。

抽屉XML

<group android:checkableBehavior="single">

    <item
        android:id="@+id/home"
        android:checked="false"
        android:icon="@drawable/ic_home"
        android:title="Home" />

    <item
        android:id="@+id/about"
        android:checked="false"
        android:icon="@drawable/ic_about"
        android:title="About" />

    <item
        android:id="@+id/gallery"
        android:checked="false"
        android:icon="@drawable/ic_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_action_settings"
        android:title="Settings" />

    <item
        android:id="@+id/navigation_subheader"
        android:title="Categories">
        <menu>

            <item
                android:id="@+id/science"
                android:checked="false"
                android:icon="@drawable/ic_science"
                android:title="Science" />

            <item
                android:id="@+id/parenting"
                android:checked="false"
                android:icon="@drawable/ic_parenting"
                android:title="Parenting" />
            <item
                android:id="@+id/android"
                android:checked="false"
                android:icon="@drawable/ic_android"
                android:title="Android" />
            <item
                android:id="@+id/technology"
                android:checked="false"
                android:icon="@drawable/ic_tech"
                android:title="Technology" />
            <item
                android:title=""
                android:checkable="false"
                android:visible="false"
                android:orderInCategory="200"/>

        </menu>
    </item>
</group>

主要的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // Initializing Toolbar and setting it as the actionbar
    toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);


    //Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {


            //Checking if the item is in checked state or not, if not make it in checked state
            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            Log.v(TAG + "-S", "Pre_Switch = " + mCurCheckPosition);

            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {


                case R.id.home:
                    homeFragment();
                    return true;
                case R.id.about:
                    webFragment();
                    return true;
                case R.id.gallery:
                    galleryFragment();
                    return true;
                case R.id.science:
                    scienceFragment();
                    return true;
                case R.id.parenting:
                    parentingFragment();
                    return true;
                case R.id.android:
                    androidFragment();
                    return true;
                case R.id.technology:
                    technologyFragment();
                    return true;
                case R.id.settings:
                    Intent i = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(i);
                default:
                    return true;


            }
        }
    });

    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();

}

最后这是我在Choreographer的日志中看到的错误,我希望这只是lib中的一个错误,但我想知道是否有其他人遇到过这个并且可能有一个解决方法。

I/Choreographer﹕ Skipped 117 frames!  The application may be doing too much work on its main thread.

完整资源来源:https://github.com/caman9119/The_Jones_Theory

android android-design-library navigationview
3个回答
13
投票

在我的header.xml中发现了问题我使用的是一个1080x720p的background.png。我将图像缩小到~400x300,确实是有趣的问题。


2
投票

如果您使用动态图像作为背景(使用用户脸书或g +横幅示例)并使用Picasso这段代码可能很有用:

ImageView background = (ImageView) findViewById(R.id.background);
Picasso
        .with(context)
        .load([Your image source here])
        .fit()
        .centerCrop()
        .into(background)

1
投票

有同样的问题,我在抽屉列表中使用512 x 512图像。所有图像最初都放在'drawable'文件夹中,但当我将它们移动到'drawable-xxhdpi'文件夹时,它是黄油光滑的。

© www.soinside.com 2019 - 2024. All rights reserved.