Android 新特性学习总结

来源:互联网 发布:手机号码生成器软件 编辑:程序博客网 时间:2024/05/16 01:33

写在前面:最近由于学习兴趣史无前例的高涨的,看到githup和各个网站的android的新特性,看起来确实很酷,一咬牙挤出时间也去看了文档和借鉴别人的学习经验拿来借鉴,写一些demo(ps:这样就都是我的了,哈哈整个世界都是朕的)

1. 了解Material Design

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

2. 动态替换Theme

  • MaterialTheme配色方案:http://www.materialpalette.com
  • 动态替换Theme的步骤:
    • 定义至少2套theme
    • 在xml布局中给控件指定颜色时要使用占位符的方式,比如:
      android:textColor="?android:colorAccent"
    • 调用setTheme方法设置当前的theme,但是该方法要在setContentView之前,如:
      setTheme(mTheme);setContentView(R.layout.activity_main);
    • 设置了Theme,需要finish当前Activity,然后重启当前Activity,让Theme生效
      Intent intent = getActivity().getIntent();getActivity().finish();//结束当前的ActivitygetActivity().overridePendingTransition(0,0);//不要动画startActivity(intent);

3. View的高度与阴影,轮廓

  • 官网介绍:https://developer.android.com/intl/zh-tw/training/material/shadows-clipping.html
  • View增加了高度的概念,高度大的View会覆盖在高度小的View之上,并带有阴影效果:
    • View高度有2个因素组成:
      View高度 = elevation + translationZ
    • elevation表示view的高度,高度越大,阴影越大,可以在xml中直接使用属性,也可以在代码中使用view.setEvelvation();
      android:elevation="10dp"
    • transtionZ属性表示view在Z方向移动的距离,一般用于属性动画中,translationZ也会影响View的高度,但是对阴影没有效,同时增加了改变translationZ的属性动画;
      android:translationZ="10dp"

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

  • 官网介绍:https://developer.android.com/intl/zh-tw/training/material/shadows-clipping.html
  • View增加了轮廓概念,轮廓会影响阴影的效果和裁剪的形状
    • View的轮廓默认是依据于background的,有其他3个取值:bounds,none,paddingBounds,可以通过outlineProvider属性更改:
      android:outlineProvider="bounds"
    • 可以通过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());        }    });
  • 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标签:

      <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true"><set>    <objectAnimator        android:duration="300"        android:propertyName="rotationX"        android:valueFrom="0"        android:valueTo="60"        android:valueType="floatType"        />    <!--<objectAnimator        android:duration="300"        android:propertyName="scaleY"        android:valueFrom="1"        android:valueTo="0.2"        android:valueType="floatType"/>--></set></item><item android:state_pressed="false"><set>    <objectAnimator        android:duration="300"        android:propertyName="rotationX"        android:valueFrom="60"        android:valueTo="0"        android:valueType="floatType"        />    <!--<objectAnimator        android:duration="300"        android:propertyName="scaleY"        android:valueFrom="0.2"        android:valueTo="1"        android:valueType="floatType"/>--></set></item></selector>
    • 同样,状态选择器动画可以用代码方式加载
      //加载动画AnimationInflater.loadStateListAnimator();//设置动画View.setStateListAnimator();
  • 定义带有帧动画的状态选择器,需要设置给background属性,不是stateListAnimator,如下所示:

    <?xml version="1.0" encoding="utf-8"?><animated-selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@+id/pressed"        android:drawable="@mipmap/btn_check_15"        android:state_pressed="true" />    <item        android:id="@+id/normal"        android:drawable="@mipmap/btn_check_0" />    <!--定义帧动画-->    <transition        android:fromId="@+id/normal"        android:toId="@+id/pressed">        <animation-list>            <item                android:drawable="@mipmap/btn_check_0"                android:duration="15" />            <item                android:drawable="@mipmap/btn_check_1"                android:duration="15" />            <item                android:drawable="@mipmap/btn_check_2"                android:duration="15" />            <item                android:drawable="@mipmap/btn_check_3"                android:duration="15" />        </animation-list>    </transition></animated-selector> 

7. CardView的使用

  • CardLayout是一个拥有高度和阴影,以及轮廓裁剪,圆角的布局
  • 添加依赖:

    compile 'com.android.support:cardview-v7:23.2.1'
  • 各属性说明:
    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.0.0+'
  • 首先设置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点击


Android 6.0新控件

1. TextInputLayout的使用

  • 高级炫酷带有提示的输入框,相当于输入框中的战斗框
  • 使用需要依赖design类库:
    compile 'com.android.support:design:23.0.0+'
  • 使用步骤:
    • 先在TextInputLayout中包裹一个EditText,如:
    • 然后使用以下方法设置:
      //获取EditTextEditText editText = inputLayout.getEditText();//设置错误提示信息inputLayout.setError("不能超过5个");//启用错误提示inputLayout.setErrorEnabled(true);

2. FloatingActionButton的使用

  • 总是能悬浮在界面上的Button,可以设置点击事件
  • 使用需要依赖design类库:
    compile 'com.android.support:design:23.0.0+'
  • 可以设置以下属性:
    app:fabSize="normal"//2个取值,normal=56dp,mini=48dpapp:elevation="10dp"//高度app:rippleColor="#0000ff"//按下水波纹颜色app:pressedTranslationZ="20dp"//按下Z轴移动距离

3. Snackbar的使用

  • 一个有趣的Toast,显示在界面底部,并且可以设置点击行为,也可以滑动出去
  • 需要让CoordinatorLayout作为父布局才能滑动出去

4. TabLayout的使用

  • 相当于ViewPagerIndicator的指示器效果,一般用来跟ViewPager结合使用
  • 使用需要依赖design类库:
    compile 'com.android.support:design:23.0.0+'
  • 单独使用TabLayout的步骤:
    • 先添加Tab,使用tabLayout.newTab()方法创建Tab:
      //1.添加TabtabLayout.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居中
  • 和ViewPager关联使用步骤:
    • 先给ViewPager填充数据,然后关联TabLayout和ViewPager:
      //给ViewPager填充数据viewpager.setAdapter(new MyAdapter());//关联TabLayout和ViewPagertabLayout.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:滚动退出屏幕,最后折叠在顶端,可配合minHeight使用,当达到minHeight的时候固定enterAlwaysCollapsed:当你的视图已经设置minHeight属性又使用此标志时,你的视图只能以最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度
    • 给想滑动出范围的View设置属性,比如ToolBar:

      app:layout_scrollFlags=”scroll|enterAlways
    • 给发出滑动行为的View设置属性,比如ViewPager:

      app:layout_behavior="@string/appbar_scrolling_view_behavior"

Android 5.0&6.0新特性的向下兼容

  • 可以通过Support Library使用的新特性可以直接向下兼容,直接使用即可。如:
    • RecyclerView
    • CardView
    • Palette颜色识别
    • 以及support design包的所有新控件
  • 对于新的属性,则定义多个layout,将使用新API的布局放在res/layout-v21中,其他的放res/layout
  • 对于代码中使用了新特性的情况,在代码中对系统Version做判断,使用对应的效果,如:
    if(Build.VERSION.SDK_INT> 21){        //使用新动画        ...    }
0 0
原创粉丝点击