Android小细节安卓UI

学习Android CoordinatorLayout之自定义B

2019-05-20  本文已影响179人  周蛋蛋
709594-20170704133953550-1062376288.gif

简单的先看一下,在我们很早之前,实现这样的效果,我们ListViw或者RecyclerView然后Add一个头进去,处理各种滑动事件,特J8烦,谷歌孙子后来也知道了大家的苦衷,就出来了CoordinatorLayout帮我们这帮苦逼程序员解决了(今天看到谷歌将Android停止给华为服务等消息,心情很气愤,谷歌和特朗普都是垃圾,操,谷歌老爹现在就是孙子),顺便我把Android design其他控件说一下

前言

Android Design Support Library——Google I/O 2015 为 Android 开发者带来的福利
很多人会把这个库和 Android Support Library混淆,其实不然,这个库是为了Design而生的。 Android Support Library只支持一些基本控件的material design,而是这个库更多的是对一些material design特效的实现。这个库是官方把一些之前用github开源项目实现的material design效果标准化了。为开发者提供了更好的material design效果实现方式。

开始

Android 5.0棒棒糖是有史以来最重要的Android版本之一,在很大程度上由于一个新的设计语言Material Design的引入,刷新整个安卓系统的体验。我们的详细介绍是一个学习适配material design的好地方。但是我们知道,对于开发人员来说他也是一个挑战,特别是向下兼容方面。从新的Android Design Support Library(android设计支持库)带来一点小的帮助:我们带来了一些重要的Material Design组件给所有开发者,兼容Android 2.1和以上的设备。你会发现一个Navigation View(导航抽屉组件),floating labels for editing text(浮动标签用于编辑文本), a floating action button(浮动操作按钮), snackbar, tabs,将这些控件结合起来的 一个手势和滚动框架。

导航抽屉是APP 识别度和内部导航的关键,保存设计上的一致性对app的可用是是非常重要的,特别是对于第一次使用的用户。NavigationView通过提供你需要的抽屉式导航框架使实现更简单,能够通过菜单资源文件直接生成导航元素。

image

即使最不起眼的EditText,在Material Design中也有提升空间。单独的EditText 在用户输入第一个字母的时候隐藏提示文字,你现在可以将EditText包含在一个TextInputLayout 当中,提示文字变成了一个floating label 悬浮在EditText上面,确保用户永远都不失去上下文的提示在输入文本的时候。

image

除了可以显示输入提示,你也可以通过setError()在EditText下面显示错误信息。

作者提示:效果类似wrapp/floatlabelededittext 如下图:

20150530135245287.gif

floating action button 是一个圆形的按钮,是你界面上的主要操作。Design library的FloatingActionButton给你一个一致的实现,在默认情况下使用colorAccent的主题颜色的。

image

除了正常尺寸的浮动操作按钮,它也支持微型尺寸(fabSize=“mini”)。由于FloatingActionButton继承于ImageView,你也可以采用Android:src或任何其他方法,如setImageDrawable()来控制FloatingActionButton内显示的图标。

如果需要为一个操作提供一个轻量级的快速反馈,使用snackbar是一个极好的机会。Snackbar显示在屏幕底部,并包含文字提示和一个操作按钮。它在给定的时长的结束后后自动消失。此外,也可以在超时之前由用户滑动让其消失。

20150530152244815.gif

对于Material Design来说,利用tabs通过切换不同的视图在你的应用程序中不是一个新概念。他们同样存在首页的顶级导航模式或者组织不同分组的内容在你的App中(例如:不同流派的音乐)。

image
然而,如果您使用的是ViewPager水平之间横向切换,您可以创建直接从你的标签PagerAdapter[getPageTitle()](http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html?utm_campaign=io15&utm_source=dac&utm_medium=blog#getPageTitle(int)) 中创建tabs,后连使用setupWithViewPager()将二者联系起来。 它可以使tab的选中事件能更新ViewPager,同时ViewPager的页面改变能更新tab的选中状态。`

其他主要利用CoordinatorLayout的例子就是程序栏(app bar),原action bar,和滚动技术。你可能已经使用Toolbar在你的布局中,它允许你更加自由的自定义其外观与布局的其余部分融为一体。Design library 把这种设计提到了一个更高的水平:使用AppBarLayout(全新的用来实现各种Material Design概念效果的一个Layout,主要负责对滑动的响应)让你的Toolbar和其他视图(例如:TabLayout提供的tabs)能够相应标记了ScrollingViewBehavior的view的滚动事件。因此,你可以创建一个布局,如下:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="qqbroswer.utils.TestCoordinatorSampleActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rl_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:minHeight="0dp"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
            android:id="@+id/toolbar"
            android:background="@color/color_red"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

分析一下:
现在,当用户滚动RecyclerView时候,AppBarLayout可以这样响应滚动事件:根据子view的滚动标志(scroll flag)来控制它们如何进入(滚入屏幕)与退出(滚出屏幕)。
Flag包括:
scroll:这个标志应该适合希望在屏幕上滚动的所有视图而设置,不使用此标志的时候,他们会保持固定在屏幕的顶部
enterAlways:此标志确保任何向下滚动将使这一视图成为可见,即“快速返回”模式
enterAlwaysCollapsed:当你的视图已经设置minHeight属性又使用此标志时,你的视图只能已最小高度(即“折叠”模式)进入,但滚动视图已经到达顶部时其扩大到完整高度。
exitUntilCollapsed:exitUntilCollapsed: 滚动退出屏幕,最后折叠在顶端

注:所有使用scroll flag属性的view都必须定义在没有使用scroll flag的view的前面,这保证了所以view从顶部退出之后,留下固定的视图在元素。

enterAlways:这个效果就是简书的文章这种,gif图就不做了,好麻烦
enterAlwaysCollapsed:这个效果就是:比如,一个recyclerView,当你下拉到item是1的时候才出现
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="qqbroswer.utils.TestCoordinatorSampleActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rl_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
           <ImageView
               app:layout_collapseMode="parallax"
               app:layout_collapseParallaxMultiplier="0.7"
               android:background="@drawable/mv"
               android:layout_width="match_parent"
               android:layout_height="match_parent" />
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/color_transparent"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>


同时在ImageView中使用了app:layout_collapseMode="parallax"属性,来产生视差渐变的效果
使用app:layout_collapseParallaxMultiplier="0.7"可以控制视差的变化,属性值的范围是[0.0, 1.0],值越大视察越大。
cs.gif

就是这个效果,本来不想做gif图,但是为了表达更加明白,还是做了,制作gif图免费的软件也没找到,今天说到这里,明天自定义gif图

一 初识CoordinatorLayout

Android Design Support Library——Google I/O 2015 为 Android 开发者带来的福利
compile 'com.android.support:design:24.1.1'
CoordinatorLayout作为support:design库里的核心控件,在它出现之前,要实现View之间嵌套滑动等交互操作可不是件容易的事,复杂、难度大,基本绕不开View的事件机制,CoordinatorLayout很大程度上解决了这个痛点,方便我们实现各种炫酷的交互效果。

zfb.gif

实现支付宝的头布局
分析一下逻辑
这个渐变动画其实可分为两段:

1、导航栏从展开状态向上收缩时,头部的各控件要慢慢向背景色过渡,也就是淡入效果;

2、导航栏向上收缩到一半,顶部的工具栏要换成收缩状态下的工具栏布局,并且随着导航栏继续向上收缩,新工具栏上的各控件也要慢慢变得清晰起来,也就是淡出效果。

如果导航栏是从收缩状态向下展开,则此时相应的做上述渐变动画的取反效果,即:

1、导航栏从收缩状态向下展开时,头部的各控件要慢慢向背景色过渡,也就是淡入效果;同时展开导航栏的下部分布局,并且该布局上的各控件渐渐变得清晰;

2、导航栏向下展开到一半,顶部的工具栏要换成展开状态下的工具栏布局,并且随着导航栏继续向下展开,新工具栏上的各控件也要慢慢变得清晰起来,也就是淡出效果。

看起来还比较复杂,如果只对某个控件做渐变动画还好,可是导航栏上的控件有好几个,而且并不固定常常会增加和修改。倘若要对导航栏上的各控件逐一动画过去,不但费力气,而且后期也不好维护。为了解决这个问题,我们可以采取类似遮罩的做法,即一开始先给导航栏罩上一层透明的视图,此时导航栏的画面就完全显示;然后随着导航栏的移动距离,计算当前位置下的遮罩透明度,比如该遮罩变得越来越不透明,看起来导航栏就像蒙上了一层面纱,蒙到最后就看不见了。反过来,也可以一开始给导航栏罩上一层不透明的视图,此时导航栏的控件是看不见的,然后随着距离的变化,遮罩变得越来越不透明,导航栏也会跟着变得越来越清晰了。

渐变动画的思路有了,可谓万事俱备,只欠东风,再来一个导航栏的移动偏移监听器便行,正好有个现成的AppBarLayout.OnOffsetChangedListener,只需给AppBarLayout对象调用addOnOffsetChangedListener方法,即可实现给导航栏注册偏移监听器的功能。接下来的事还是直接看看具体的实现代码吧:


    private AppBarLayout appBar;
    /**
     * 大布局背景,遮罩层
     */
    private View bgContent;
    /**
     * 展开状态下toolbar显示的内容
     */
    private View toolbarOpen;
    /**
     * 展开状态下toolbar的遮罩层
     */
    private View bgToolbarOpen;
    /**
     * 收缩状态下toolbar显示的内容
     */
    private View toolbarClose;
    /**
     * 收缩状态下toolbar的遮罩层
     */
    private View bgToolbarClose;
  appBar = findViewById(R.id.app_bar);
        bgContent = findViewById(R.id.bg_content);
        toolbarOpen = findViewById(R.id.include_toolbar_open);
        bgToolbarOpen = findViewById(R.id.bg_toolbar_open);
        toolbarClose = findViewById(R.id.include_toolbar_close);
        bgToolbarClose = findViewById(R.id.bg_toolbar_close);

        appBar.addOnOffsetChangedListener(this);
   /**
     * @param appBarLayout
     * @param verticalOffset RGB 计算颜色
     */
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        //垂直方向偏移量
        int offset = Math.abs(verticalOffset);
        //最大偏移距离
        int scrollRange = appBarLayout.getTotalScrollRange();
        if (offset <= scrollRange / 2) {//当滑动没超过一半,展开状态下toolbar显示内容,根据收缩位置,改变透明值
            toolbarOpen.setVisibility(View.VISIBLE);
            toolbarClose.setVisibility(View.GONE);
            //根据偏移百分比 计算透明值
            float scale2 = (float) offset / (scrollRange / 2);
            int alpha2 = (int) (255 * scale2);
            bgToolbarOpen.setBackgroundColor(Color.argb(alpha2, 120, 130, 133));
        } else {//当滑动超过一半,收缩状态下toolbar显示内容,根据收缩位置,改变透明值
            toolbarClose.setVisibility(View.VISIBLE);
            toolbarOpen.setVisibility(View.GONE);
            float scale3 = (float) (scrollRange - offset) / (scrollRange / 2);
            int alpha3 = (int) (255 * scale3);
            bgToolbarClose.setBackgroundColor(Color.argb(alpha3, 125, 130, 133));
        }
        //根据偏移百分比计算扫一扫布局的透明度值
        float scale = (float) offset / scrollRange;
        int alpha = (int) (255 * scale);
        bgContent.setBackgroundColor(Color.argb(alpha, 125, 130, 133));
    }

