material designなdrawerを実装したい

28
Material Design Drawer を実装したい 2015/3/12 @sakebook http://github.com/sakebook http://sakebook.hatenablog.com

Upload: shinya-sakemoto

Post on 14-Jul-2015

1.434 views

Category:

Devices & Hardware


0 download

TRANSCRIPT

Page 1: Material Designなdrawerを実装したい

Material DesignなDrawerを実装したい

2015/3/12@sakebook

http://github.com/sakebookhttp://sakebook.hatenablog.com

Page 2: Material Designなdrawerを実装したい

自己紹介酒本伸也

@sakebookAndroid Developer & Lifelogger

JX通信社 Vingow(Android) / NewsDigest(iOS)

Swift

Page 3: Material Designなdrawerを実装したい

Google Play

Page 4: Material Designなdrawerを実装したい

真似したい

Page 5: Material Designなdrawerを実装したい

Project Template

Page 6: Material Designなdrawerを実装したい

いろいろ違う

Page 7: Material Designなdrawerを実装したい

よく見たらいろいろある

Page 8: Material Designなdrawerを実装したい

それぞれの実装方法を紹介

Page 9: Material Designなdrawerを実装したい

• ハンバーガーがくるっとする

• ToolBarに被らない

Google Photo

Page 10: Material Designなdrawerを実装したい

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@android:color/white</item> </style>

Google Photo

spinBarsを回転させるかどうか

style.xml

Page 11: Material Designなdrawerを実装したい

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/parent_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/toolbar" layout="@layout/tool_bar"/> <android.support.v4.widget.DrawerLayout android:id=“@+id/drawer_layout …

Google Photo

DrawerLayoutに親を作り、その間にToolBar

activiy_hoge.xml

Page 12: Material Designなdrawerを実装したい

• ToolBarに重なる

Google Music

Page 13: Material Designなdrawerを実装したい

• ToolBarに重なる• StatusBarを暗くする

Fril

Page 14: Material Designなdrawerを実装したい

<style name=“Theme.Fril" parent="BaseTheme"> <item name=“android:windowTranslucentStatus">true</item> </style>

Fril

StatusBarが透過するv19からなので、styleを分ける

value-v19/styles.xml

Page 15: Material Designなdrawerを実装したい

Fril

StatusBarを透過した分だけ調節する

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent"> …

activity_hoge.xml

Page 16: Material Designなdrawerを実装したい

Fril

Statusbarの色を指定する

…mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); …mDrawerLayout.setStatusBarBackgroundColor(Color.parseColor("#ff6600"));…

HogeActivity.java

Page 17: Material Designなdrawerを実装したい

• StatusBarに重なる• Drawerの中身も

StatusBarに描画される

Google Play

Page 18: Material Designなdrawerを実装したい

StatusBarの部分の描画を許可するStatusBarの色を無色にするv16からなのでstyleを分ける

StatusBarを透過にするのではなく重なる部分を透過に見せている

values-v16/styles.xml

Google Play

<style name="Theme.Mail" parent="BaseTheme"> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item></style>

Page 19: Material Designなdrawerを実装したい

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent">

<LinearLayout android:orientation="vertical" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/tool_bar"/> </LinearLayout>

<com.sakebook.android.sample.navigationdrawersample.ui.ScrimInsetsFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" app:insetForeground="#80000000" android:id="@+id/parent_drawer" android:fitsSystemWindows="true" android:layout_gravity="start" android:layout_width="match_parent" android:layout_height="match_parent"> … </com.sakebook.android.sample.navigationdrawersample.ui.ScrimInsetsFrameLayout>

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

activity_hoge.xmlGoogle Play

}}

Page 20: Material Designなdrawerを実装したい

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent">

<LinearLayout android:orientation="vertical" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/tool_bar"/> </LinearLayout>

StatusBarが描画可能になった分だけ調節するToolBarが隠れるのを防ぐ

activity_hoge.xmlGoogle Play

Page 21: Material Designなdrawerを実装したい

<com.sakebook.android.sample.navigationdrawersample.ui.ScrimInsetsFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" app:insetForeground="#80000000" android:id="@+id/parent_drawer" android:fitsSystemWindows="true" android:layout_gravity="start" android:layout_width="match_parent" android:layout_height="match_parent"> … </com.sakebook.android.sample.navigationdrawersample.ui.ScrimInsetsFrameLayout></android.support.v4.widget.DrawerLayout>

FrameLayoutを拡張したカスタムレイアウトScrimInsetsFrameLayout (by iosched)

activity_hoge.xmlGoogle Play

Page 22: Material Designなdrawerを実装したい

app:insetForeground を定義する

attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="ScrimInsetsView"> <attr name="insetForeground" format="reference|color" /> </declare-styleable></resources>

Google Play

Page 23: Material Designなdrawerを実装したい

FrameLayoutを拡張したカスタムレイアウトScrimInsetsFrameLayout (by iosched)

ScrimInsetsFrameLayoutGoogle Play private void init(Context context, AttributeSet attrs, int defStyle) { final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScrimInsetsView, defStyle, 0); if (a == null) { return; } mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsView_insetForeground); a.recycle(); setWillNotDraw(true); }

@Override protected boolean fitSystemWindows(Rect insets) { mInsets = new Rect(insets); setWillNotDraw(mInsetForeground == null); ViewCompat.postInvalidateOnAnimation(this); if (mOnInsetsCallback != null) { mOnInsetsCallback.onInsetsChanged(insets); } return true; // consume insets }

Page 24: Material Designなdrawerを実装したい

• 透過させているのではなく、StatusBarに重なる部分に半透明の色をつけている

Google Play

Page 25: Material Designなdrawerを実装したい

理解したところで

Page 26: Material Designなdrawerを実装したい

Drawerだけでもやることいっぱい

http://www.google.com/design/spec/patterns/navigation-drawer.html

Page 27: Material Designなdrawerを実装したい

ライブラリで楽しようhttps://github.com/mikepenz/MaterialDrawer

https://github.com/neokree/MaterialNavigationDrawer

https://github.com/rudsonlive/NavigationDrawer-MaterialDesign

https://github.com/kanytu/android-material-drawer-template

https://github.com/HeinrichReimer/material-drawer

Page 28: Material Designなdrawerを実装したい

以上

https://github.com/sakebook/android-sample-NavigationDrawerSample