经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android Studio实现简单补间动画
来源:jb51  时间:2022/7/20 13:09:10  对本文有异议

本文实例为大家分享了Android Studio实现简单补间动画的具体代码,供大家参考,具体内容如下

1、动画发在res/anim/,创建new/Directory

2、创建动画,  平移,缩放,旋转,改变透明度

  1. //平移
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <set xmlns:android="http://schemas.android.com/apk/res/android">
  4. ?
  5. <translate
  6. ?
  7. ? ? android:fromXDelta="0.0"
  8. ? ? android:fromYDelta="0.0"
  9. ? ? android:toXDelta="100"
  10. ? ? android:toYDelta="0.0"
  11. ? ? android:repeatMode="reverse"
  12. ? ? android:repeatCount="infinite"
  13. ? ? android:duration="4000"
  14. ? ? />
  15. ?
  16. </set>
  17.  
  18. //缩放
  19. <?xml version="1.0" encoding="utf-8"?>
  20. <set xmlns:android="http://schemas.android.com/apk/res/android">
  21. <scale
  22. ?
  23. ? ? android:repeatMode="reverse"
  24. ? ? android:repeatCount="infinite"
  25. ? ? android:duration="3000"
  26. ? ? android:fromXScale="1.0"
  27. ? ? android:fromYScale="1.0"
  28. ? ? android:toXScale="0.5"
  29. ? ? android:toYScale="0.5"
  30. ? ? android:pivotX="50%"
  31. ? ? android:pivotY="50%"/>
  32. ?
  33. </set>
  34.  
  35. //旋转
  36. <?xml version="1.0" encoding="utf-8"?>
  37. <set xmlns:android="http://schemas.android.com/apk/res/android">
  38. ?
  39. ? ? <rotate android:fromDegrees="0"
  40. ? ? ? ? android:toDegrees="360"
  41. ? ? ? ? android:pivotX="50%"
  42. ? ? ? ? android:pivotY="50%"
  43. ? ? ? ? android:repeatMode="reverse"
  44. ? ? ? ? android:repeatCount="infinite"
  45. ? ? ? ? android:duration="1000"
  46. ? ? ? ? />
  47. ?
  48. </set>
  49.  
  50. //改变透明度
  51. <?xml version="1.0" encoding="utf-8"?>
  52. <set xmlns:android="http://schemas.android.com/apk/res/android">
  53. <alpha
  54. ? ? android:interpolator="@android:anim/linear_interpolator"
  55. ? ? android:repeatMode="reverse"
  56. ? ? android:repeatCount="infinite"
  57. ? ? android:duration="1000"
  58. ? ? android:fromAlpha="1.0"
  59. ? ? android:toAlpha="0.0"/>
  60. ?
  61. </set>

 3,布局activity_main.xml,点击按钮图片发送变化,图片需要自己下载添加44

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? xmlns:app="http://schemas.android.com/apk/res-auto"
  4. ? ? xmlns:tools="http://schemas.android.com/tools"
  5. ? ? android:layout_width="match_parent"
  6. ? ? android:layout_height="match_parent"
  7. ? ? android:layout_weight="1"
  8. ? ? android:orientation="vertical"
  9. ? ? tools:context=".MainActivity">
  10. ?
  11. ? ? ? <ImageView
  12. ? ? ? ? ? android:layout_width="300dp"
  13. ? ? ? ? ? android:layout_height="300dp"
  14. ? ? ? ? ? android:layout_centerInParent="true"
  15. ? ? ? ? ? android:layout_gravity="center"
  16. ? ? ? ? ? android:id="@+id/fk"
  17. ? ? ? ? ? android:src="@drawable/ff"/>
  18. ?
  19. ? ?<LinearLayout
  20. ? ? ? ?android:layout_width="match_parent"
  21. ? ? ? ?android:layout_height="match_parent"
  22. ? ? ? android:orientation="horizontal">
  23. ?
  24. ? ? ? <Button
  25. ? ? ? ?android:layout_width="wrap_content"
  26. ? ? ? ?android:layout_height="wrap_content"
  27. ? ? ? ?android:text="平移"
  28. ? ? ? ?android:textSize="24sp"
  29. ? ? ? ?android:layout_weight="1"
  30. ? ? ? ? android:gravity="center"
  31. ? ? ? ? ? android:layout_gravity="bottom"
  32. ? ? ? ?android:id="@+id/but"/>
  33. ? ? ? <Button
  34. ? ? ? ? ? android:layout_width="wrap_content"
  35. ? ? ? ? ? android:layout_height="wrap_content"
  36. ? ? ? ? ? android:text="缩放"
  37. ? ? ? ? ? android:textSize="24sp"
  38. ? ? ? ? ? android:layout_gravity="bottom"
  39. ? ? ? ? ? android:layout_weight="1"
  40. ? ? ? ? ? android:gravity="center"
  41. ? ? ? ? ? android:id="@+id/but1"/>
  42. ? ? ? <Button
  43. ? ? ? ? ? android:layout_width="wrap_content"
  44. ? ? ? ? ? android:layout_height="wrap_content"
  45. ? ? ? ? ? android:text="旋转"
  46. ? ? ? ? ? android:textSize="24sp"
  47. ? ? ? ? ? android:gravity="center"
  48. ? ? ? ? ? android:layout_gravity="bottom"
  49. ? ? ? ? ? android:layout_weight="1"
  50. ? ? ? ? ? android:id="@+id/but2"/>
  51. ? ? ? <Button
  52. ? ? ? ? ? android:layout_width="wrap_content"
  53. ? ? ? ? ? android:layout_height="wrap_content"
  54. ? ? ? ? ? android:text="改变透明度"
  55. ? ? ? ? ? android:textSize="24sp"
  56. ? ? ? ? ? android:gravity="center"
  57. ? ? ? ? ? android:layout_gravity="bottom"
  58. ? ? ? ? ? android:layout_weight="1"
  59. ? ? ? ? ? android:id="@+id/but3"/>
  60. ? ?</LinearLayout>
  61. ?
  62. </RelativeLayout>

