Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 Material Design 復刻釘釘通訊錄詳情頁

編輯:關於 Android 程式設計

前陣子翻釘釘的時候,被通訊錄詳情頁的視覺效果吸引住了——頭部圖片漸變、名字縮放、圓形頭像懸浮、下方內容推滑上移,過渡得非常順。正好在手頭研究 Material Design,於是想試試能不能純用 Android 原生控件把這種效果做出來。

先看效果:

釘釘還有一個頭像隨滾動聯動的細節,這裡暫未實現,略有差別。其餘主要特點有四個:名字大小隨滾動縮放、圓形頭像固定懸浮、背景高斯模糊、下方詳情內容推滑。這些全部可以用 Material Design 自帶的元素實現,效果相當不錯。

布局結構

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/imageView_bg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@null"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.6" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#80000000"
                android:contentDescription="@null"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.6" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginStart="@dimen/dp68"
                android:layout_marginEnd="@dimen/dp68"
                android:orientation="vertical"
                android:paddingBottom="@dimen/dp300">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/dp100"
                    android:text="@string/company_info"
                    android:textColor="#3E50B4"
                    android:textSize="@dimen/sp50" />

                <TextView
                    android:id="@+id/textView_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp60"
                    android:textColor="#333333"
                    android:textSize="@dimen/sp45" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp10"
                    android:text="@string/contacts_name"
                    android:textColor="#b7b7b7"
                    android:textSize="@dimen/sp40" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="@dimen/dp40"
                    android:background="#ececec" />

                <TextView
                    android:id="@+id/textView_phone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp40"
                    android:textColor="#222222"
                    android:textSize="@dimen/sp45" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp10"
                    android:text="@string/contacts_phone"
                    android:textColor="#b7b7b7"
                    android:textSize="@dimen/sp40" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="@dimen/dp40"
                    android:background="#ececec" />

                <TextView
                    android:id="@+id/textView_email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp40"
                    android:textColor="#222222"
                    android:textSize="@dimen/sp45" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp10"
                    android:text="@string/contacts_email"
                    android:textColor="#b7b7b7"
                    android:textSize="@dimen/sp40" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="@dimen/dp40"
                    android:background="#ececec" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp100"
                    android:text="@string/person_info"
                    android:textColor="#3E50B4"
                    android:textSize="@dimen/sp50" />

                <TextView
                    android:id="@+id/textView_birthday"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp40"
                    android:textColor="#222222"
                    android:textSize="@dimen/sp45" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp10"
                    android:text="@string/contacts_birthday"
                    android:textColor="#b7b7b7"
                    android:textSize="@dimen/sp40" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="@dimen/dp40"
                    android:background="#ececec" />

                <TextView
                    android:id="@+id/textView_sex"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp40"
                    android:textColor="#222222"
                    android:textSize="@dimen/sp45" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp10"
                    android:text="@string/contacts_sex"
                    android:textColor="#b7b7b7"
                    android:textSize="@dimen/sp40" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="@dimen/dp40"
                    android:background="#ececec" />

                <TextView
                    android:id="@+id/textView_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp40"
                    android:textColor="#222222"
                    android:textSize="@dimen/sp45" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dp10"
                    android:text="@string/contacts_address"
                    android:textColor="#b7b7b7"
                    android:textSize="@dimen/sp40" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_marginTop="@dimen/dp40"
                    android:background="#ececec" />
            </LinearLayout>

            <android.support.design.widget.FloatingActionButton
                android:background="@android:color/transparent"
                app:borderWidth="0dp" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:background="?attr/colorPrimary" />

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

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

    <android.support.v4.widget.NestedScrollView>
        <!-- 可滑動的詳情內容 -->
    </android.support.v4.widget.NestedScrollView>

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

中間那個圓形頭像由 FloatingActionButton 實現,關鍵是把背景設為透明、邊線寬度設為 0:

android:background="@android:color/transparent"
app:borderWidth="0dp"

整體結構的分工是這樣的:可折疊的頭部區域用 CollapsingToolbarLayout,它必須被 AppBarLayout 包裹;模糊背景就是一個 ImageView;固定頂部的導航欄是 Toolbar;下方可滑動的具體內容用 NestedScrollView,作用等同於 ScrollView。

背景圖片的模糊處理

圖片從網絡下載後再做模糊。這裡用 Picasso 加載,它支持自定義 Transformation:

Picasso.with(getApplicationContext())
       .load(contactsInfo.getImgSrc())
       .transform(new BlurTransformation())
       .placeholder(R.drawable.default_pic)
       .error(R.drawable.default_pic)
       .into(mImageViewBg);

