简体   繁体   English

打开导航抽屉时如何使背景活动变小?

[英]How to make background Activity smaller while opening the Navigation Drawer?

I want to make my background Activity a little bit smaller while opening the Navigation Drawer , Simulate the effect that exists in the Airbnb application. 我想在打开Navigation Drawer时模拟我的背景Activity ,模拟Airbnb应用程序中存在的效果。 I guess the best explanation would be a screenshot: 我想最好的解释是截图:

在此输入图像描述

But the point is not to make the View just smaller, but to make it an animation that is synchronized to the Drawer Open/Close animation. 但重点不在于使视图更小,而是使其成为与抽屉打开/关闭动画同步的动画。 So if you started to open the Drawer an in the middle decided to stop and go back, the background Activity scale will be affected accordingly. 因此,如果您开始打开抽屉,中间决定停止并返回,则背景活动比例将相应地受到影响。

How can this be done using the build in DrawerLayout? 如何使用DrawerLayout中的构建来完成这项工作? Is there some implementation for this? 这有什么实现吗?

You can perform this effect using the ActionBarDrawerToggle in the support library v4. 您可以使用支持库v4中的ActionBarDrawerToggle执行此效果。

All you have to do is to Override onDrawerSlide method to retrieve the opening % of the drawer menu, and then scale your FrameLayout where your fragment is placed in. 您所要做的就是覆盖onDrawerSlide方法以检索抽屉菜单的开始%,然后缩放放置片段的FrameLayout。

Example with code: 代码示例:

main_layout.xml main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <FrameLayout android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>    

    <ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>

Now in your Activity which holds the Drawer: 现在在你的活动中持有抽屉:

public class ConfigurerActivity extends ActionBarActivity 
{
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private FrameLayout frame;
    private float lastScale = 1.0f;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        frame = (FrameLayout) findViewById(R.id.content_frame);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.acc_drawer_open, R.string.acc_drawer_close) 
        {            
            @SuppressLint("NewApi")
            public void onDrawerSlide(View drawerView, float slideOffset)
            {
                float min = 0.9f;
                float max = 1.0f;
                float scaleFactor = (max - ((max - min) * slideOffset));

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
                {
                    frame.setScaleX(scaleFactor);
                    frame.setScaleY(scaleFactor);
                }
                else
                {               
                    ScaleAnimation anim = new ScaleAnimation(lastScale, scaleFactor, lastScale, scaleFactor, frame.getWidth()/2, frame.getHeight()/2);
                    anim.setDuration(0);
                    anim.setFillAfter(true);
                    frame.startAnimation(anim);

                    lastScale = scaleFactor;
                }
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // ... more of your code
    }
}

Note that i use 2 different methods to scale, because setScaleX/Y are not available in PRE - Honeycomb Android versions. 请注意,我使用2种不同的方法进行缩放,因为在PRE-Honeycomb Android版本中,setScaleX / Y不可用。

With this, you can set your own scaleFactor (i think 0.9f its small enough) or maybe try new effects (change color) based on the opening % of the drawer. 有了这个,你可以设置你自己的scaleFactor(我认为0.9f足够小)或者可以根据抽屉的开口百分比尝试新的效果(改变颜色)。

Hope it helps. 希望能帮助到你。

Hey you can also give best effect for smaller activity . 嘿,你也可以为较小的活动提供最佳效果。 Check ResideMenu 检查ResideMenu

demo with library. 带库的演示。

if you have a look at the third-party library SlidingMenu , that should have everything you want. 如果你看看第三方库SlidingMenu ,它应该拥有你想要的一切。

The bit you are interested in is the behindScrollScale , which says this: 你感兴趣的是behindScrollScale ,它说:

behindScrollScale - a float representing the relationship between the above view scrolling and the behind behind view scrolling. behindScrollScale - 一个浮点数,表示上面的视图滚动和后面的视图滚动之间的关系。 If set to 0.5f, the behind view will scroll 1px for every 2px that the above view scrolls. 如果设置为0.5f,后面的视图将滚动1px为上述视图滚动的每2px。 If set to 1.0f, the behind view will scroll 1px for every 1px that the above view scrolls. 如果设置为1.0f,后面的视图将滚动1px为上述视图滚动的每1px。 And if set to 0.0f, the behind view will never scroll; 如果设置为0.0f,后面的视图将永远不会滚动; it will be static. 它将是静态的。 This one is fun to play around with. 这个玩起来很有趣。 Default is 0.25f. 默认值为0.25f。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM