標籤:android style blog http ar io color os 使用
先請注意,這裡的菜單並不是按機器上的 MENU出現在那種菜單,而是基於Android SDK 提供的 android.view.animation.TranslateAnimation(extends android.view.animation.Animation)類執行個體後附加到一個 Layout 上使之產生的有動畫出現和隱藏效果的菜單。
原理:Layout(菜單)從螢幕內(挨著螢幕邊沿,其實並非一定,視需要的初態和末態而定)動態移動到螢幕外(在外面可以挨著邊沿,也可以離遠點,這個無所謂了),這樣就可以達到動態菜單的效果了。但是由於Animation的一些奇怪特性(setFill**() 函數的作用效果,這個在我使用的某幾個Animation 當中出現了沒有想明白的效果),就暫不理會這個東西了,所以使得我們還需要用上 XML屬性android:visibility。當Layout(菜單)顯示的時候,設定android:visibility="visible",當Layout(菜單)隱藏的時候,設定android:visibility="gone",這裡 android:visibility 可以有3個值,"visible"為可見,"invisible"為不可見但占空間,"gone"為不可見且不佔空間(所謂的佔不佔空間,這個可以自己寫個 XML來試試就明白了)。
Class TranslateAnimation 的使用:Animation有兩種定義方法,一種是用Java code,一種是用XML,這裡只介紹用 code來定義(因為用XML來定義的那種我沒用過。。嘿嘿。。)。多的不說,看代碼。
這裡是TranslateAnimationMenu.java(我在裡面還另加入了 ScaleAnimation產生的動畫,各位朋友可以照著 SDK以及程式效果來理解):
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 { /** Called when the activity is first created. */ // 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) { // TODO Auto-generated method stub 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="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:id="@+id/menu" android:layout_width="fill_parent" android:layout_height="100px" android:layout_alignParentTop="true" android:background="#ffffff" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="I am a menu" /> </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>
Android 基於TranslateAnimation 的動畫動態菜單(非系統menu菜單)