且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使后台活动较小,同时打开抽屉式导航栏?

更新时间:2022-12-29 08:27:19

您可以通过执行此效果的ActionBarDrawerToggle 在支持库V4。

You can perform this effect using the ActionBarDrawerToggle in the support library v4.

所有你需要做的是重写onDrawerSlide方法来检索抽屉菜单开幕%,然后扩展您的FrameLayout您的片段放在

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.

与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
    }
}

请注意,我用2个不同的方式向规模化,因为setScaleX / Y不在pre可用 - 蜂窝的Andr​​oid版本

Note that i use 2 different methods to scale, because setScaleX/Y are not available in PRE - Honeycomb Android versions.

有了这个,你可以设置自己的比例因子(我认为0.9F它足够小),或者尝试新的效果(变色)的基础上抽屉的开启%。

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.

希望它帮助。