Speed Up 'Navigation Drawer' Animation Speed on Closing

Speed up 'Navigation Drawer' animation speed on closing?

You can definitely adjust the duration of the animation, but it will require you to copy over the classes from the support library, then edit them accordingly.

ViewDragHelper

The duration is determined here in ViewDragHelper

Then is applied to the DrawerLayout when ViewDragHelper.smoothSlideViewTo is called

You'll need to create a modified version of ViewDragHelper.forceSettleCapturedViewAt that passes in a duration param.

forceSettleCapturedViewAt(... int duration)

Then create your version of ViewDragHelper.smoothSlideViewTo.

public boolean smoothSlideViewTo(... int duration) {
...
return forceSettleCapturedViewAt(... int duration);
}

DrawerLayout

Next you'll need to modify DrawerLayout.closeDrawer and DrawerLayout.closeDrawers to match your new ViewDragHelper modifications.

ActionBarDrawerToggle

You'll also have to copy over ActionBarDrawerToggle and ActionBarDrawerToggleHoneycomb. These files won't require any editing though.

Controlling the Navigation Drawer sliding speed

I think this link will help you: Speed up 'Navigation Drawer' animation speed on closing?

public boolean smoothSlideViewTo(... int duration) {
...
return forceSettleCapturedViewAt(... int duration);
}

Optimizing drawer and activity launching speed

According the docs,

Avoid performing expensive operations such as layout during animation as it can cause stuttering; try to perform expensive operations during the STATE_IDLE state.

Instead of using a Handler and hard-coding the time delay, you can override the onDrawerStateChanged method of ActionBarDrawerToggle (which implements DrawerLayout.DrawerListener), so that you can perform the expensive operations when the drawer is fully closed.

Inside MainActivity,

private class SmoothActionBarDrawerToggle extends ActionBarDrawerToggle {

private Runnable runnable;

public SmoothActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
}

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
if (runnable != null && newState == DrawerLayout.STATE_IDLE) {
runnable.run();
runnable = null;
}
}

public void runWhenIdle(Runnable runnable) {
this.runnable = runnable;
}
}

Set the DrawerListener in onCreate:

mDrawerToggle = new SmoothActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);
mDrawerLayout.setDrawerListener(mDrawerToggle);

Finally,

private void selectItem(int position) {
switch (position) {
case DRAWER_ITEM_SETTINGS: {
mDrawerToggle.runWhenIdle(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
});
mDrawerLayout.closeDrawers();
break;
}
case DRAWER_ITEM_HELP: {
mDrawerToggle.runWhenIdle(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, HelpActivity.class);
startActivity(intent);
}
});
mDrawerLayout.closeDrawers();
break;
}
}
}

Change transition of Navigation drawer

you can create the fade in / out animations by creating the following animation xml:

<!-- fade out-->    
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" >
</alpha>
</set>

r

<!--fade in-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0">
</alpha>
</set>

then override the onDrawerSlide of DrawerLayout.DrawerListener, to add the appropriate animation to your drawer menu as it slides open/close. For example:

mDrawerLayout.addDrawerListener(
new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Respond when the drawer's position changes
Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
drawerView.startAnimation(animFadeIn);
}

@Override
public void onDrawerOpened(View drawerView) {
// Respond when the drawer is opened
}

@Override
public void onDrawerClosed(View drawerView) {
// Respond when the drawer is closed
}

@Override
public void onDrawerStateChanged(int newState) {
// Respond when the drawer motion state changes
}
}
);

Control the sliding speed of the drawer in Flutter

This package flutter_innner_drawer can do the trick, you can set the animation duration and even customize the animation of the drawer



Related Topics



Leave a reply



Submit