布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/activity_top_bg"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <include
                layout="@layout/include_open"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />
            <!--pin:折叠后,此布局将固定在顶部。-->
            <!--parallax:折叠时,此布局也会有视差折叠效果。-->
            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:layout_collapseMode="pin">

                <include
                    android:id="@+id/include_toolbar_open"
                    layout="@layout/include_toolbar_open" />

                <include
                    android:id="@+id/include_toolbar_close"
                    layout="@layout/include_toolbar_close" />

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_scrolling" />

</android.support.design.widget.CoordinatorLayout>

layout="@layout/include_open"

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:background="@color/activity_top_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_scan" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="扫一扫"
                android:textColor="@color/white" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_payment" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="付钱"
                android:textColor="@color/white" />

        </LinearLayout>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_transfer" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="收钱"
                android:textColor="@color/white" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@mipmap/ic_card" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="5dp"
                android:text="卡包"
                android:textColor="@color/white" />

        </LinearLayout>


    </LinearLayout>

    <View
        android:id="@+id/bg_content"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

</FrameLayout>

include_open.png
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/activity_top_bg"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_toLeftOf="@+id/iv_contact"
        android:background="@drawable/shape_search"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <ImageView
            android:id="@+id/iv_search"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_search_black_24dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/iv_search"
            android:background="@null"
            android:hint="搜索商品"
            android:paddingLeft="10dp"
            android:textColorHint="@color/black"
            android:textSize="13sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/iv_plus"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:src="@mipmap/ic_add" />

    <ImageView
        android:id="@+id/iv_contact"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/iv_plus"
        android:src="@mipmap/ic_contact" />

    <View
        android:id="@+id/bg_toolbar_open"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
include_toolbar_open.png

``

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/activity_top_bg">

    <ImageView
        android:id="@+id/iv_scan"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:src="@mipmap/ic_scan" />

    <ImageView
        android:id="@+id/iv_pay"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/iv_scan"
        android:src="@mipmap/ic_payment" />

    <ImageView
        android:id="@+id/iv_charge"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/iv_pay"
        android:src="@mipmap/ic_transfer" />

    <ImageView
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/iv_charge"
        android:src="@mipmap/ic_card" />

    <ImageView
        android:id="@+id/iv_plus"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:src="@mipmap/ic_add" />

    <View
        android:id="@+id/bg_toolbar_close"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
include_toolbar_close.png
上一篇下一篇

猜你喜欢

热点阅读