我已经实现了主/细节流程,我希望在添加片段后获得从汉堡包图标到箭头图标的过渡动画(与打开导航抽屉时相同的动画).
我使用的代码如下:
protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_home); toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); getSupportActionbar().setdisplayHomeAsUpEnabled(true); drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); toggle = new ActionbarDrawerToggle(this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.setDrawerIndicatorEnabled(true); toggle.syncState(); //Add home page fragment FragmentManager fragmentManager = getFragmentManager(); HabitHomeFragment homePageFragment = new HabitHomeFragment(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.ID.habit_home, homePageFragment); fragmentTransaction.commit();}
在添加细节片段时:
public voID showDetails() { toggle.setDrawerIndicatorEnabled(false); getSupportActionbar().setdisplayHomeAsUpEnabled(true); fragmentTransaction.replace(R.ID.habit_home, habitDetailsFragment).addToBackStack("detail").commit();}
和工具栏:
<androID.support.design.Widget.AppbarLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:theme="@style/Apptheme.AppbarOverlay"> <androID.support.v7.Widget.Toolbar androID:ID="@+ID/toolbar" androID:layout_wIDth="match_parent" androID:layout_height="?attr/actionbarSize" androID:background="?attr/colorPrimary" app:popuptheme="@style/Apptheme.PopupOverlay" /></androID.support.design.Widget.AppbarLayout> ....
目前在更改片段后,图标只是更改,没有动画.
解决方法:
先解释一下.
> androID.support.v7.app.ActionbarDrawerToggle使用特殊的可绘制类来制作汉堡包到箭头的图标和动画.
>该类是androID.support.v7.graphics.drawable.DrawerArrowDrawable
> DrawerArrowDrawable使用方法setProgress(浮点进度)实现动画,其中进度从0(汉堡包)到1(箭头).
> ActionbarDrawerToggle使用调用DrawerArrowDrawable.setProgress()的private voID setposition(float position)
> ActionbarDrawerToggle使用public voID onDrawerSlIDe(VIEw drawerVIEw,float slIDeOffset)来调用private setposition()
> ActionbarDrawerToggle在构造函数中使用它的侦听器调用toolbar.setNavigationOnClickListener(),该侦听器用于切换抽屉.
> ActionbarDrawerToggle跟踪实际的DrawerArrowDrawable状态.工具栏和Actionbar不跟踪实际的DrawerArrowDrawable状态.
那么,你应该在活动中做些什么.选项-A,使用ActionbarDrawerToggle.
// define a variable to track hamburger-arrow state protected boolean isHomeAsUp = false; protected DrawerLayout drawer; protected Toolbar toolbar; protected ActionbarDrawerToggle toggle; // I've implemented it in setContentVIEw(), but you can implement it in onCreate() @OverrIDe public voID setContentVIEw(@LayoutRes int layoutResID) { super.setContentVIEw(layoutResID); toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); toggle = new ActionbarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); // overwrite Navigation OnClickListener that is set by ActionbarDrawerToggle toolbar.setNavigationOnClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { if (drawer.isDrawerOpen(GravityCompat.START)){ drawer.closeDrawer(GravityCompat.START); } else if (isHomeAsUp){ onBackpressed(); } else { drawer.openDrawer(GravityCompat.START); } } }); } // call this method for animation between hamburged and arrow protected voID setHomeAsUp(boolean isHomeAsUp){ if (this.isHomeAsUp != isHomeAsUp) { this.isHomeAsUp = isHomeAsUp; ValueAnimator anim = isHomeAsUp ? ValueAnimator.offloat(0, 1) : ValueAnimator.offloat(1, 0); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator valueAnimator) { float slIDeOffset = (float) valueAnimator.getAnimatedValue(); toggle.onDrawerSlIDe(drawer, slIDeOffset); } }); anim.setInterpolator(new DecelerateInterpolator()); // You can change this duration to more closely match that of the default animation. anim.setDuration(400); anim.start(); } }
或者您可以使用toolbar.setNavigationIcon()将DrawerArrowDrawable设置为导航图标,并在没有ActionbarDrawerToggle的情况下为其设置动画
见选项-B:https://stackoverflow.com/a/42024138/1148784
以上是内存溢出为你收集整理的android – 从添加片段的汉堡包到箭头图标的过渡动画全部内容,希望文章能够帮你解决android – 从添加片段的汉堡包到箭头图标的过渡动画所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)