編輯:關於 Android 程式設計
本文示範一個常見的互動效果:點擊後,面板從螢幕底部滑出。拍照/選圖、分享選單等情境幾乎都會用到這種設計。

以下仿作一個同類效果:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center"
android:text="修改頭像"
android:textColor="#8a8a8a"
android:textSize="15sp" />
<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#00c7c0" />
<Button
android:id="@+id/btn_take_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:textColor="#0e61aa"
android:textSize="18sp" />
<Button
android:id="@+id/btn_pick_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="從相簿選擇"
android:textColor="#0e61aa"
android:textSize="18sp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginTop="15dip"
android:text="取消"
android:textColor="#0e61aa"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
面板出現時由下往上滑入,關閉時由上往下滑出,因此需要兩個動畫檔。
退出動畫 pop_exit_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
顯示動畫 pop_enter_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
public class TakePhotoPopWin extends PopupWindow {
private Context mContext;
private View view;
private Button btn_take_photo, btn_pick_photo, btn_cancel;
public TakePhotoPopWin(Context mContext, View.OnClickListener itemsOnClick) {
this.view = LayoutInflater.from(mContext).inflate(R.layout.take_photo_pop, null);
btn_take_photo = (Button) view.findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) view.findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
// 取消鈕直接關閉
btn_cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
this.setOutsideTouchable(true);
// 點擊面板外的地方就關閉
this.view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = view.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
this.setContentView(this.view);
// 高度填滿,讓半透明遮罩覆蓋整個螢幕
this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
this.setFocusable(true);
// 半透明背景,做出遮罩效果
ColorDrawable dw = new ColorDrawable(0xb0000000);
this.setBackgroundDrawable(dw);
// 套用進出場動畫
this.setAnimationStyle(R.style.take_photo_anim);
}
}
這裡最容易漏掉的是最後一行 setAnimationStyle()。少了它,前面寫的兩個動畫檔完全不會生效。
對應的 style 定義:
<style name="take_photo_anim" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
<item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
</style>
public void showPopFormBottom(View view) {
TakePhotoPopWin takePhotoPopWin = new TakePhotoPopWin(this, onClickListener);
// showAtLocation(View parent, int gravity, int x, int y)
takePhotoPopWin.showAtLocation(findViewById(R.id.main_view), Gravity.CENTER, 0, 0);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_take_photo:
System.out.println("btn_take_photo");
break;
case R.id.btn_pick_photo:
System.out.println("btn_pick_photo");
break;
}
}
};
進入新頁面、註冊登入時常見的「等待中」提示,其中那個持續旋轉的小圖示,用的就是旋轉動畫。

關於這類自訂 ProgressDialog 的做法,可參考另有一篇整理。
一個實務上的提醒:若使用 ColorDrawable 搭配 MATCH_PARENT 做遮罩,需注意 PopupWindow 的 setOutsideTouchable 與背景設定會互相影響點擊行為。若發現點擊外部無效,通常是把背景設為 null 導致的,改回半透明 ColorDrawable 即可恢復。
開源元件 GridViewWithHeaderAndFooter 使用與原始碼解析
GridViewWithHeaderAndFooter 這個元件,讓你能像使用 ListView 一樣,為 GridView 加上 Header 與 Footer。專案網
深入淺出:Android 應用中的記憶體洩漏全解析
Android 應用中的記憶體洩漏全解析 記憶體洩漏與記憶體溢出的區別 記憶體溢出(out of memory)指的是程式在申請記憶體時,沒有足夠的空間可供使用。每個
Android 自定義 View 實戰:打造水波紋進度球
在日常 Android 開發中,自定義控件的出鏡率其實相當高。設計與產品為了讓畫面更精緻、互動更有意思,往往會畫出各種炫目的效果圖,而最終把它落地成可運行代碼的,永遠是我
Android 客戶端註冊與登入實作詳解(上)
開發 Android App 幾乎一定會跟伺服器打交道,其中帳號的註冊與登入更是必備技能。本文聚焦「客戶端如何向伺服器發起註冊請求」,伺服器端收到請求後的處理不在討論範圍