Android动画开发——Animation显示隐藏菜单效果

这里的菜单并不是按机器上的MENU出现在那种菜单,而是基于Android SDK提供的android.view.animation.TranslateAnimation(extends android.view.animation.Animation)类实例后附加到一个Layout上使之产生的有动画出现和隐藏效果的菜单。



Class TranslateAnimation的使用:Animation有两种定义方法,一种是用Java code,一种是用XML,这里只介绍用code来定义.

package com.cn.amenu; 
     
    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 Amenu extends Activity { 
        private Animation mShowAction = null; 
        private Animation mHiddenAction = null; 
        private LinearLayout mMenu = null; 
        private Button mButton = null; 
        private boolean menuShowed; 
     
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
     
            mMenu = (LinearLayout) this.findViewById(R.id.menu1); 
            mButton = (Button) this.findViewById(R.id.button01); 
            // 动画 
            mShowAction = 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); 
            mShowAction.setDuration(500); 
            mHiddenAction = 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); 
             
            mHiddenAction.setDuration(500); 
            menuShowed=false; 
            mMenu.setVisibility(View.GONE); 
            mButton.setOnClickListener(new btn_click()); 
        } 
         
        class btn_click implements OnClickListener{ 
     
            @Override 
            public void onClick(View arg0) { 
                if (menuShowed) { 
                    menuShowed=false; 
                    mMenu.startAnimation(mHiddenAction); 
                    mMenu.setVisibility(View.GONE); 
                }else{ 
                    menuShowed=true; 
                    mMenu.startAnimation(mShowAction); 
                    mMenu.setVisibility(View.VISIBLE); 
                } 
            } 
        } 
     } 


 

 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://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/menu1" 
            android:layout_width="fill_parent" 
            android:layout_height="50px" 
            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" 
            /> 
    <img src="http://dl.iteye.com/upload/attachment/498976/12e90b24-75e4-38bb-84ca-bfb6426c29ae.jpg" mce_src="http://dl.iteye.com/upload/attachment/498976/12e90b24-75e4-38bb-84ca-bfb6426c29ae.jpg" alt=""> 
      
        </LinearLayout> 
        <Button 
            android:id="@+id/button01" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_alignParentBottom="true" 
            android:text="click to show / hide menu" 
        /> 
    </RelativeLayout> 


效果:



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值