/** 模糊轉換器 */
class BlurTransformation implements Transformation {
    public BlurTransformation() {
        super();
    }

    @Override
    public Bitmap transform(Bitmap bitmap) {
        return PictureUtil.blurBitmap(getApplicationContext(), bitmap, 15f);
    }

    @Override
    public String key() {
        return "blur";
    }
}

核心的模糊算法基於 Renderscript:

/** 圖片模糊 */
public static Bitmap blurBitmap(Context context, Bitmap bitmap, float blur) {
    // 創建一張與原圖等大的空 bitmap
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    // 初始化 Renderscript
    RenderScript rs = RenderScript.create(context.getApplicationContext());

    // 創建內建的模糊 Script
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // 建立輸入輸出的 Allocation
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    // 設置模糊半徑並執行
    blurScript.setRadius(blur);
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    // 把結果拷貝回 outBitmap
    allOut.copyTo(outBitmap);

    bitmap.recycle();

    // 銷毀 Renderscript,避免內存洩漏
    rs.destroy();
    return outBitmap;
}

CollapsingToolbarLayout 的關鍵屬性

CollapsingToolbarLayout 提供了一個可折疊的 Toolbar,它繼承自 FrameLayout。給它設置 layout_scrollFlags 後,內部子控件(ImageView、Toolbar 等)就能響應 layout_behavior 事件,做出相應的滾動行為(移出屏幕或固定在頂端)。

scroll —— 想滾動就必須加上這個標志。

enterAlways —— 實現 quick return 效果,向下滑動時立即顯示 View(比如 Toolbar)。

exitUntilCollapsed —— 向上滾動時收縮 View,但 Toolbar 會固定留在頂部。

enterAlwaysCollapsed —— 當 View 設置了 minHeight 並搭配此標志時,它只能以最小高度進入,只有滾動到頂部才會擴展到完整高度。

另外幾個常用屬性:

app:contentScrim —— CollapsingToolbarLayout 完全收縮後的背景顏色。

app:expandedTitleMarginStart —— 擴展狀態(未收縮時)標題向左填充的距離。

app:layout_collapseMode(折疊模式)—— 在 CollapsingToolbarLayout 中定義,於其子視圖中使用:

pin —— 設置為該模式後,CollapsingToolbarLayout 完全收縮時 Toolbar 仍保留在屏幕上。

parallax —— 設置為該模式後,內容滾動時子 View(如 ImageView)同時滾動,形成視差效果,通常與 layout_collapseParallaxMultiplier 搭配。這裡的視差含義是:值設為 0 時圖片跟著一起滾,設為 1 時圖片不動。

layout_collapseParallaxMultiplier(視差因子)—— 取值 0~1。

標題的設置

使用 CollapsingToolbarLayout 時必須把 title 設置在它自己身上,設到 Toolbar 上是不會顯示的:

mCollapsingToolbarLayout.setTitle("leeshenzhou");

標題顏色需要分別設置擴展態與收縮態:

Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

CollapsingToolbarLayout mCollapsingToolbarLayout =
        (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
mCollapsingToolbarLayout.setTitle("leeshenzhou");
mCollapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);  // 未收縮時的標題顏色
mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN); // 收縮後 Toolbar 上的顏色

懸浮按鈕的綁定

那個隨 CollapsingToolbarLayout 一起滾動的 FloatingActionButton,是通過 CoordinatorLayout 的 layout_anchor 實現的。設置之後,CoordinatorLayout 會把懸浮視圖與指定視圖綁定在一起,再用 layout_anchorGravity 控制它停靠的位置。

幾個實踐要點

1. AppBarLayout 的高度要固定。
2. CollapsingToolbarLayout 的子視圖設置 layout_collapseMode;若想讓 Toolbar 固定,一定要設為 pin 模式。parallax 其實不是必須的——滾動效果本身由 CollapsingToolbarLayout 實現,子 View 設置它只是讓自己跟著滾。
3. 懸浮視圖記得設置 app:layout_anchor 與 app:layout_anchorGravity。
4. 標題內容由 CollapsingToolbarLayout 呈現而非 Toolbar;滑動後標題固定在頂部也是它內部完成的,與是否設置 layout_collapseMode 有關。
5. 想移除 Toolbar 導航欄的背景色,在 XML 中加 android:background="?attr/colorPrimary"。
6. 想讓 CollapsingToolbarLayout 收起後呈現純色,加 app:contentScrim="?attr/colorPrimary";去掉這行則會顯示它自身的背景。

源碼地址:https://github.com/chuwuwang/OnTheWay(代碼仍在修改中,效果請切換到 1.2.0 tag 查看)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved