DrawerLayout 防qq特效
DrawerLayout 用法,先從佈局檔案看,如下所示 “
需要注意的是:用ToolBar替換ActionBar的時候,必須呼叫這個方法supportRequestWindowFeature(Window.FEATURE_NO_TITLE);否則會報錯
package com.example.bluetooth; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.RelativeLayout;
public class BitmapActivity extends AppCompatActivity {
private DrawerLayout drawerLayout; private RelativeLayout mainLeft; private RelativeLayout mainRight; private Toolbar toolbar; private ActionBarDrawerToggle drawerToggle; public static String TAG = "BitmapActivity lgj"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_bitmap); initView(); //將actionBar替換成toolbar setSupportActionBar(toolbar); initEvent(); } private void initEvent() { drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar, R.string.drawerOpen,R.string.drawerClose); //同步狀態 drawerToggle.syncState(); //給側滑空間設定監聽
// drawerLayout.setDrawerListener(drawerToggle); drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { View content = drawerLayout.getChildAt(0); //側滑的view View menu = drawerView; Log.i(TAG,slideOffset+”“); //縮放側滑選單從0.7到 -> 1 float scaleLeft = (float) (0.7 + 0.3*slideOffset); //放大側滑面板 menu.setScaleX(scaleLeft); menu.setScaleY(scaleLeft); //content 放大從1到0.7 float scaleContent = (float) (1-0.3*slideOffset); content.setScaleX(scaleContent); content.setScaleY(scaleContent); //content平移從0到1, int id = menu.getId(); if (id == R.id.main_left_drawer_layout){ content.setTranslationX(menu.getMeasuredWidth()*slideOffset); }else { content.setTranslationX(-menu.getMeasuredWidth()*slideOffset); } }
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
} //左邊選單開關事件
public void openLeftLayout(View view) {
if (drawerLayout.isDrawerOpen(mainLeft)) {
drawerLayout.closeDrawer(mainLeft);
} else {
drawerLayout.openDrawer(mainLeft);
}
}
// 右邊選單開關事件
public void openRightLayout(View view) {
if (drawerLayout.isDrawerOpen(mainRight)) {
drawerLayout.closeDrawer(mainRight);
} else {
drawerLayout.openDrawer(mainRight);
}
}
private void initView() {
toolbar = (Toolbar) findViewById(R.id.toolBar);
drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
mainLeft = (RelativeLayout) findViewById(R.id.main_left_drawer_layout);
mainRight = (RelativeLayout) findViewById(R.id.main_right_drawer_layout);
}
}
“`