超炫button按钮动画效果

0_13318021996Z1W.gif.png 
今天从网上看到一个这样的效果,感觉很有创意,自己也搜集了一些资料,仿照着实现了一下。    下面就直接上源码:
    首先看一下布局文件:
  1.     <?xml version="1.0" encoding="utf-8"?>  
  2.     <RelativeLayout android:layout_width="fill_parent"  
  3.         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:background="#ffffff">  
  5.       
  6.         <ImageView android:layout_width="wrap_content"  
  7.             android:layout_height="fill_parent" android:background="@drawable/splash_shadow"  
  8.             android:layout_marginLeft="50dip" />  
  9.       
  10.         <RelativeLayout android:id="@+id/relativeLayout_top_bar"  
  11.             android:layout_width="fill_parent" android:layout_height="40dip"  
  12.             android:background="@drawable/qa_bar">  
  13.       
  14.             <ImageView android:layout_width="60dip"  
  15.                 android:layout_height="20dip" android:background="@drawable/qa_logo"  
  16.                 android:layout_centerInParent="true" />  
  17.       
  18.         </RelativeLayout>  
  19.       
  20.         <TextView android:layout_below="@id/relativeLayout_top_bar"  
  21.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  22.             android:textSize="20sp" android:text="Tap to Refresh"  
  23.             android:layout_centerHorizontal="true" android:layout_marginTop="50dip" android:textStyle="bold"/>  
  24.       
  25.       
  26.         <Button android:id="@+id/button_composer_sleep"  
  27.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  28.             android:background="@drawable/composer_sleep"  
  29.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  30.             android:layout_alignParentBottom="true">  
  31.         </Button>  
  32.       
  33.         <Button android:id="@+id/button_composer_thought"  
  34.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  35.             android:background="@drawable/composer_thought"  
  36.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  37.             android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_sleep">  
  38.         </Button>  
  39.       
  40.         <Button android:id="@+id/button_composer_music"  
  41.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  42.             android:background="@drawable/composer_music"  
  43.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  44.             android:layout_alignParentBottom="true">  
  45.         </Button>  
  46.       
  47.         <Button android:id="@+id/button_composer_place"  
  48.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  49.             android:background="@drawable/composer_place"  
  50.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  51.             android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_music">  
  52.         </Button>  
  53.       
  54.       
  55.         <Button android:id="@+id/button_composer_with"  
  56.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  57.             android:background="@drawable/composer_with"  
  58.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  59.             android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_place">  
  60.         </Button>  
  61.       
  62.       
  63.         <Button android:id="@+id/button_composer_camera"  
  64.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  65.             android:background="@drawable/composer_camera"  
  66.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  67.             android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_with">  
  68.         </Button>  
  69.       
  70.       
  71.         <Button android:id="@+id/button_friends_delete"  
  72.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  73.             android:background="@drawable/friends_delete"  
  74.             android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"  
  75.             android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_camera">  
  76.         </Button>  
  77.       
  78.       
  79.     </RelativeLayout>  
