Android之動畫主菜單

來源:互聯網
上載者:User

目前,使用者對安卓應用程式的UI設計要求越來越高,因此,掌握一些新穎的設計很有必要,比如菜單,傳統的菜單已經不能滿足使用者的需求。其中圓盤旋轉菜單的實現就比較好,該菜單共分裡外三層導覽功能表.可以依次從外向裡關閉三層菜單,也可以反向開啟,並且伴有圓盤旋轉的動畫效果,首先,看下效果:

源碼下載:http://download.csdn.net/detail/weidi1989/4588807

以下是具體的代碼及解釋:
1. 菜單布局檔案:
大家看到主要有三個RalativeLayout,就是大家看到的三層,但是關於圖片的傾斜 是怎樣實現的呢?實際上是個假象,圖片是正放的,裡面映像是傾斜的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <RelativeLayout        android:layout_width="100dip"        android:layout_height="50dip"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:background="@drawable/level1" >        <ImageButton            android:id="@+id/home"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:background="@drawable/icon_home" />    </RelativeLayout>    <RelativeLayout        android:layout_width="180dip"        android:layout_height="90dip"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:id="@+id/level2"        android:background="@drawable/level2" >        <ImageButton            android:id="@+id/search"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_margin="10dip"            android:background="@drawable/icon_search" />        <ImageButton            android:id="@+id/menu"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerHorizontal="true"            android:layout_margin="6dip"            android:background="@drawable/icon_menu" />        <ImageButton            android:id="@+id/myyouku"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignParentRight="true"            android:layout_margin="10dip"            android:background="@drawable/icon_myyouku" />    </RelativeLayout>    <RelativeLayout        android:layout_width="280dip"        android:layout_height="140dip"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:id="@+id/level3"        android:background="@drawable/level3" >        <ImageButton            android:id="@+id/c1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_marginBottom="6dip"            android:layout_marginLeft="12dip"            android:background="@drawable/channel1" />        <ImageButton            android:id="@+id/c2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_above="@id/c1"            android:layout_marginBottom="12dip"            android:layout_marginLeft="28dip"            android:background="@drawable/channel2" />        <ImageButton            android:id="@+id/c3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_above="@id/c2"            android:layout_marginBottom="6dip"            android:layout_marginLeft="8dip"            android:layout_toRightOf="@id/c2"            android:background="@drawable/channel3" />        <ImageButton            android:id="@+id/c4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerHorizontal="true"            android:layout_margin="6dip"            android:background="@drawable/channel4" />                        <ImageButton            android:id="@+id/c5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_above="@+id/c6"            android:layout_marginBottom="6dip"            android:layout_marginRight="8dip"            android:layout_toLeftOf="@+id/c6"            android:background="@drawable/channel5" />                        <ImageButton            android:id="@+id/c6"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_above="@+id/c7"            android:layout_marginBottom="12dip"            android:layout_marginRight="28dip"            android:layout_alignParentRight="true"            android:background="@drawable/channel6" />                                <ImageButton            android:id="@+id/c7"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_marginBottom="6dip"            android:layout_marginRight="12dip"            android:layout_alignParentRight="true"            android:background="@drawable/channel7" />    </RelativeLayout></RelativeLayout>

2.MainActivity:

package cn.oce.youku;import cn.itcast.youku.R;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.RelativeLayout;public class MainActivity extends Activity {private ImageButton home;private ImageButton menu;private RelativeLayout level2;private RelativeLayout level3;private boolean isLevel2Show = true;private boolean isLevel3Show = true;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);home = (ImageButton) findViewById(R.id.home);menu = (ImageButton) findViewById(R.id.menu);level2 = (RelativeLayout) findViewById(R.id.level2);level3 = (RelativeLayout) findViewById(R.id.level3);menu.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(isLevel3Show){//隱藏3級導覽功能表MyAnimation.startAnimationOUT(level3, 500, 0);}else {//顯示3級導覽功能表MyAnimation.startAnimationIN(level3, 500);}isLevel3Show = !isLevel3Show;}});home.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(!isLevel2Show){//顯示2級導覽功能表MyAnimation.startAnimationIN(level2, 500);} else {if(isLevel3Show){//隱藏3級導覽功能表MyAnimation.startAnimationOUT(level3, 500, 0);//隱藏2級導覽功能表MyAnimation.startAnimationOUT(level2, 500, 500);isLevel3Show = !isLevel3Show;}else {//隱藏2級導覽功能表MyAnimation.startAnimationOUT(level2, 500, 0);}}isLevel2Show = !isLevel2Show;}});}}

3.自訂動畫類MyAnimation:

package cn.oce.youku;import android.view.View;import android.view.ViewGroup;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.RotateAnimation;public class MyAnimation {//入動畫public static void startAnimationIN(ViewGroup viewGroup, int duration){for(int i = 0; i < viewGroup.getChildCount(); i++ ){viewGroup.getChildAt(i).setVisibility(View.VISIBLE);//設定顯示viewGroup.getChildAt(i).setFocusable(true);//獲得焦點viewGroup.getChildAt(i).setClickable(true);//可以點擊}Animation animation;/** * 旋轉動畫 * RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue) * fromDegrees 開始旋轉角度 * toDegrees 旋轉到的角度 * pivotXType X軸 參照物 * pivotXValue x軸 旋轉的參考點 * pivotYType Y軸 參照物 * pivotYValue Y軸 旋轉的參考點 */animation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);animation.setFillAfter(true);//停留在動畫結束位置animation.setDuration(duration);viewGroup.startAnimation(animation);}//齣動畫public static void startAnimationOUT(final ViewGroup viewGroup, int duration , int startOffSet){Animation animation;animation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);animation.setFillAfter(true);//停留在動畫結束位置animation.setDuration(duration);animation.setStartOffset(startOffSet);animation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationRepeat(Animation animation) {// TODO Auto-generated method stub}@Overridepublic void onAnimationEnd(Animation animation) {for(int i = 0; i < viewGroup.getChildCount(); i++ ){viewGroup.getChildAt(i).setVisibility(View.GONE);//設定顯示viewGroup.getChildAt(i).setFocusable(false);//獲得焦點viewGroup.getChildAt(i).setClickable(false);//可以點擊}}});viewGroup.startAnimation(animation);}}

這樣,一個三級導航圓盤旋轉菜單就完成了,以後完全可以借鑒這些優秀的UI設計,甚至根據新的需求,可以做出更好的UI。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.