使用设计支持库时如何将汉堡包图标动画化为后退箭头?
How to animate the hamburger icon to back arrow while using design support library?
您好,我正在使用来自 android 的最新支持库,称为设计支持库,并将其中的 NavigationView 用于显示抽屉。但问题是当我打开抽屉时,我的汉堡包图标不会旋转成后退箭头图标,它始终保持不变,但我记得当我使用没有支持库的抽屉布局时它会自动旋转,这是我最后一次尝试我做的:
dl.setDrawerListener(new ActionBarDrawerToggle(this, dl, tb, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
supportInvalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
});
但什么都没发生,我在这里有什么小窍门或提示吗?
我已经通过这个解决了我的问题,实际上我使用的是使用以下代码将汉堡包图标硬设置为操作栏图标的方法:
ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
我刚刚注释掉了这行代码,然后它起作用了。
您可以添加这些行。
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mActionBarDrawerToggle.syncState(); //Create a ActionBarDrawerToggle object instead of using a anonymous class in set drawerlistener
}
});
希望对您有所帮助。
这是绑定工具栏、抽屉布局以及如何同步它们的完整代码。
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.my_drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawer,
toolbar, R.string.drawer_open, R.string.drawer_close);
drawer.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggle.syncState();
我制作了一个 class 可以用来制作汉堡包或菜单图标的动画。
import android.app.Activity;
import android.content.Context;
import android.support.v7.graphics.drawable.DrawerArrowDrawable;
/**
* Created by ankush38u on 5/13/2016.
*/
public class DrawerArrowAnimation {
public static class DrawerArrowDrawableToggle extends DrawerArrowDrawable implements DrawerToggle {
private final Activity mActivity;
public DrawerArrowDrawableToggle(Activity activity, Context themedContext) {
super(themedContext);
mActivity = activity;
}
public void setPosition(float position) {
if (position == 1f) {
setVerticalMirror(true);
} else if (position == 0f) {
setVerticalMirror(false);
}
setProgress(position);
}
public float getPosition() {
return getProgress();
}
}
/**
* Interface for toggle drawables. Can be public in the future
*/
public static interface DrawerToggle {
public void setPosition(float position);
public float getPosition();
}
}
然后在 activity 中为这个动画可绘制对象创建一个变量或片段,并通过 .setHomeAsUpIndicator(drawerDrawable)
将可绘制对象设置为主页图标的可绘制资源
public static DrawerArrowAnimation.DrawerArrowDrawableToggle drawerDrawable;
//this is if you are using fragments
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
drawerDrawable = new DrawerArrowAnimation.DrawerArrowDrawableToggle(((AppCompatActivity) getActivity()), ((AppCompatActivity) getActivity()).getSupportActionBar().getThemedContext());
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(drawerDrawable);
现在您可以使用drawerDrawable.setPosition(浮动位置);
setPosition 从 0.0f 到 1.0f 为可绘制图标设置动画。
您好,我正在使用来自 android 的最新支持库,称为设计支持库,并将其中的 NavigationView 用于显示抽屉。但问题是当我打开抽屉时,我的汉堡包图标不会旋转成后退箭头图标,它始终保持不变,但我记得当我使用没有支持库的抽屉布局时它会自动旋转,这是我最后一次尝试我做的:
dl.setDrawerListener(new ActionBarDrawerToggle(this, dl, tb, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
supportInvalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
});
但什么都没发生,我在这里有什么小窍门或提示吗?
我已经通过这个解决了我的问题,实际上我使用的是使用以下代码将汉堡包图标硬设置为操作栏图标的方法:
ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
我刚刚注释掉了这行代码,然后它起作用了。
您可以添加这些行。
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mActionBarDrawerToggle.syncState(); //Create a ActionBarDrawerToggle object instead of using a anonymous class in set drawerlistener
}
});
希望对您有所帮助。
这是绑定工具栏、抽屉布局以及如何同步它们的完整代码。
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.my_drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawer,
toolbar, R.string.drawer_open, R.string.drawer_close);
drawer.setDrawerListener(drawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggle.syncState();
我制作了一个 class 可以用来制作汉堡包或菜单图标的动画。
import android.app.Activity;
import android.content.Context;
import android.support.v7.graphics.drawable.DrawerArrowDrawable;
/**
* Created by ankush38u on 5/13/2016.
*/
public class DrawerArrowAnimation {
public static class DrawerArrowDrawableToggle extends DrawerArrowDrawable implements DrawerToggle {
private final Activity mActivity;
public DrawerArrowDrawableToggle(Activity activity, Context themedContext) {
super(themedContext);
mActivity = activity;
}
public void setPosition(float position) {
if (position == 1f) {
setVerticalMirror(true);
} else if (position == 0f) {
setVerticalMirror(false);
}
setProgress(position);
}
public float getPosition() {
return getProgress();
}
}
/**
* Interface for toggle drawables. Can be public in the future
*/
public static interface DrawerToggle {
public void setPosition(float position);
public float getPosition();
}
}
然后在 activity 中为这个动画可绘制对象创建一个变量或片段,并通过 .setHomeAsUpIndicator(drawerDrawable)
将可绘制对象设置为主页图标的可绘制资源public static DrawerArrowAnimation.DrawerArrowDrawableToggle drawerDrawable;
//this is if you are using fragments
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
drawerDrawable = new DrawerArrowAnimation.DrawerArrowDrawableToggle(((AppCompatActivity) getActivity()), ((AppCompatActivity) getActivity()).getSupportActionBar().getThemedContext());
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(drawerDrawable);
现在您可以使用drawerDrawable.setPosition(浮动位置); setPosition 从 0.0f 到 1.0f 为可绘制图标设置动画。