[TOC]
随着5.0版本的发布,带来了新版本的ui设计,那就是MaterialDesign。
如何保持兼容性:
在低于5.0的版本API21之前添加依赖:
dependencies {
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}
但是有些为5.0独有
操作行为转换
触摸反馈
揭露动画
基于路径的动画
矢量图片
图片着色
为早期版本保持兼容
// Check if we're running on Android 5.0 or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Call some material design APIs here
} else {
// Implement this feature without material design
}
material design有用的网站:http://www.materialdoc.com/
改变主题
将继承的主题改为Theme.AppCompat
继承AppCompatActivity
是FragmentActivity的子类。活动应该改为继承AppCompatActivity
使用ActionBar
getSupportActionBar.show();
使用MD图标
MaterialNavagation Drawer
Material Views
- RecyclerView
- CardView
- ToolBar
- Floating action bars
- material animations
ripple animations
触摸发聩提供了视觉瞬间的确认反馈,当用户与ui元素交互时,通常用于可触摸的元素。为了指定资源
对于可点击的View
android:background:"?android:attr/selectableItemBackground"
对于按钮shared Element Transitions
- Circular Reveal
- Scrolling Anmations
#Material 风格
##elevation和shadows高度和阴影
引入了高度的概念,在代码中可以用getElevation()
or setElevation()
<ImageView …
android:elevation="8dp" />
Dynamic Color Palettes动态调色板
从图片提取出的颜色创造出沉浸的感觉,增加静谧的氛围和最佳的可读性。
低版本支持:
compile 'com.android.support:palette-v7:21.0.+'
生成调色板
Palette.Builder用来创建调色板,提供方法设置最大数量的颜色和同步生成和异步生成。
- 同步
当你正在访问图片下载线程
Palette.from(bitmap).generate();
Palette.from(bitmap).maximumColorCount(numberOfColors).generate();
- 异步
为generate
方法传递一个PaletteAsyncListener,就会执行一个异步的任务,手机图片的信息
// This is the quick and easy integration path.
// May not be optimal (since you're dipping in and out of threads)
Palette.from(bitmap).maximumColorCount(numberOfColors).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
// Get the "vibrant" color swatch based on the bitmap
Palette.Swatch vibrant = palette.getVibrantSwatch();
if (vibrant != null) {
// Set the background color of a layout based on the vibrant color
containerView.setBackgroundColor(vibrant.getRgb());
// Update the title TextView with the proper text color
titleView.setTextColor(vibrant.getTitleTextColor());
}
}
});
调整颜色的数量
对于风景来说,12-16.对于很多人脸组成的图片24-32
属性
当调色板生成时,会自动生成6个样本
- Vibrant. –>Palette.getVibrantSwatch()
- Vibrant dark.–> Palette.getDarkVibrantSwatch()
- Vibrant light. –>Palette.getLightVibrantSwatch()
- Muted. –>Palette.getMutedSwatch()
- Muted dark.–> Palette.getDarkMutedSwatch()
- Muted light. –>Palette.getLightMutedSwatch()
每个样本的属性:
- getPopulation(): the amount of pixels which this swatch represents.
- getRgb(): the RGB value of this color.
- getHsl(): the HSL value of this color.
- getBodyTextColor(): the RGB value of a text color which can be displayed on top of this color.
- getTitleTextColor(): the RGB value of a text color which can be displayed on top of this color.
titleTextColor:对于对比度要求较小更加的透明
bodyTextColor:由于字体较小所以要求高对比度。