Android Animation动画

举报
yechaoa 发表于 2022/05/30 23:52:43 2022/05/30
【摘要】 效果图 都在注释里了 补间动画 package com.yechaoa.viewanimationdemo; import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;i...


效果图



都在注释里了


补间动画


  
  1. package com.yechaoa.viewanimationdemo;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.animation.AlphaAnimation;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationSet;
  9. import android.view.animation.AnimationUtils;
  10. import android.view.animation.RotateAnimation;
  11. import android.view.animation.ScaleAnimation;
  12. import android.view.animation.TranslateAnimation;
  13. import android.widget.Button;
  14. import android.widget.ImageView;
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  16. private ImageView mImageView;
  17. private Button mBtnGone, mBtnVisible, mBtnMoveToLeft, mBtnMoveToRight,
  18. mBtnRotate, mBtnScale, mBtnAlpha, mBtnAnimGroup;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. initView();
  24. initListener();
  25. }
  26. private void initView() {
  27. mImageView = findViewById(R.id.imageView);
  28. mBtnGone = findViewById(R.id.btn_gone);
  29. mBtnVisible = findViewById(R.id.btn_visible);
  30. mBtnMoveToLeft = findViewById(R.id.btn_move_to_left);
  31. mBtnMoveToRight = findViewById(R.id.btn_move_to_right);
  32. mBtnRotate = findViewById(R.id.btn_rotate);
  33. mBtnScale = findViewById(R.id.btn_Scale);
  34. mBtnAlpha = findViewById(R.id.btn_alpha);
  35. mBtnAnimGroup = findViewById(R.id.btn_anim_group);
  36. }
  37. private void initListener() {
  38. mBtnGone.setOnClickListener(this);
  39. mBtnVisible.setOnClickListener(this);
  40. mBtnMoveToLeft.setOnClickListener(this);
  41. mBtnMoveToRight.setOnClickListener(this);
  42. mBtnRotate.setOnClickListener(this);
  43. mBtnScale.setOnClickListener(this);
  44. mBtnAlpha.setOnClickListener(this);
  45. mBtnAnimGroup.setOnClickListener(this);
  46. }
  47. @Override
  48. public void onClick(View view) {
  49. switch (view.getId()) {
  50. case R.id.btn_gone:
  51. //位移动画
  52. //4个数值分别表示坐标轴中以远点为中心的正负X轴Y轴 f表示float
  53. TranslateAnimation mHiddenAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
  54. 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
  55. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
  56. -1.0f);
  57. //设置时长
  58. mHiddenAnimation.setDuration(500);
  59. mImageView.startAnimation(mHiddenAnimation);
  60. //0.0f 加一个渐变的效果
  61. mImageView.animate().alpha(0);
  62. mImageView.setVisibility(View.GONE);
  63. break;
  64. case R.id.btn_visible:
  65. TranslateAnimation mShowAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
  66. Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
  67. -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  68. mShowAnimation.setDuration(500);
  69. mImageView.startAnimation(mShowAnimation);
  70. mImageView.animate().alpha(1);
  71. mImageView.setVisibility(View.VISIBLE);
  72. break;
  73. case R.id.btn_move_to_left:
  74. TranslateAnimation mLeftAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
  75. Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,
  76. 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  77. mLeftAnimation.setDuration(500);
  78. mImageView.startAnimation(mLeftAnimation);
  79. mImageView.animate().alpha(0);
  80. mImageView.setVisibility(View.VISIBLE);
  81. break;
  82. case R.id.btn_move_to_right:
  83. TranslateAnimation mRightAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
  84. Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
  85. 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  86. mRightAnimation.setDuration(500);
  87. mImageView.startAnimation(mRightAnimation);
  88. mImageView.animate().alpha(0);
  89. mImageView.setVisibility(View.VISIBLE);
  90. break;
  91. case R.id.btn_rotate:
  92. //旋转动画
  93. RotateAnimation mRotateAnimation = new RotateAnimation(0, 60, 0.5f, 0.5f);
  94. mRotateAnimation.setDuration(2000);
  95. mImageView.startAnimation(mRotateAnimation);
  96. break;
  97. case R.id.btn_Scale:
  98. /**
  99. * 一共8个参数
  100. * 前4个参数是分别是X轴、Y轴的起始位置和结束位置
  101. * 后4个参数相对位置值,Animation.RELATIVE_TO_SELF表示自己,也就是以自己为中心点向四周缩放
  102. */
  103. //缩放动画
  104. ScaleAnimation mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
  105. Animation.RELATIVE_TO_SELF, 0.5f,
  106. Animation.RELATIVE_TO_SELF, 0.5f);
  107. mScaleAnimation.setDuration(2000);
  108. //设置重复次数
  109. mScaleAnimation.setRepeatCount(2);
  110. //动画执行完后是否停留在执行完的状态
  111. mScaleAnimation.setFillAfter(true);
  112. //执行前的等待时间
  113. mScaleAnimation.setStartOffset(500);
  114. //取消动画
  115. //mScaleAnimation.cancel();
  116. mScaleAnimation.setAnimationListener(new Animation.AnimationListener() {
  117. @Override
  118. public void onAnimationStart(Animation animation) {
  119. Log.d("onAnimationStart", "动画开始");
  120. }
  121. @Override
  122. public void onAnimationEnd(Animation animation) {
  123. Log.d("onAnimationEnd", "动画结束");
  124. }
  125. @Override
  126. public void onAnimationRepeat(Animation animation) {
  127. Log.d("onAnimationRepeat", "动画重复");
  128. }
  129. });
  130. mImageView.startAnimation(mScaleAnimation);
  131. break;
  132. case R.id.btn_alpha:
  133. //渐变动画
  134. AlphaAnimation mAlphaAnimation = new AlphaAnimation(1.0f, 0.0f);
  135. mAlphaAnimation.setDuration(2000);
  136. mAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {
  137. @Override
  138. public void onAnimationStart(Animation animation) {
  139. }
  140. @Override
  141. public void onAnimationEnd(Animation animation) {
  142. //淡出之后再淡入
  143. //xml的方式引用,参数与代码设置一样
  144. Animation aa = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
  145. mImageView.startAnimation(aa);
  146. }
  147. @Override
  148. public void onAnimationRepeat(Animation animation) {
  149. }
  150. });
  151. mImageView.startAnimation(mAlphaAnimation);
  152. break;
  153. case R.id.btn_anim_group:
  154. /**
  155. * 两种方式可以实现组合动画
  156. * 1.给第一个动画设置监听,结束之后执行第二个动画
  157. * 2.用AnimationSet,动画集合类
  158. */
  159. RotateAnimation ra = new RotateAnimation(0, 360,
  160. Animation.RELATIVE_TO_SELF, 0.5f,
  161. Animation.RELATIVE_TO_SELF, 0.5f);
  162. ra.setDuration(2000);
  163. AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
  164. aa.setDuration(2000);
  165. ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
  166. Animation.RELATIVE_TO_SELF, 0.5f,
  167. Animation.RELATIVE_TO_SELF, 0.5f);
  168. sa.setDuration(2000);
  169. AnimationSet animationSet = new AnimationSet(true);
  170. animationSet.addAnimation(ra);
  171. animationSet.addAnimation(aa);
  172. animationSet.addAnimation(sa);
  173. //animationSet.setInterpolator(new DecelerateInterpolator(5));
  174. mImageView.startAnimation(animationSet);
  175. animationSet.start();
  176. /**
  177. * Interpolator定义动画的速率
  178. * AccelerateDecelerateInterpolator延迟减速,在动作执行到中间的时候才执行该特效。
  179. * AccelerateInterpolator会使慢慢以(float)的参数降低速度
  180. * CycleInterpolator曲线运动特效,要传递float型的参数。
  181. * DecelerateInterpolator开头和结尾慢,中间快
  182. * LinearInterpolator匀速
  183. */
  184. break;
  185. }
  186. }
  187. }


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:duration="2000"
  5. android:fromAlpha="0.0"
  6. android:toAlpha="1.0" />
  7. </set>


帧动画


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="true">
  4. <item
  5. android:drawable="@mipmap/ic_launcher"
  6. android:duration="200" />
  7. <item
  8. android:drawable="@mipmap/ic_launcher"
  9. android:duration="200" />
  10. <item
  11. android:drawable="@mipmap/ic_launcher"
  12. android:duration="200" />
  13. <item
  14. android:drawable="@mipmap/ic_launcher"
  15. android:duration="200" />
  16. <item
  17. android:drawable="@mipmap/ic_launcher"
  18. android:duration="200" />
  19. <item
  20. android:drawable="@mipmap/ic_launcher"
  21. android:duration="200" />
  22. </animation-list>

这个文件必须放在drawable文件下


github : https://github.com/yechaoa/ViewAnimationDemo




文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yechaoa/article/details/77606475

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。