android 动画

要实现的效果如图:


代码很简单,主要是TranslateAnimation类的

publicTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

Constructor to use when building a TranslateAnimation from code

Parameters
fromXTypefromXValuetoXTypetoXValuefromYTypefromYValuetoYTypetoYValue
Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise
代码如下:
Java代码
  1. package com.lenovo.wufl.translateanimationmenu;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.TranslateAnimation;  
  9. import android.widget.Button;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.TextView;  
  12.   
  13. public class TranslateAnimationMenuActivity extends Activity implements  
  14.         OnClickListener {  
  15.     private TextView mMenu;  
  16.     private Button mButton;  
  17.     private TranslateAnimation mShowAnimation;  
  18.     private TranslateAnimation mHideAnimation;  
  19.     private boolean isShow;  
  20.   
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.   
  27.         initView();  
  28.         initAnimation();  
  29.     }  
  30.   
  31.     private void initView() {  
  32.         mMenu = (TextView) findViewById(R.id.menu);  
  33.         mButton = (Button) findViewById(R.id.button);  
  34.         mButton.setOnClickListener(this);  
  35.         mMenu.setVisibility(View.GONE);  
  36.         isShow = false;  
  37.     }  
  38.   
  39.     private void initAnimation() {  
  40.         // 从自已-1倍的位置移到自己原来的位置  
  41.         mShowAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,  
  42.                 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,  
  43.                 Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,  
  44.                 0.0f);  
  45.         mHideAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,  
  46.                 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,  
  47.                 Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,  
  48.                 -1.0f);  
  49.         mShowAnimation.setDuration(500);  
  50.         mHideAnimation.setDuration(500);  
  51.   
  52.     }  
  53.   
  54.     @Override  
  55.     public void onClick(View v) {  
  56.         if (v.getId() == R.id.button) {  
  57.             if (isShow) {  
  58.                 isShow = false;  
  59.                 mMenu.startAnimation(mHideAnimation);  
  60.                 mMenu.setVisibility(View.GONE);  
  61.             } else {  
  62.                 isShow = true;  
  63.                 mMenu.startAnimation(mShowAnimation);  
  64.                 mMenu.setVisibility(View.VISIBLE);  
  65.             }  
  66.         }  
  67.     }  
  68. }  

布局:
Html代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/menu"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="68dp"  
  11.         android:background="@android:color/darker_gray"  
  12.         android:gravity="center"  
  13.         android:text="I am a menu"  
  14.         android:textColor="@android:color/background_dark" />  
  15.   
  16.     <Button  
  17.         android:id="@+id/button"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignParentBottom="true"  
  21.         android:text="show/hide menu" />  
  22.   
  23. </RelativeLayout> 
转载自:http://xiaoruanjian.iteye.com/blog/1365929

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值