編輯:關於 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 提供了一個可折疊的 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 查看)
dumpsys 使用指南:用一行命令看清 Android 系統服務的狀態
一、dumpsys 是什麼 dumpsys 執行在裝置上,能輸出各項系統服務的當前狀態。官方描述是「The dumpsys tool runs on the device
Android 動畫入門:幀動畫與補間動畫詳解
寫在前面: 為了讓用戶交互更流暢自然,動畫已經成為一款應用中必不可少的元素。Android 中常用的動畫大致分為三類:最早的幀動畫、補間動畫,以及 3.0 之後加入的屬性
Android 多行文字排版:左右對齊與自動換行的六種做法
需求說明 這個功能要讓多行文字分成左右兩部分:左側是長度不一的標題,右側是描述內容。右側描述必須統一對齊,若內容換行,也要從相同起始位置繼續排。 常見解法 一種方式是自
Gradle 打包三板斧:在 Eclipse 專案上搞定多渠道與差異化配置
同一個 App,要因應不同應用市場換渠道標識、換包名、換引導頁、換 icon——如果全靠手工改再一個個打包,那是純體力活,而且極易出錯。Gradle 把這件事變成了改幾行