复制代码
实现button按钮动画效果的核心类:
  1. import android.R.anim;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.util.Log;  
  5. import android.view.Display;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.animation.Animation;  
  9. import android.view.animation.RotateAnimation;  
  10. import android.view.animation.ScaleAnimation;  
  11. import android.view.animation.TranslateAnimation;  
  12. import android.view.animation.Animation.AnimationListener;  
  13. import android.widget.Button;  
  14. import android.widget.RelativeLayout.LayoutParams;  
  15.   
  16. public class PathButtonActivity extends Activity   
  17. {  
  18.     private Button buttonCamera, buttonDelete, buttonWith, buttonPlace, buttonMusic, buttonThought, buttonSleep;  
  19.     private Animation animationTranslate, animationRotate, animationScale;  
  20.     private static int width, height;  
  21.     private LayoutParams params = new LayoutParams(0, 0);  
  22.     private static Boolean isClick = false;  
  23.       
  24.     /** Called when the activity is first created. */  
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState)   
  27.     {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.path_button);  
  30.           
  31.         initialButton();  
  32.     
  33.           
  34.     }  
  35.     private void initialButton()   
  36.     {  
  37.         // TODO Auto-generated method stub  
  38.         Display display = getWindowManager().getDefaultDisplay();   
  39.         height = display.getHeight();    
  40.         width = display.getWidth();  
  41.         Log.v("width  & height is:", String.valueOf(width) + ", " + String.valueOf(height));  
  42.           
  43.         params.height = 50;  
  44.         params.width = 50;  
  45.         //设置边距  (int left, int top, int right, int bottom)  
  46.         params.setMargins(10, height - 98, 0, 0);  
  47.           
  48.         buttonSleep = (Button) findViewById(R.id.button_composer_sleep);      
  49.         buttonSleep.setLayoutParams(params);  
  50.           
  51.         buttonThought = (Button) findViewById(R.id.button_composer_thought);  
  52.         buttonThought.setLayoutParams(params);  
  53.           
  54.         buttonMusic = (Button) findViewById(R.id.button_composer_music);  
  55.         buttonMusic.setLayoutParams(params);  
  56.           
  57.         buttonPlace = (Button) findViewById(R.id.button_composer_place);  
  58.         buttonPlace.setLayoutParams(params);  
  59.           
  60.         buttonWith = (Button) findViewById(R.id.button_composer_with);  
  61.         buttonWith.setLayoutParams(params);  
  62.   
  63.         buttonCamera = (Button) findViewById(R.id.button_composer_camera);  
  64.         buttonCamera.setLayoutParams(params);  
  65.           
  66.         buttonDelete = (Button) findViewById(R.id.button_friends_delete);         
  67.         buttonDelete.setLayoutParams(params);  
  68.           
  69.         buttonDelete.setOnClickListener(new OnClickListener()   
  70.         {  
  71.                   
  72.             @Override  
  73.             public void onClick(View v)   
  74.             {  
  75.                 // TODO Auto-generated method stub                    
  76.                 if(isClick == false)  
  77.                 {  
  78.                     isClick = true;  
  79.                     buttonDelete.startAnimation(animRotate(-45.0f, 0.5f, 0.45f));                     
  80.                     buttonCamera.startAnimation(animTranslate(0.0f, -180.0f, 10, height - 240, buttonCamera, 80));  
  81.                     buttonWith.startAnimation(animTranslate(30.0f, -150.0f, 60, height - 230, buttonWith, 100));  
  82.                     buttonPlace.startAnimation(animTranslate(70.0f, -120.0f, 110, height - 210, buttonPlace, 120));  
  83.                     buttonMusic.startAnimation(animTranslate(80.0f, -110.0f, 150, height - 180, buttonMusic, 140));  
  84.                     buttonThought.startAnimation(animTranslate(90.0f, -60.0f, 175, height - 135, buttonThought, 160));  
  85.                     buttonSleep.startAnimation(animTranslate(170.0f, -30.0f, 190, height - 90, buttonSleep, 180));  
  86.                   
  87.                 }  
  88.                 else  
  89.                 {                     
  90.                     isClick = false;  
  91.                     buttonDelete.startAnimation(animRotate(90.0f, 0.5f, 0.45f));  
  92.                     buttonCamera.startAnimation(animTranslate(0.0f, 140.0f, 10, height - 98, buttonCamera, 180));  
  93.                     buttonWith.startAnimation(animTranslate(-50.0f, 130.0f, 10, height - 98, buttonWith, 160));  
  94.                     buttonPlace.startAnimation(animTranslate(-100.0f, 110.0f, 10, height - 98, buttonPlace, 140));  
  95.                     buttonMusic.startAnimation(animTranslate(-140.0f, 80.0f, 10, height - 98, buttonMusic, 120));  
  96.                     buttonThought.startAnimation(animTranslate(-160.0f, 40.0f, 10, height - 98, buttonThought, 80));  
  97.                     buttonSleep.startAnimation(animTranslate(-170.0f, 0.0f, 10, height - 98, buttonSleep, 50));  
  98.                       
  99.                 }  
  100.                       
  101.             }  
  102.         });  
  103.         buttonCamera.setOnClickListener(new OnClickListener()   
  104.         {  
  105.                   
  106.             @Override  
  107.             public void onClick(View v)   
  108.             {  
  109.                 // TODO Auto-generated method stub    
  110.                 buttonCamera.startAnimation(setAnimScale(2.5f, 2.5f));  
  111.                 buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));      
  112.                 buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));  
  113.                 buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));  
  114.                 buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));  
  115.                 buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));  
  116.                 buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));  
  117.             }  
  118.         });  
  119.         buttonWith.setOnClickListener(new OnClickListener()   
  120.         {  
  121.                   
  122.             @Override  
  123.             public void onClick(View v)   
  124.             {  
  125.                 // TODO Auto-generated method stub                    
  126.                 buttonWith.startAnimation(setAnimScale(2.5f, 2.5f));      
  127.                 buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));    
  128.                 buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));  
  129.                 buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));  
  130.                 buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));  
  131.                 buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));  
  132.                 buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));  
  133.             }  
  134.         });  
  135.         buttonPlace.setOnClickListener(new OnClickListener()   
  136.         {  
  137.                   
  138.             @Override  
  139.             public void onClick(View v)   
  140.             {  
  141.                 // TODO Auto-generated method stub                    
  142.                 buttonPlace.startAnimation(setAnimScale(2.5f, 2.5f));  
  143.                 buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));      
  144.                 buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));    
  145.                 buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));  
  146.                 buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));  
  147.                 buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));  
  148.                 buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));  
  149.             }  
  150.         });  
  151.         buttonMusic.setOnClickListener(new OnClickListener()   
  152.         {  
  153.                   
  154.             @Override  
  155.             public void onClick(View v)   
  156.             {  
  157.                 // TODO Auto-generated method stub                    
  158.                 buttonMusic.startAnimation(setAnimScale(2.5f, 2.5f));  
  159.                 buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));  
  160.                 buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));      
  161.                 buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));    
  162.                 buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));  
  163.                 buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));  
  164.                 buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));  
  165.             }  
  166.         });  
  167.         buttonThought.setOnClickListener(new OnClickListener()   
  168.         {  
  169.                   
  170.             @Override  
  171.             public void onClick(View v)   
  172.             {  
  173.                 // TODO Auto-generated method stub                    
  174.                 buttonThought.startAnimation(setAnimScale(2.5f, 2.5f));  
  175.                 buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));  
  176.                 buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));      
  177.                 buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));    
  178.                 buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));  
  179.                 buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));  
  180.                 buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));  
  181.             }  
  182.         });  
  183.         buttonSleep.setOnClickListener(new OnClickListener()   
  184.         {  
  185.                   
  186.             @Override  
  187.             public void onClick(View v)   
  188.             {  
  189.                 // TODO Auto-generated method stub                    
  190.                 buttonSleep.startAnimation(setAnimScale(2.5f, 2.5f));  
  191.                 buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));  
  192.                 buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));      
  193.                 buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));    
  194.                 buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));  
  195.                 buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));  
  196.                 buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));  
  197.             }  
  198.         });  
  199.           
  200.     }  
  201.       
  202.     protected Animation setAnimScale(float toX, float toY)   
  203.     {  
  204.         // TODO Auto-generated method stub  
  205.         animationScale = new ScaleAnimation(1f, toX, 1f, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.45f);  
  206.         animationScale.setInterpolator(PathButtonActivity.this, anim.accelerate_decelerate_interpolator);  
  207.         animationScale.setDuration(500);  
  208.         animationScale.setFillAfter(false);  
  209.         return animationScale;  
  210.           
  211.     }  
  212.       
  213.     protected Animation animRotate(float toDegrees, float pivotXValue, float pivotYValue)   
  214.     {  
  215.         // TODO Auto-generated method stub  
  216.         animationRotate = new RotateAnimation(0, toDegrees, Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF, pivotYValue);  
  217.         animationRotate.setAnimationListener(new AnimationListener()   
  218.         {  
  219.               
  220.             @Override  
  221.             public void onAnimationStart(Animation animation)   
  222.             {  
  223.                 // TODO Auto-generated method stub  
  224.                   
  225.             }  
  226.               
  227.             @Override  
  228.             public void onAnimationRepeat(Animation animation)   
  229.             {  
  230.                 // TODO Auto-generated method stub  
  231.                   
  232.             }  
  233.               
  234.             @Override  
  235.             public void onAnimationEnd(Animation animation)   
  236.             {  
  237.                 // TODO Auto-generated method stub  
  238.                 animationRotate.setFillAfter(true);  
  239.             }  
  240.         });  
  241.         return animationRotate;  
  242.     }  
  243.     //移动的动画效果          
  244.     /*   
  245.      * TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)   
  246.      *   
  247.      * float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;  
  248.      *  
  249.          * float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值;  
  250.      *  
  251.          * float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值;  
  252.      *  
  253.          * float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值;  
  254.      */  
  255.     protected Animation animTranslate(float toX, float toY, final int lastX, final int lastY,  
  256.             final Button button, long durationMillis)   
  257.     {  
  258.         // TODO Auto-generated method stub  
  259.         animationTranslate = new TranslateAnimation(0, toX, 0, toY);                  
  260.         animationTranslate.setAnimationListener(new AnimationListener()  
  261.         {  
  262.                           
  263.             @Override  
  264.             public void onAnimationStart(Animation animation)  
  265.             {  
  266.                 // TODO Auto-generated method stub  
  267.                                   
  268.             }  
  269.                           
  270.             @Override  
  271.             public void onAnimationRepeat(Animation animation)   
  272.             {  
  273.                 // TODO Auto-generated method stub  
  274.                               
  275.             }  
  276.                           
  277.             @Override  
  278.             public void onAnimationEnd(Animation animation)  
  279.             {  
  280.                 // TODO Auto-generated method stub  
  281.                 params = new LayoutParams(0, 0);  
  282.                 params.height = 50;  
  283.                 params.width = 50;                                            
  284.                 params.setMargins(lastX, lastY, 0, 0);  
  285.                 button.setLayoutParams(params);  
  286.                 button.clearAnimation();  
  287.                           
  288.             }  
  289.         });                                                                                               
  290.         animationTranslate.setDuration(durationMillis);  
  291.         return animationTranslate;  
  292.     }  
  293.       
  294.   
  295.           
  296. }  
复制代码

其实就是在button点击的时候播放Tween动画。

这样就基本完成,参照上面的代码 自己也可以自行修改成自己喜欢的样式。
看一下在我手机上运行的效果:
0_1331802747lnoe.gif.jpg 
0_1331802752pvp9.gif.png 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值