您的位置:首页 > 移动开发 > Android开发

Android5.0 新特性学习总结

2017-04-01 10:16 183 查看


Android 5.0新特性

  目录:
1.Material Design
2.动态替换Theme
3.View的高度与阴影
4.View的轮廓与裁剪(在Android5.1以及以上才有效果)
5.Palette的使用
6.水波纹动画,自定义水波纹动画以及状态选     择器动画
7.CardView的使用
8.RecyclerView的使用
9.ToolBar的使用
10.Android 5.0新特性的向下兼容



1. 了解Material Design

概念:融合卡片式,立体式的设计风格,强调层次感,动画,阴影等元素
国内翻译介绍:查看 http://design.1sters.com
官网介绍:http://developer.android.com/training/material
演示Android5.0 Demo
Android UI样式风格发展:2.3版本(黄色丑陋版)->4.0(Holo)->5.0(MaterialDesign)


2. 动态替换Theme

MaterialTheme配色方案:http://www.materialpalette.com
修改状态栏,ActionBar,界面背景,NavigationBar的颜色。让Activity使用自定义的Theme。
<style name="AppTheme" parent="@android:style/Theme.Material">
<!--状态栏颜色-->
<item name="android:colorPrimaryDark">#f00</item>
<!--ActionBar颜色-->
<item name="android:colorPrimary">#ff0</item>
<!--界面背景颜色-->
<item name="android:windowBackground">@color/colorWindowBackground</item>
<!--导航栏颜色-->
<item name="android:navigationBarColor">#00f</item>
</style>


动态替换Theme的步骤:
定义至少2套theme
调用setTheme方法设置当前的theme,但是该方法要在setContentView之前,如:
setTheme(mTheme);
setContentView(R.layout.activity_main);


设置了Theme,需要finish当前Activity,然后重启当前Activity,让Theme生效
Intent intent = getActivity().getIntent();
getActivity().finish();//结束当前的Activity
getActivity().overridePendingTransition(0,0);//不要动画
startActivity(intent);



3. View的高度与阴影

官网介绍:https://developer.android.com/intl/zh-tw/training/material/shadows-clipping.html
View新增属性z轴,用来体现Material Design中的层次,影响因素2个:elevation 和 translationZ
View高度 = elevation + translationZ

elevation表示view的高度,高度越大,阴影越大,可以在xml中直接使用属性, 也可以在代码中使用view.setEvelvation();
android:elevation="10dp"


transtionZ属性表示view在Z方向移动的距离,一般用于属性动画中
android:translationZ="10dp"


高度影响View的绘制顺序,过去是按View添加顺序绘制,先添加的先绘制,现在高度小的先绘制,因为高度小的,层级低,在下面, 高度相同的,按添加顺序绘制
注意:
如果View的背景色为透明,则不会显示出阴影效果
只有子View的大小比父View小时,阴影才能显示出来


4. View的轮廓与裁剪(在Android5.1以及以上才有效果)

官网介绍:https://developer.android.com/intl/zh-tw/training/material/shadows-clipping.html

View增加了轮廓概念,轮廓用来表示怎么显示阴影,也就是说轮廓什么形状,阴影就显示什么形状。

View的轮廓可以通过outlineProvider属性设置,默认是依据于background的,还有其他3个取值:bounds,none,paddingBounds
android:outlineProvider="bounds"

none:即使设置了evaluation也不显示阴影
background:按背景来显示轮廓,如果background是颜色值,则轮廓就是view的大小,如果是shape,则按shape指定的形状作为轮廓
bounds: View的矩形大小作轮廓
paddedBounds: View的矩形大小减去padding的值后的大小作轮廓。


可以通过setOutlineProvider()方法自定义轮廓:
tv_blue.setOutlineProvider(new ViewOutlineProvider() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0,0,
view.getWidth(),view.getHeight());
}
});


注意:如果background是图片,那只能通过代码setOutlineProvider()来指定轮廓

View的裁剪是指将View按照轮廓裁剪,能改变View的形状,如圆形头像:
先设置轮廓:
再设置根据轮廓裁剪View,目前只支持对矩形,圆形,圆角矩形的裁剪:
//设置对View进行裁剪
tv_clip.setClipToOutline(true);



5. Palette的使用

使用Palette可以让我们从一张图片中拾取颜色,将拾取到的颜色赋予ActionBar,StatusBar以及背景色可以让界面色调实现统一
使用Palette需要添加以下依赖:
compile 'com.android.support:palette-v7:23.0.0+'


Palette提供的API
传入Bitmap即可获取Palette对象,以下是同步和异步使用方式:
//同步获取,需要在子线程中使用
Palette palette = Palette.from(drawable.getBitmap()).generate();
//异步获取,可以在主线程中使用
Palette.from(drawable.getBitmap()).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
//...
}
});


得到Palette对象后,获取其中的颜色,颜色对应如下:
vibrant      -  有活力的颜色
lightVibrant -  有活力的亮色
darkVibrant  -  有活力的暗色
muted        -  柔和暗淡的颜色
lightMuted   -  柔和的亮色
darkMuted    -  柔和的暗色


