編輯:關於 Android 程式設計
最近做專案做得有點頭痛。原本想直接搬 ListView 的註解版(Annotation)來用,那是一個可以做出類似 Android 通知欄「滑動刪除」效果的開源實作,據說是某位高手獨力完成的,這裡先向他致敬一下。
說實話,它的原理並不難理解:偵測使用者的滑動距離與速度,再依此判斷該做什麼操作。想看懂思路,直接去玩一下 Google Keep 兩種列表模式下的滑動刪除就很清楚了。
不幸的是,我在版面配置裡同時用到了 Fragment 和 ViewPager,結果產生了手勢衝突。為此我改寫了 library 裡的手勢偵測方法,最後勉強改出一個能跑的版本——但是!效果實在慘不忍睹,只好作罷。
反覆權衡之後,我決定改用「點擊 ListView 項目展開」的方式來達成我想要的效果。
先用 getLayoutParams() 取得指定元件的 LayoutParams,接著修改它的屬性,最後再寫回去:
LinearLayout.LayoutParams lp = (RelativeLayout.LayoutParams) deletButton.getLayoutParams();
lp.width = btnWidth;
lp.leftMargin = 10;
deletButton.setLayoutParams(lp);
getMeasuredHeight() 回傳的是原始測量高度,與螢幕無關;getHeight() 回傳的是元件在螢幕上實際顯示的高度。當內容可以被螢幕完整包住時,兩者數值相等;只有當 View 超出螢幕之後,差異才會顯現出來——此時 getMeasuredHeight() 等於 getHeight() 加上螢幕外沒被顯示的那段高度。
MeasureSpec 常出現在自訂 View 中,因為我們往往需要覆寫 onMeasure(int widthMeasureSpec, int heightMeasureSpec),由它來決定自訂元件在螢幕上的尺寸。這兩個傳入參數並不是單純的數字,而是把「模式」與「尺寸」打包在一起的複合值,必須分別取出:
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
mode 共有三種:
50dip)或 FILL_PARENT 時,元件大小已經確定。需要自行設定時,可以呼叫 MeasureSpec.makeMeasureSpec(int size, int mode)。
先看一下 demo 的執行結果,讀者可以自行再擴充:

效果還算可以。圖示畫得有點呆萌,還請別見笑(自己畫的,可憐的程式員一隻)。
先上程式碼:
public class ViewExpandAnimation extends Animation {
private View mAnimationView = null;
private LayoutParams mViewLayoutParams = null;
private int mStart = 0;
private int mEnd = 0;
public ViewExpandAnimation(View view) {
animationSettings(view, 500);
}
public ViewExpandAnimation(View view, int duration) {
animationSettings(view, duration);
}
private void animationSettings(View view, int duration) {
setDuration(duration);
mAnimationView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
mStart = mViewLayoutParams.bottomMargin;
mEnd = (mStart == 0 ? (0 - view.getHeight()) : 0);
view.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
mViewLayoutParams.bottomMargin = mStart + (int) ((mEnd - mStart) * interpolatedTime);
mAnimationView.requestLayout();
} else {
mViewLayoutParams.bottomMargin = mEnd;
mAnimationView.requestLayout();
if (mEnd != 0) {
mAnimationView.setVisibility(View.GONE);
}
}
}
}
動效本身的程式碼並不多。setDuration(duration) 用來設定動畫長度;接著操作 RelativeLayout.bottomMargin,官方文件對它的說明是「子元件的下邊界,單位為像素」;mAnimationView.requestLayout() 內部會呼叫 layout 流程,讓版面重新配置一次。
Adapter 的覆寫相對單純,這裡就不貼全部程式碼,只提幾個要注意的地方。首先在 getView(...) 中載入自訂的項目版面:
convertView = myInflater.inflate(R.layout.expand_item, null);
對應的版面配置如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip" >
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center|left"
android:text="name"
android:textColor="#000000"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<Button android:id="@+id/btnOpen" ... />
<Button android:id="@+id/btnView" ... />
<Button android:id="@+id/btnWarning" ... />
</RelativeLayout>
</LinearLayout>
我讓第二個 RelativeLayout(footer)在預設狀態下是隱藏的。在 BaseAdapter 中的設定如下:
RelativeLayout footer = (RelativeLayout) convertView.findViewById(R.id.footer);
int widthSpec = MeasureSpec.makeMeasureSpec((int) (mLcdWidth - 10 * mDensity), MeasureSpec.EXACTLY);
footer.measure(widthSpec, 0);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) footer.getLayoutParams();
params.bottomMargin = -footer.getMeasuredHeight();
footer.setVisibility(View.GONE);
最後在 setOnItemClickListener 裡觸發動畫:點擊時展開,再點一次則收合,並且同一時間只允許一個項目展開(我認為使用者不太可能同時去收合兩個項目):
if (mLastTouchTag != null) {
View temp = arg0.findViewWithTag(mLastTouchTag);
if (temp != null) {
View footTemp = temp.findViewById(R.id.footer);
if (footTemp != null && footTemp.getVisibility() != View.GONE) {
footTemp.startAnimation(new ViewExpandAnimation(footTemp));
}
}
}
mLastTouchTag = (ViewHolder) v.getTag();
View footer = v.findViewById(R.id.footer);
footer.startAnimation(new ViewExpandAnimation(footer));
就先寫到這裡,有問題歡迎留言。
Android TV 應用入門:從建立專案到模擬器運行
近來開始接觸 Android TV 開發,發現這方面的中文資料還比較少;但隨著產品與用戶的不斷升級,Android TV 與 Android Watch 的開發離我們越來
自訂 SeekBar 控制音量並與系統音量同步更新
自訂 SeekBar 控制音量同步更新 身為開發者,SeekBar 你一定會碰到,那麼如何自訂 SeekBar,以及如何讓它控制的音量與系統的音量同步更新?
OSGi ServiceListener實戰:動態監聽服務註冊與變更
前言 在OSGi的世界裡,動態性是最鮮明的特徵之一。Bundle可以在執行期安裝、啟動、停止、更新與移除,而服務也可能隨時被註冊、修改或取消註冊。若應用需要即時感知這些變
Android 菜單的三種實現方式:Options Menu、Context Menu 與 Submenu
菜單是許多應用不可或缺的一部分,Android 更是如此——幾乎所有 Android 設備都有一個「menu」鍵。SDK 提供了三種類型:options menu(常規菜