4、逻辑代码MainActivity

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. ? ? private ImageView imageView;
  3. ? ? private Button button;
  4. ? ? private Button button1;
  5. ? ? private Button button2;
  6. ? ? private Button button3;
  7. ?
  8. ? ? @RequiresApi(api = Build.VERSION_CODES.N)
  9. ? ? @Override
  10. ? ? protected void onCreate(Bundle savedInstanceState) {
  11. ? ? ? ? super.onCreate(savedInstanceState);
  12. ? ? ? ? setContentView(R.layout.activity_main);
  13. ? ? ? ? //要用控件,定id
  14. ? ? ? ? button=findViewById(R.id.but);
  15. ? ? ? ? button1=findViewById(R.id.but1);
  16. ? ? ? ? button2=findViewById(R.id.but2);
  17. ? ? ? ? button3=findViewById(R.id.but3);
  18. ?
  19. ? ? ? ? imageView=findViewById(R.id.fk);
  20. ? ? ? ? button.setOnClickListener(this);
  21. ? ? ? ? button1.setOnClickListener(this);
  22. ? ? ? ? button2.setOnClickListener(this);
  23. ? ? ? ? button3.setOnClickListener(this);
  24. ?
  25. ? ? }
  26. ?
  27. ? ? @Override
  28. ? ? public void onClick(View view) {
  29. ? ? ? ? switch (view.getId()){
  30. ? ? ? ? ? ? case R.id.but:
  31. ? ? ? ? ? ? ? ? Animation translation= AnimationUtils.loadAnimation(this,R.anim.translate);
  32. ? ? ? ? ? ? ? ? imageView.startAnimation(translation);
  33. ? ? ? ? ? ? ? ? break;
  34. ? ? ? ? ? ? case R.id.but1:
  35. ? ? ? ? ? ? ? ? Animation scale= AnimationUtils.loadAnimation(this,R.anim.scale);
  36. ? ? ? ? ? ? ? ? imageView.startAnimation(scale);
  37. ? ? ? ? ? ? ? ? break;
  38. ? ? ? ? ? ? case R.id.but2:
  39. ? ? ? ? ? ? ? ? Animation rotate= AnimationUtils.loadAnimation(this,R.anim.rotate);
  40. ? ? ? ? ? ? ? ? imageView.startAnimation(rotate);
  41. ? ? ? ? ? ? ? ? break;
  42. ? ? ? ? ? ? case R.id.but3:
  43. ? ? ? ? ? ? ? ? Animation alpha= AnimationUtils.loadAnimation(this,R.anim.alpha);
  44. ? ? ? ? ? ? ? ? imageView.startAnimation(alpha);
  45. ? ? ? ? ? ? ? ? break;
  46. ? ? ? ? }
  47. ?
  48. ? ? }
  49. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号