Android基於TranslateAnimation的動畫動態菜單
先請注意,這裏的菜單並不是按機器上的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以及程序效果來理解):
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 { /** 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:andro 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: 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: android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Click to show/hide menu" /> </RelativeLayout>

最後更新:2017-04-04 07:03:45