获取指定颜色的采样对象,获取采样得到的颜色:
//我们可以直接使用palette获取指定颜色:
palette.getLightMutedColor(defaultColor);

//一般也可以先获取采样对象Swatch,从Swatch中获取我们需要的颜色:
//获取有活力颜色的采样对象
Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();


采样对象Swatch提供了以下方法来获取颜色:
//swatch.getPopulation(): the amount of pixels which this swatch represents.
//swatch.getRgb(): the RGB value of this color.
//swatch.getHsl(): the HSL value of this color,即色相,饱和度,明度.
//swatch.getBodyTextColor(): the RGB value of a text color which can be displayed on top of this color.
//swatch.getTitleTextColor(): the RGB value of a text color which can be displayed on top of this color
//一般会将getRgb设置给控件背景色,getBodyTextColor()设置给文字颜色
textView.setBackgroundColor(vibrantSwatch.getRgb());
textView.setTextColor(vibrantSwatch.getBodyTextColor());



6. 水波纹动画,自定义水波纹动画以及状态选择器动画

首先,在Android5.0以上,点击效果默认自带水波纹效果,并且有2种选择:
//矩形边框水波纹
android:background="?android:attr/selectableItemBackground"
//无边框限制水波纹
android:background="?android:attr/selectableItemBackgroundBorderless"


自定义水波纹动画
使用ViewAnimationUtils创建圆形水波纹动画,注意该动画不能在Activity的onCreate方法中执行:
Animator circularReveal = ViewAnimationUtils.createCircularReveal(text, 0, text.getHeight() , 1f, text.getWidth()*2);
circularReveal.setDuration(1000);
circularReveal.start();


使用ripple标签或者RippleDrawable可以更改控件水波纹动画颜色:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#00ff00">
<item android:id="@android:id/mask" ><color android:color="#0000ff" />


定义带有属性动画的状态选择器
通过stateListAnimator属性指定状态选择器的动画:
android:stateListAnimator="@drawable/selector_anim"


状态选择器文件中需要加入objectAnimator标签:
<selector  xmlns:android = "http://schemas.android.com/apk/res/android" >
<item  android:statepressed = "true" >
<objectAnimator  android:propertyName = "scaleX"
android:duration = "@android:integer/configshortAnimTime"
android:valueTo = "0.2"
android:valueFrom = "1"
android:valueType = "floatType" >
//...


同样,状态选择器动画可以用代码方式加载
//加载动画
AnimatorInflater.loadStateListAnimator();
//设置动画
View.setStateListAnimator();


定义带有帧动画的状态选择器,需要设置给background属性,不是stateListAnimator,如下所示:
<animated-selector  xmlns:android = "http://schemas.android.com/apk/res/android" >
<item  android:id = "@+id/pressed"  android:drawable = "@drawable/drawableP"
android:state_pressed = "true" />
<item  android:id = "@id/default"
android:drawable = "@drawable/drawableD" />
<!-- 指定帧动画 - ->
<transition  android:fromId = "@+id/default"  android:toId = "@+id/pressed" >
<animation-list>
<item  android:duration = "15"  android:drawable = "@drawable/dt1 " />
<item  android:duration = "15"  android:drawable = "@drawable/dt2" />
...
</animation-list>
</animated-selector>



7. CardView的使用

CardLayout拥有高度和阴影,以及轮廓裁剪,圆角等功能
各属性说明:
1.设置圆角:card_view:cardCornerRadius="10dp"
2.设置高度:card_view:cardElevation="10dp"
3.设置内边距:card_view:contentPadding="10dp"
4.设置背景色:card_view:cardBackgroundColor="?android:attr/colorPrimary"



8. RecyclerView的使用

https://developer.android.com/intl/zh-tw/training/material/lists-cards.html
先添加依赖 compile 'com.android.support:recyclerview-v7:23.1.1'
设置LayoutManager:控制RecyclerView如何显示布局,系统提供3个布局管理器:
LinearLayoutManager:线性布局,有横向和竖直方向显示
GridLayoutManager:网格布局,有横向和竖直方向显示
StaggeredGridLayoutManager: 瀑布流布局,有横向和竖直方向显示

然后给RecyclerView设置Adapter<RecyclerView.ViewHolder>
设置点击事件,由于RecyclerView没有setOnItemClickListener,只能在Adapter中给View设置Click事件


9. ToolBar的使用

它用来代替ActionBar,但是比ActionBar更加灵活,相当于可以写在布局文件中的ActionBar;与DrawerLayout的使用的时候,DrawerLayout可以覆盖在ToolBar上,并且ToolBar和ActionBar不能同时使用
使用ToolBar的步骤:
先隐藏ActionBar,可以继承一个不带ActionBar的Theme,如:
style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"


然后在Activity中设置ToolBar替代ActionBar:
setSupportActionBar(toolBar);


最后设置ToolBar的显示内容:
toolBar.setTitle("ToolBar");//设置标题
toolBar.setNavigationIcon(iconRes);//设置图标
toolBar.setOnMenuItemClickListener();//设置Menu Item点击



10. Android 5.0新特性的向下兼容

