2017Google Study Jams China:Google Study Jams活動官網(wǎng)
轉(zhuǎn)載請注明出處:http://www.reibang.com/p/f496ee6b3214
本文出自Shawpoo的簡書
我的博客:CSDN博客
前言
習慣性的在文章開始先來一段介(fei)紹(hua)伏伯。Material Design是在Google I/O 2014提出的一套全新的設計語言規(guī)范躬厌。其目的是專為設計適用于多個平臺和設備的視覺恢口、運動與互動效果而制定的綜合指南。 Android 現(xiàn)在已支持 Material Design 應用讨盒。 如果要在Android 應用中使用 Material Design父款,請遵循Material Design 規(guī)范內(nèi)定義的指導方針尾膊,并使用Android 5.0(API級別21)及以上版本所提供的新組件和功能漱受。
另外對于這次Study Jams舉報的第二屆Android開發(fā)者活動络凿,我已經(jīng)完成了結(jié)業(yè)APP—簡云Note。一款很簡單很水的APP昂羡,主要目的是為了應用Material Desian的組件和配色絮记。歡迎各位小伙伴體驗、點評:
所以本文主要以簡云Note為例來講述Material Design的使用紧憾。
一到千、Color—顏色的選擇
1、靈感來源
顏色Material Design的色彩從當代建筑赴穗、路標、人行橫道以及運動場館中獲取靈感膀息,由此引發(fā)出大膽的顏色表達激活了色彩般眉,與單調(diào)乏味的周邊環(huán)境形成鮮明的對比。強調(diào)大膽的陰影和高光潜支。引出意想不到且充滿活力的顏色甸赃。
2、建議顏色
首先我們要選擇APP的色彩冗酿、主題色等埠对,這里推薦一個調(diào)色板網(wǎng)站,通過下圖可以看到在這些基礎的顏色真的很大膽鮮艷裁替。其次點擊某一項顏色的時候项玛,會列舉出以當前基礎色為準的其他對比色,在左上角都標出了一個飽和度弱判。官方建議我們以飽和度500的顏色作為APP的主色(colorPrimary)襟沮,通過顏色的對比又發(fā)現(xiàn)飽和度700更適合作為狀態(tài)欄的顏色,即比主色更暗一點的顏色(colorPrimaryDark)昌腰,我還通過網(wǎng)上的一個教程說建議使用飽和度A200的顏色作為強調(diào)色(colorAccent)开伏。有了這些建議我們可以更方便,快速的選擇所要搭配的顏色遭商。當然這只是一個建議固灵,我個人覺得只要能將調(diào)色板上的這些顏色很好的搭配起來即可。
3劫流、如何應用
在Android Studio新建項目之后巫玻,我們會發(fā)現(xiàn)項目中res/values/colors.xml中有三個默認的顏色丛忆,所以我們可以直接引用:
<resources>
<color name="colorPrimary">#03a9f4</color> // 主色,建議飽和度500
<color name="colorPrimaryDark">#0288d1</color> // 暗色大审,建議飽和度700
<color name="colorAccent">#FF4081</color> // 強調(diào)色蘸际,建議飽和度A200
</resources>
下圖為簡云Note首頁運行圖,可以看到除了主色徒扶、暗色和強調(diào)色之外粮彤,在左側(cè)每一個字的背景又選擇了其他飽和度為500的顏色,字體的顏色采用了黑色和灰色:
二姜骡、Icon—圖標的設計
定義:系統(tǒng)圖標的設計要簡潔友好导坟,有潮流感,有時候也可以設計的古怪幽默一點圈澈。要把很多含義精簡到一個很簡化的圖標上表達出來惫周,當然要保證在這么小的尺寸下,圖標的意義仍然是清晰易懂康栈。
設計原則:展示一些黑體的幾何形狀递递。一個簡潔的黑體圖形在采用對稱一致的設計時,一樣能夠擁有獨一無二的品質(zhì)啥么。
網(wǎng)格登舞、比例和大小:圖標網(wǎng)格是所有圖標的基準網(wǎng)格并且具有特定的組成和比例。圖標由一些對齊圖標網(wǎng)格的平面幾何形狀組成悬荣〔っ耄基本的平面幾何形狀有四種,具有特定尺寸以保證所有圖標有一致的視覺感和比例氯迂。
兩種形狀相同尺寸不同的圖標集供您在應用程序中使用:狀態(tài)欄践叠、上下文圖標集和操作欄、桌面圖標集嚼蚀。
了解了圖標的這些規(guī)則之后禁灼,好像還是沒有什么卵用,我還是不知道該怎么用驰坊,當然這些只是了解一下就可以了匾二,我們又不是美工,不會做圖拳芙。我們可以使用現(xiàn)有的資源來幫助我們獨立完成開發(fā)所需要的icon:
1察藐、使用Android Studio創(chuàng)建icon(矢量圖片):
在 Android 5.0(API 級別 21)及更高版本中,可自己定義矢量圖片舟扎,而且圖片可在不損失清晰度的情況下縮放分飞。 只需一個資產(chǎn)文件即可創(chuàng)建一個矢量圖像,而位圖圖像則需要為每個屏幕密度提供一個資產(chǎn)文件睹限。 如果要創(chuàng)建一個矢量圖像譬猫,請您在 <vector> XML 元素中定義形狀的詳情讯檐。
在res/drawable右擊選擇 New->Vector Asset:
可以看到Android Studio為我們提供了Material icon,而且可以選擇大小染服,顏色的填充飽和度别洪,以及各種icon供我們選擇:
創(chuàng)建之后會在drawable下生成一個xxx.xml的文件,我們可以在通過@drawable/xxx直接引用柳刮。文件內(nèi)容如下挖垛,通過這種方式創(chuàng)建圖標的話,優(yōu)點在于節(jié)省資源秉颗,而且可以通過控制代碼來直接更換顏色痢毒。
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000" //可以更換填充的顏色
android:pathData="M11.99,2C6.47,2 2,6.47 2,12s4.47,10 9.99,10S22,17.53 22,12 17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8zM16.18,7.76l-1.06,1.06 -1.06,-1.06L13,8.82l1.06,1.06L13,10.94 14.06,12l1.06,-1.06L16.18,12l1.06,-1.06 -1.06,-1.06 1.06,-1.06zM7.82,12l1.06,-1.06L9.94,12 11,10.94 9.94,9.88 11,8.82 9.94,7.76 8.88,8.82 7.82,7.76 6.76,8.82l1.06,1.06 -1.06,1.06zM12,14c-2.33,0 -4.31,1.46 -5.11,3.5h10.22c-0.8,-2.04 -2.78,-3.5 -5.11,-3.5z"/>
</vector>
2、推薦幾個下載Icon的網(wǎng)站(支持png蚕甥、svg):
三、Components—組件的實踐
1菇怀、Toolbar
- a凭舶、在app的build.gradle中添加所需依賴包(新建項目默認有):
dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
}
- b、確保每個使用Toolbar的Activity繼承AppCompatActivity(默認):
public class MyActivity extends AppCompatActivity {
// ...
}
- c爱沟、使用NoActionBar主題库快,去除默認的ActionBar:
<!-- res/values/styles.xml-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!--manifest.xml中配置-->
<application
android:name=".app.NoteApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"> //引用主題
// ...
</application>
- d、在Activity的布局中添加Toolbar:
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp" // 仰角
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
注:Material Design 規(guī)范建議標題欄具有4dp的仰角钥顽。
- e、將Toolbar設置為Activity的標題欄:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle("這是一個標題"); //設置標題
}
- f靠汁、為Toolbar添加Action Button(菜單操作按鈕):
在res/menu下創(chuàng)建xxx.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_done"
android:orderInCategory="100"
android:title="@string/menu_note_done"
android:icon="@mipmap/ic_action_done"
app:showAsAction="always" />
</menu>
ps: app:showAsAction的屬性值:
always: 無論是否溢出蜂大,總是顯示;ifRoom: 根據(jù)屏幕的寬度來定是否顯示蝶怔;never:總是顯示在溢出菜單中奶浦。
在Activity中重寫onCreateOptionsMenu()關(guān)聯(lián)菜單:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.meun_add_or_edit, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_done) {
//點擊菜單選項進行的操作
return true;
}
return super.onOptionsItemSelected(item);
}
- 拓展:APP中經(jīng)常會實現(xiàn)沉浸式(透明式)狀態(tài)欄,請參考我的另一篇文章:
Android4.4+教你輕松打造沉浸式/透明式狀態(tài)欄
2踢星、SwipeRefreshLayout + RecyclerView + CardView
- 下拉刷新:SwipeRefreshLayout為v4包下的一個組件澳叉,其動畫也符合Material Design設計,通過給RecyclerView的addOnScrollListener()設置滑動監(jiān)聽來實現(xiàn)上拉加載更多沐悦。每一個item是一個CardView成洗,可以設置陰影,按下的動畫等等藏否。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
- 每一條item的CardView:
在app的build.gradle中添加所需依賴包:
dependencies {
compile 'com.android.support:cardview-v7:25.3.1'
}
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
3瓶殃、TextInputLayout
TextInputLayout的使用只需要在EditText外層嵌套一個布局即可,可以友好的提示用戶輸入框所填寫的內(nèi)容的要求:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/MyHintText">
<EditText
android:id="@+id/ed_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:hint="@string/input_title_hint"
android:maxLines="1"
android:theme="@style/MyEditText" />
</android.support.design.widget.TextInputLayout>
4副签、SncakBar 和 FloatingActionButton
- SncakBar是Toast的替代品遥椿,在頁面底部彈出:
Snackbar.make(view, "這massage",Snackbar.LENGTH_LONG).show();
- FloatingActionButton浮動按鈕:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:tag="@color/colorAccent"
app:rippleColor="@color/click_bg"
app:layout_anchor="@id/content"
app:layout_anchorGravity="bottom|right"
app:srcCompat="@mipmap/ic_action_add"/>
5基矮、DrawerLayout + NavigationView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main" //設置頭布局
app:menu="@menu/activity_main_drawer" /> //設置菜單項
</android.support.v4.widget.DrawerLayout>
6、CoordinatorLayout的使用
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="@color/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@mipmap/note_detail_bg"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/SimpleToolbarStyle"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//...
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_detail_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
android:src="@mipmap/ic_action_favorite_border"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
7冠场、過渡動畫
采用第三方庫家浇,添加相關(guān)依賴:
compile 'com.github.XunMengWinter:CircularAnim:0.3.4'
代碼中應用:
CircularAnim.fullActivity(MainActivity.this, view)
.colorOrImageRes(R.color.colorPrimary)
.duration(300)
.go(new CircularAnim.OnAnimationEndListener() {
@Override
public void onAnimationEnd() {
//具體操作
}
});
8、高度(Z軸)碴裙、陰影和觸摸反饋
Toolbar:設置android:elevation="4dp"來設置仰角高度钢悲,且點擊Aciton Buttn時Toolbar自帶觸摸按下效果。
CardView設置android:clickable="true"和android:foreground="?android:attr/selectableItemBackground"可以出現(xiàn)一個觸摸反饋青团,其次還可以增加Z軸的考慮譬巫,當我們點擊CardView的時候,給一個抬起的操作督笆,通過 android:stateListAnimator="@animator/selector_cardview"進行引用:
在res下新建animator文件夾芦昔,然后新建xxx.xml:
<!-- res/animtor/xxx.xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="6dp"
android:valueType="floatType"></objectAnimator>
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"
android:valueType="floatType"></objectAnimator>
</item>
</selector>
- Button點擊反饋,apiv21+顯示水波紋操作娃肿,apiv21以下顯示漸變咕缎,通過設置android:background=“@drawable/selector_button”來自動進行版本兼容:
res/drawable/selector_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_btn_pressed" android:state_pressed="true"/>
<item android:drawable="@color/disable_gray" android:state_enabled="false"/>
<item android:drawable="@drawable/shape_btn_normal"/>
</selector>
res/drawable-v21/selector_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">//這里是擴散水波紋的色值
<!--<item android:drawable="@drawable/shape_btn_normal"/>-->
<item>
<selector>
<item
android:drawable="@drawable/shape_btn_normal"
android:state_pressed="false">
</item>
<item
android:drawable="@color/disable_gray"
android:state_enabled="false">
</item>
</selector>
</item>
</ripple>
建議:如果想要更深入的學習Material Design,請參考Material Design的最佳實例APP—Google Play料扰。
參考閱讀
Material Design中文版
Android開發(fā)者官網(wǎng)-Material Design
Material Design官網(wǎng)
Material Design簡介
Material Design標準色(500)
Material Design配色
Material Design圖標(png凭豪、svg等)
Android4.4+教你輕松打造沉浸式/透明式狀態(tài)欄