編輯:關於 Android 程式設計
學 Android 一段時間了,學過的東西老是忘,於是決定寫成筆記逼自己整理一次。這次要做的是一個常見互動:往上捲動時 Toolbar 收起,往下捲動時滑回來。
這個效果會用到 ObjectAnimator,先把它搞清楚。
ObjectAnimator 屬於屬性動畫,是 ValueAnimator 的子類。它本身就包含了時間引擎與數值計算,所以能直接為某個物件的某個屬性製作動畫。
常用的 ofInt、ofFloat 方法,參數是:(操作物件, 屬性名稱, 動畫起始值, 動畫結束值)。屬性名稱一般是 alpha、translationY、translationX、rotationX、rotationY、scaleX、scaleY。
ObjectAnimator animator = ObjectAnimator.ofFloat(imageBall, "alpha", 0f, 1f);
animator.setDuration(500).start(); // 透明度 0 → 1,歷時 500ms
因為要用自訂的 Toolbar,先在 styles.xml 中改用 NoActionBar 主题:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
避免第一個 item 被 Toolbar 遮住:
View header = new View(this);
header.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
(int) getResources().getDimension(R.dimen.abc_action_bar_default_height_material)));
listView.addHeaderView(header);
這裡用 R.dimen.abc_action_bar_default_height_material 取得 ActionBar 的標準高度,讓墊高值與 Toolbar 一致。
mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
位移超過這個值,才算一次有效滑動,可以避免手指輕微抖動就觸發動畫。
private void scorllEvent() {
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
mFirstY = (int) motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE:
mCurrentY = (int) motionEvent.getY();
if (mCurrentY - mFirstY > mTouchSlop) {
direction = 0; // 向下滑動
} else {
direction = 1; // 向上滑動
}
if (direction == 1) {
if (mShow) {
ToolbarAnim(0);
mShow = false;
}
} else {
if (!mShow) {
ToolbarAnim(1);
mShow = true;
}
}
break;
}
return false; // 不消費事件,讓 ListView 正常捲動
}
});
}
關鍵是最後那個 return false。若回傳 true,事件會被攔下,ListView 就捲不動了——我們只是「旁聽」手勢,不是要接管它。
private void ToolbarAnim(int i) {
if (animator != null && animator.isRunning()) {
animator.cancel();
}
if (i == 0) {
// 顯示:translationY 回到 0
animator = ObjectAnimator.ofFloat(toolbar, "translationY",
toolbar.getTranslationY(), 0);
} else {
// 隱藏:translationY 移到 -height
animator = ObjectAnimator.ofFloat(toolbar, "translationY",
toolbar.getTranslationY(), -toolbar.getHeight());
}
animator.start();
}
動畫前先檢查並取消上一個仍在跑的動畫,否則連續滑動會讓兩段動畫互相打架。
上滑(隱藏):

下滑(顯示):

用日誌印出位移可以驗證:

從輸出可以看到,上滑時 translationY 由 0 走到 -56,下滑時再由 -56 回到 0,與預期一致。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<String> list;
private Toolbar toolbar;
private int mTouchSlop; // 系統認為的最小滑動距離
private int mFirstY; // 第一次觸摸的位置
private int mCurrentY; // 當前觸摸的 Y 軸位置
private boolean mShow = true; // 當前是否顯示
private int direction;
private ObjectAnimator animator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
listView = (ListView) findViewById(R.id.list_view);
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add(i + "");
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_expandable_list_item_1, list);
listView.setAdapter(adapter);
// 墊一個與 ActionBar 等高的 HeaderView,避免首項被 Toolbar 遮住
View header = new View(this);
header.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.MATCH_PARENT,
(int) getResources().getDimension(
R.dimen.abc_action_bar_default_height_material)));
listView.addHeaderView(header);
mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
scorllEvent();
}
private void scorllEvent() {
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
mFirstY = (int) motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE:
mCurrentY = (int) motionEvent.getY();
if (mCurrentY - mFirstY > mTouchSlop) {
direction = 0; // 向下
} else {
direction = 1; // 向上
}
if (direction == 1) {
if (mShow) {
ToolbarAnim(0);
mShow = false;
}
} else {
if (!mShow) {
ToolbarAnim(1);
mShow = true;
}
}
break;
}
return false;
}
});
}
private void ToolbarAnim(int i) {
if (animator != null && animator.isRunning()) {
animator.cancel();
}
if (i == 0) {
animator = ObjectAnimator.ofFloat(toolbar, "translationY",
toolbar.getTranslationY(), 0);
} else {
animator = ObjectAnimator.ofFloat(toolbar, "translationY",
toolbar.getTranslationY(), -toolbar.getHeight());
}
animator.start();
}
}
OnScrollListener 取代 OnTouchListener 更直觀:直接讀 dy 的正負就能判斷方向,不必自己算位移,也不會與 ListView 的滑動判定打架。
21 天學會 Android 開發:SurfaceView 繪圖實戰
上一篇介紹了 MediaPlayer,這次用兩篇的篇幅來談 SurfaceView。網路上介紹 SurfaceView 的寫法層出不窮:有的繼承 SurfaceView
Android 學習筆記:使用 Intent 啟動內建應用
Intent 的基本觀念在 Android 中,Intent 常用來描述「要做什麼」以及「對什麼資料做」。它通常由動作與資料組成。動作表示操作類型,資料則表示受影響的對象
Android 仿人人網滑動側邊欄效果實作
為了在有限螢幕裡塞進更多資訊,很多應用都採用側邊欄設計。如下圖: 核心思路 版面底層放兩個(或多個)並排的視圖,透過控制它們的寬度與左邊距來決定誰露出來。菜單要展開
Android 讀取手機文件夾與文件列表的工具類實作
浏覽本地文件、選擇文件,這類需求背後都需要一個能遍歷目錄的工具類。這裡封裝了一個懶漢式單例,把「列目錄、取父路徑、算大小、排序」這些瑣碎操作一次收攏。 一、工具類 G