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

Android 自製 QQ 風格頂部滑入提示框

編輯:關於 Android 程式設計

QQ 的訊息提示框效果一直做得不錯,於是照著做了一個類似版本。整體上是 Translate 動畫與 FrameLayout 的組合:FrameLayout 讓提示文字疊在標題列之上,Translate 動畫負責從畫面外滑入與滑出。

版面配置 activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- 提示框本體,預設隱藏 -->
        <TextView
            android:id="@+id/toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:layout_marginBottom="20dp"
            android:background="@color/transparent_black"
            android:gravity="center"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:text="@string/toast"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:visibility="invisible" />

        <!-- 標題列 -->
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue"
            android:gravity="center"
            android:padding="5dp"
            android:text="@string/title"
            android:textColor="@color/white"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="@string/hello_world" />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:text="@string/btn" />
    </FrameLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    TextView rootView;
    Button btn;
    Handler handler;
    TextView tv;
    Animation mTranslateInAnimation, mTranslateOutAnimation;
    boolean flag = true;   // 是否可接受點擊

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rootView = (TextView) findViewById(R.id.info);
        btn = (Button) findViewById(R.id.btn);
        tv = (TextView) findViewById(R.id.toast);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                tv.startAnimation(mTranslateOutAnimation);
            }
        };

        // 進場:從上方(自身 -1.5 倍高度處)滑回原位
        mTranslateInAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, -1.5f, Animation.RELATIVE_TO_SELF, 0);
        mTranslateInAnimation.setDuration(1000);

        // 退場:往上方滑出畫面
        mTranslateOutAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1.5f);
        mTranslateOutAnimation.setDuration(1000);

        // 動畫結束後把元件真正隱藏,並恢復可點擊
        mTranslateOutAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                tv.setVisibility(View.INVISIBLE);
                flag = true;
            }

            @Override public void onAnimationRepeat(Animation animation) {}
        });

        btn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag) {
                    flag = false;    // 提示框未退場前不響應新的點擊
                } else {
                    return;
                }

                // 播放進場動畫
                tv.startAnimation(mTranslateInAnimation);
                tv.setVisibility(View.VISIBLE);

                // 停留三秒後觸發退場
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            sleep(3000);
                            handler.sendEmptyMessage(0);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }
}

幾個實作要點

為什麼用 FrameLayout?提示框需要浮在標題列上方,FrameLayout 的子元件會互相堆疊,正好符合這個需求。

為什麼位移量是 -1.5f?相對於自身尺寸的 1.5 倍,是為了確保元件完全移出可視區域,不會殘留一角在畫面上。實際數值可依提示框高度調整。

flag 旗標的作用。提示框顯示期間若允許重複點擊,動畫會互相打架。用一個旗標把互動鎖住,等退場動畫的 onAnimationEnd 再解鎖,行為就穩定多了。

可再優化的地方。這裡用 sleep(3000) 加 Handler 計時,也可以直接用 Handler.postDelayed 或 RxJava 的延遲策略;退場時除了位移,加上 alpha 淡出會更細膩。若要在多個頁面重複使用,建議把這段邏輯抽成獨立的自訂 Toast 類別,對外只暴露 show(CharSequence) 一個方法。

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