超炫button按钮动画效果

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值