超炫button按钮动画效果

本文展示了一个Android应用中实现按钮动画效果的方法,通过使用自定义的动画类,实现按钮在点击时的平移、旋转和缩放等动态变化。包括按钮布局设计、关键代码解析以及实际运行效果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. <ImageView android:layout_width="wrap_content"
  6. android:layout_height="fill_parent" android:background="@drawable/splash_shadow"
  7. android:layout_marginLeft="50dip" />

  8. <RelativeLayout android:id="@+id/relativeLayout_top_bar"
  9. android:layout_width="fill_parent" android:layout_height="40dip"
  10. android:background="@drawable/qa_bar">

  11. <ImageView android:layout_width="60dip"
  12. android:layout_height="20dip" android:background="@drawable/qa_logo"
  13. android:layout_centerInParent="true" />

  14. </RelativeLayout>

  15. <TextView android:layout_below="@id/relativeLayout_top_bar"
  16. android:layout_width="wrap_content" android:layout_height="wrap_content"
  17. android:textSize="20sp" android:text="Tap to Refresh"
  18. android:layout_centerHorizontal="true" android:layout_marginTop="50dip" android:textStyle="bold"/>


  19. <Button android:id="@+id/button_composer_sleep"
  20. android:layout_width="wrap_content" android:layout_height="wrap_content"
  21. android:background="@drawable/composer_sleep"
  22. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  23. android:layout_alignParentBottom="true">
  24. </Button>

  25. <Button android:id="@+id/button_composer_thought"
  26. android:layout_width="wrap_content" android:layout_height="wrap_content"
  27. android:background="@drawable/composer_thought"
  28. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  29. android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_sleep">
  30. </Button>

  31. <Button android:id="@+id/button_composer_music"
  32. android:layout_width="wrap_content" android:layout_height="wrap_content"
  33. android:background="@drawable/composer_music"
  34. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  35. android:layout_alignParentBottom="true">
  36. </Button>

  37. <Button android:id="@+id/button_composer_place"
  38. android:layout_width="wrap_content" android:layout_height="wrap_content"
  39. android:background="@drawable/composer_place"
  40. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  41. android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_music">
  42. </Button>


  43. <Button android:id="@+id/button_composer_with"
  44. android:layout_width="wrap_content" android:layout_height="wrap_content"
  45. android:background="@drawable/composer_with"
  46. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  47. android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_place">
  48. </Button>


  49. <Button android:id="@+id/button_composer_camera"
  50. android:layout_width="wrap_content" android:layout_height="wrap_content"
  51. android:background="@drawable/composer_camera"
  52. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  53. android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_with">
  54. </Button>


  55. <Button android:id="@+id/button_friends_delete"
  56. android:layout_width="wrap_content" android:layout_height="wrap_content"
  57. android:background="@drawable/friends_delete"
  58. android:layout_marginBottom="10dip" android:layout_marginLeft="10dip"
  59. android:layout_alignParentBottom="true" android:layout_above="@id/button_composer_camera">
  60. </Button>


  61. </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. public class PathButtonActivity extends Activity
  16. {
  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. /** Called when the activity is first created. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.path_button);

  28. initialButton();


  29. }
  30. private void initialButton()
  31. {
  32. // TODO Auto-generated method stub
  33. Display display = getWindowManager().getDefaultDisplay();
  34. height = display.getHeight();
  35. width = display.getWidth();
  36. Log.v("width& height is:", String.valueOf(width) + ", " + String.valueOf(height));

  37. params.height = 50;
  38. params.width = 50;
  39. //设置边距(int left, int top, int right, int bottom)
  40. params.setMargins(10, height - 98, 0, 0);

  41. buttonSleep = (Button) findViewById(R.id.button_composer_sleep);
  42. buttonSleep.setLayoutParams(params);

  43. buttonThought = (Button) findViewById(R.id.button_composer_thought);
  44. buttonThought.setLayoutParams(params);

  45. buttonMusic = (Button) findViewById(R.id.button_composer_music);
  46. buttonMusic.setLayoutParams(params);

  47. buttonPlace = (Button) findViewById(R.id.button_composer_place);
  48. buttonPlace.setLayoutParams(params);

  49. buttonWith = (Button) findViewById(R.id.button_composer_with);
  50. buttonWith.setLayoutParams(params);

  51. buttonCamera = (Button) findViewById(R.id.button_composer_camera);
  52. buttonCamera.setLayoutParams(params);

  53. buttonDelete = (Button) findViewById(R.id.button_friends_delete);
  54. buttonDelete.setLayoutParams(params);

  55. buttonDelete.setOnClickListener(new OnClickListener()
  56. {

  57. @Override
  58. public void onClick(View v)
  59. {
  60. // TODO Auto-generated method stub
  61. if(isClick == false)
  62. {
  63. isClick = true;
  64. buttonDelete.startAnimation(animRotate(-45.0f, 0.5f, 0.45f));
  65. buttonCamera.startAnimation(animTranslate(0.0f, -180.0f, 10, height - 240, buttonCamera, 80));
  66. buttonWith.startAnimation(animTranslate(30.0f, -150.0f, 60, height - 230, buttonWith, 100));
  67. buttonPlace.startAnimation(animTranslate(70.0f, -120.0f, 110, height - 210, buttonPlace, 120));
  68. buttonMusic.startAnimation(animTranslate(80.0f, -110.0f, 150, height - 180, buttonMusic, 140));
  69. buttonThought.startAnimation(animTranslate(90.0f, -60.0f, 175, height - 135, buttonThought, 160));
  70. buttonSleep.startAnimation(animTranslate(170.0f, -30.0f, 190, height - 90, buttonSleep, 180));

  71. }
  72. else
  73. {
  74. isClick = false;
  75. buttonDelete.startAnimation(animRotate(90.0f, 0.5f, 0.45f));
  76. buttonCamera.startAnimation(animTranslate(0.0f, 140.0f, 10, height - 98, buttonCamera, 180));
  77. buttonWith.startAnimation(animTranslate(-50.0f, 130.0f, 10, height - 98, buttonWith, 160));
  78. buttonPlace.startAnimation(animTranslate(-100.0f, 110.0f, 10, height - 98, buttonPlace, 140));
  79. buttonMusic.startAnimation(animTranslate(-140.0f, 80.0f, 10, height - 98, buttonMusic, 120));
  80. buttonThought.startAnimation(animTranslate(-160.0f, 40.0f, 10, height - 98, buttonThought, 80));
  81. buttonSleep.startAnimation(animTranslate(-170.0f, 0.0f, 10, height - 98, buttonSleep, 50));

  82. }

  83. }
  84. });
  85. buttonCamera.setOnClickListener(new OnClickListener()
  86. {

  87. @Override
  88. public void onClick(View v)
  89. {
  90. // TODO Auto-generated method stub
  91. buttonCamera.startAnimation(setAnimScale(2.5f, 2.5f));
  92. buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));
  93. buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));
  94. buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));
  95. buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));
  96. buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));
  97. buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));
  98. }
  99. });
  100. buttonWith.setOnClickListener(new OnClickListener()
  101. {

  102. @Override
  103. public void onClick(View v)
  104. {
  105. // TODO Auto-generated method stub
  106. buttonWith.startAnimation(setAnimScale(2.5f, 2.5f));
  107. buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));
  108. buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));
  109. buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));
  110. buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));
  111. buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));
  112. buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));
  113. }
  114. });
  115. buttonPlace.setOnClickListener(new OnClickListener()
  116. {

  117. @Override
  118. public void onClick(View v)
  119. {
  120. // TODO Auto-generated method stub
  121. buttonPlace.startAnimation(setAnimScale(2.5f, 2.5f));
  122. buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));
  123. buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));
  124. buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));
  125. buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));
  126. buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));
  127. buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));
  128. }
  129. });
  130. buttonMusic.setOnClickListener(new OnClickListener()
  131. {

  132. @Override
  133. public void onClick(View v)
  134. {
  135. // TODO Auto-generated method stub
  136. buttonMusic.startAnimation(setAnimScale(2.5f, 2.5f));
  137. buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));
  138. buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));
  139. buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));
  140. buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));
  141. buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));
  142. buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));
  143. }
  144. });
  145. buttonThought.setOnClickListener(new OnClickListener()
  146. {

  147. @Override
  148. public void onClick(View v)
  149. {
  150. // TODO Auto-generated method stub
  151. buttonThought.startAnimation(setAnimScale(2.5f, 2.5f));
  152. buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));
  153. buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));
  154. buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));
  155. buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));
  156. buttonSleep.startAnimation(setAnimScale(0.0f, 0.0f));
  157. buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));
  158. }
  159. });
  160. buttonSleep.setOnClickListener(new OnClickListener()
  161. {

  162. @Override
  163. public void onClick(View v)
  164. {
  165. // TODO Auto-generated method stub
  166. buttonSleep.startAnimation(setAnimScale(2.5f, 2.5f));
  167. buttonPlace.startAnimation(setAnimScale(0.0f, 0.0f));
  168. buttonWith.startAnimation(setAnimScale(0.0f, 0.0f));
  169. buttonCamera.startAnimation(setAnimScale(0.0f, 0.0f));
  170. buttonMusic.startAnimation(setAnimScale(0.0f, 0.0f));
  171. buttonThought.startAnimation(setAnimScale(0.0f, 0.0f));
  172. buttonDelete.startAnimation(setAnimScale(0.0f, 0.0f));
  173. }
  174. });

  175. }

  176. protected Animation setAnimScale(float toX, float toY)
  177. {
  178. // TODO Auto-generated method stub
  179. animationScale = new ScaleAnimation(1f, toX, 1f, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.45f);
  180. animationScale.setInterpolator(PathButtonActivity.this, anim.accelerate_decelerate_interpolator);
  181. animationScale.setDuration(500);
  182. animationScale.setFillAfter(false);
  183. return animationScale;

  184. }

  185. protected Animation animRotate(float toDegrees, float pivotXValue, float pivotYValue)
  186. {
  187. // TODO Auto-generated method stub
  188. animationRotate = new RotateAnimation(0, toDegrees, Animation.RELATIVE_TO_SELF, pivotXValue, Animation.RELATIVE_TO_SELF, pivotYValue);
  189. animationRotate.setAnimationListener(new AnimationListener()
  190. {

  191. @Override
  192. public void onAnimationStart(Animation animation)
  193. {
  194. // TODO Auto-generated method stub

  195. }

  196. @Override
  197. public void onAnimationRepeat(Animation animation)
  198. {
  199. // TODO Auto-generated method stub

  200. }

  201. @Override
  202. public void onAnimationEnd(Animation animation)
  203. {
  204. // TODO Auto-generated method stub
  205. animationRotate.setFillAfter(true);
  206. }
  207. });
  208. return animationRotate;
  209. }
  210. //移动的动画效果
  211. /*
  212. * TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
  213. *
  214. * float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;
  215. *
  216.    * float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值;
  217. *
  218.    * float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值;
  219. *
  220.    * float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值;
  221. */
  222. protected Animation animTranslate(float toX, float toY, final int lastX, final int lastY,
  223. final Button button, long durationMillis)
  224. {
  225. // TODO Auto-generated method stub
  226. animationTranslate = new TranslateAnimation(0, toX, 0, toY);
  227. animationTranslate.setAnimationListener(new AnimationListener()
  228. {

  229. @Override
  230. public void onAnimationStart(Animation animation)
  231. {
  232. // TODO Auto-generated method stub

  233. }

  234. @Override
  235. public void onAnimationRepeat(Animation animation)
  236. {
  237. // TODO Auto-generated method stub

  238. }

  239. @Override
  240. public void onAnimationEnd(Animation animation)
  241. {
  242. // TODO Auto-generated method stub
  243. params = new LayoutParams(0, 0);
  244. params.height = 50;
  245. params.width = 50;
  246. params.setMargins(lastX, lastY, 0, 0);
  247. button.setLayoutParams(params);
  248. button.clearAnimation();

  249. }
  250. });
  251. animationTranslate.setDuration(durationMillis);
  252. return animationTranslate;
  253. }



  254. }
复制代码

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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值