可以通过Support Library使用的新特性可以向下兼容,如:
RecyclerView (recyclerview-v7)
CardView (cardview-v7)
Palette颜色识别 (palette-v7)
ToolBar (appcompat-v7)
SwipeRefreshLayout (v4)

定义针对版本的资源目录
layout:使用新API的布局放在res/layout-v21中,其他的放res/layout
drawable:使用新属性的drawable文件放到 res/drawable-v21,其他的放 res/drawable
values: 新的主题放到 res/values-v21, 其他的放 res/values

在代码中对系统Version做判断,使用对应的效果,如:
if(Build.VERSION.SDK_INT> 21){
//使用新动画
...
}



Android 6.0新控件


1. TextInputLayout的使用

高级炫酷带有提示的输入框,相当于输入框中的战斗框
使用需要依赖design类库:
compile 'com.android.support:design:23.0.0+'


使用步骤:
先在TextInputLayout中包裹一个EditText,如:

然后给EditText添加文本变化监听器:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if(s.length() > 5){
//设置错误提示信息
inputLayout.setError("不能超过5个");
//启用错误提示
inputLayout.setErrorEnabled(true);
}else{
//关闭错误提示
inputLayout.setErrorEnabled(false);
}
}
});



2. FloatingActionButton的使用

总是能悬浮在界面上的Button,可以设置点击事件
使用需要依赖design类库:
compile 'com.android.support:design:23.0.0+'


可以设置以下属性:
app:fabSize="normal"//2个取值,normal=56dp,mini=48dp
app:elevation="10dp"//高度
app:rippleColor="#0000ff"//按下水波纹颜色
app:pressedTranslationZ="20dp"//按下Z轴移动距离
注意:设置android:clickable="true"才有按下的效果



3. Snackbar的使用

一个介于Toast和Dialog之间的和用户交互的控件,显示在界面底部,并且可以设置点击行为,也可以滑动出去
在和FloatingActionButton一起使用时,需要让CoordinatorLayout作为父布局才能使FloatingActionButton给SnackBar腾出空间


4. TabLayout的使用

相当于ViewPagerIndicator的指示器效果,一般用来跟ViewPager结合使用
使用需要依赖design类库:
compile 'com.android.support:design:23.0.0+'


单独使用TabLayout的步骤:
先添加Tab,使用tabLayout.newTab()方法创建Tab:
//1.添加Tab
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));


给tabLayout设置tab改变的监听器:
//2.给tabLayout添加tab改变的监听器
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.e("TAG", "onTabSelected: "+tab.getText());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});


在xml中给TabLayout设置属性:
app:tabIndicatorColor="#00f"//横线颜色
app:tabSelectedTextColor="#f00"//选中字体颜色
app:tabTextColor="#0f0"//默认字体颜色
app:tabMode="fixed"//fixed:不能滑动,每个Tab平分宽度,scrollable:可以滑动tab,每个tab宽度很小,适用于tab很多的情景
app:tabGravity="fill"//fill:平分宽度,center:让tab居中,如果tabMode是scrollable,那tabGravity会被忽略


和ViewPager关联使用步骤:
先给ViewPager填充数据,然后关联TabLayout和ViewPager:
//给ViewPager填充数据
viewpager.setAdapter(new MyAdapter());
//关联TabLayout和ViewPager
tabLayout.setupWithViewPager(viewpager);


需要注意的是,ViewPager的adapter的getPageTitle()方法的返回值将会设置给Tab的标题


5. CoordinatorLayout的使用

协调布局,能够协调多个布局的位置关系,可以实现让FAB上下滑动,展开或折叠ToolBar,控制View扩展收缩以及放大缩小
使用需要依赖design类库:
compile 'com.android.support:design:23.0.0+'


使用它结合AppBarLayout实现向上滑动隐藏ToolBar的效果:
AppBarLayout会将包裹的所有子View作为一个整体的AppBar,有着统一的界面着色;
app:layout_scrollFlags属性介绍:
scroll:表示该View可以被滑动出CoordinatorLayout的范围,所有想滚动出屏幕的view都需要设置这个flag, 没有设置这个flag的view将被固定在屏幕顶部。例如,TabLayout 没有设置这个值,将会停留在屏幕顶部
enterAlways:表示任意向下的滚动都会导致该View可见
exitUntilCollapsed:滚动退出屏幕,最后折叠在顶端
enterAlwaysCollapsed:当你的视图已经设置minHeight属性又使用此标志时,你的视图只能以最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度


给想滑动出范围的View设置属性,比如ToolBar:
app:layout_scrollFlags=”scroll|enterAlways


给发出滑动行为的View设置属性,比如ViewPager:
app:layout_behavior="@string/appbar_scrolling_view_behavior"


实现步骤:
1.CoordinatorLayout作根控件,包裹AppBarLayout和可滚动的控件,比如ViewPager
2.AppBarLayout包裹 ToolBar 及TabLayout,
ToolBar要滑动,给其设置app:layout_scrollFlags
3.ViewPager是发出滑动行为的控件,设置属性 app:layout_behavior
注意:带layout_scrollFlags的view需要放在固定View的前面,这样滑动的view才能够正常退出,而固定的view继续留在顶部
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: