編輯:Android 開發教學
先提醒一句:這裡說的「菜單」並不是按機器 MENU 鍵時出現的那種系統菜單,而是基於 Android SDK 提供的 android.view.animation.TranslateAnimation(extends android.view.animation.Animation),將其實例附加到某個 Layout 上,从而产生帶有動畫進出效果的菜單。
原理:讓 Layout(菜單)從螢幕內(緊貼螢幕邊緣,其實也不一定,視需要的初態與末態而定)動態移動到螢幕外(在外面可以緊貼邊緣,也可以離遠些,這無所謂),就能達到動態菜單的效果。但由於 Animation 有一些古怪的特性(setFill**() 函式的作用效果,這點我在使用某幾個 Animation 時遇到了沒想明白的現象),暫且先不深究,所以我們還得搭配 XML 屬性 android:visibility 一起使用:當 Layout(菜單)顯示時設為 android:visibility="visible",隱藏時設為 "gone"。visibility 共有三個值——"visible" 為可見,"invisible" 為不可見但佔空間,"gone" 為不可見且不佔空間(所謂佔不佔空間,自己寫個 XML 試試就明白了)。
Class TranslateAnimation 的使用:Animation 有兩種定義方式,一種用 Java code,一種用 XML,這裡只介紹用 code 的方式。多的不說,直接看程式碼。
以下是 TranslateAnimationMenu.java(我另外加入了 ScaleAnimation 產生的動畫,各位朋友可對照 SDK 與程式效果來理解):
package com.TranslateAnimation.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
public class TranslateAnimationMenu extends Activity {
//TranslateAnimation showAction, hideAction;
Animation showAction, hideAction;
LinearLayout menu;
Button button;
boolean menuShowed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menu = (LinearLayout) findViewById(R.id.menu);
button = (Button) findViewById(R.id.button);
// 這裡是TranslateAnimation動畫
showAction = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
// 這裡是ScaleAnimation動畫
//showAction = new ScaleAnimation(
// 1.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
showAction.setDuration(500);
// 這裡是TranslateAnimation動畫
hideAction = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f);
// 這裡是ScaleAnimation動畫
//hideAction = new ScaleAnimation(
// 1.0f, 1.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
hideAction.setDuration(500);
menuShowed = false;
menu.setVisibility(View.GONE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (menuShowed) {
menuShowed = false;
menu.startAnimation(hideAction);
menu.setVisibility(View.GONE);
} else {
menuShowed = true;
menu.startAnimation(showAction);
menu.setVisibility(View.VISIBLE);
}
}
});
}
}
以下是 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<LinearLayout android:id="@+id/menu"
android:layout_height="100px" android:layout_width="fill_parent"
android:layout_alignParentTop="true" android:background="#ffffff">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="I am a menu"
android:gravity="center" />
</LinearLayout>
<Button android:id="@+id/button" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:text="Click to show/hide menu" />
</RelativeLayout>
透過 TranslateAnimation 的位移動畫搭配 visibility 的切換,就能做出菜單滑入滑出的流暢效果,點擊按鈕即可反覆顯示與隱藏。
Android學習筆記:實現App初始啟動引導界面
Android學習筆記——App初始啟動界面實現 安卓手機上的很多應用啟動時,都會先閃過一張圖片作為開場,轉瞬即逝。這張圖通常就用應用的圖標,同時充當廣告位。
實現可循環顯示圖片的Android Gallery組件
Gallery組件主要用於橫向顯示圖片列表。按常規做法,Gallery只能有限地顯示指定的圖片——若指定了10張圖,顯示到第10張後就不再繼續。在大多數情況下這沒問題,但
Android 的 Activity 與 Task:理解活動棧的運作方式
要理解 Android 的畫面跳轉行為,就必須先理解 Task。 Task 的概念 A task is a stack of activities. Task 就是一個堆
ApiDemos 解析(43):遠端 Service 的 Started 模式
Remote Service Controller 與先前解析過的 Local Service Controller 都採用 Service 的 Started 模式,兩