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

Android 動畫實戰:從螢幕底部滑出的 PopupWindow

編輯:關於 Android 程式設計

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

以下仿作一個同類效果:

一、面板版面 take_photo_pop.xml

<?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>

三、自訂 PopupWindow

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 即可恢復。

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