Android 动画

一、Tween动画

         1、特点:
                   1.只是实现了简单的渐变,平移,拉伸,缩放;
                   2.Tween动画实现完成以后,该动画会恢复原状,自身属性并没有发生任何改变,动画的形成是通过父布局改变了其位置或渐变度,从而来改变其本身在短时间内的属性
                   3.此动画的形成是依赖于其父布局的,其属性没有发生任何改变
         2、在Tween动画中所有动画的父类是Animation。
         3、动画的渐变
   
   
  1. /**
  2. * 渐变
  3. */
  4. private void alpin(){
  5. /**
  6. * 第一个参数:表示开始的透明度
  7. * 第二个参数:表示结束的透明度
  8. * 透明度的范围是0-1;1表示完全不透明;0表示完全透明
  9. */
  10. Animation animation=new AlphaAnimation(0,1);
  11. animation.setDuration(3000);
  12. //设置动画重复的次数
  13. animation.setRepeatCount(5);
  14. /**
  15. * Animation.REVERSE:重复的时候进行反转
  16. * Animation.RESTART:每次执行都重新开始
  17. */
  18. animation.setRepeatMode(Animation.RESTART);
  19. animation.setAnimationListener(new Animation.AnimationListener() {
  20. /**
  21. * 动画开始执行的时候的一个回调
  22. * @param animation
  23. */
  24. @Override
  25. public void onAnimationStart(Animation animation) {
  26. }
  27. /**
  28. * 动画结束的时候的一个回调
  29. * @param animation
  30. */
  31. @Override
  32. public void onAnimationEnd(Animation animation) {
  33. }
  34. /**
  35. * 动画重复执行的时候的一个回调
  36. * @param animation
  37. */
  38. @Override
  39. public void onAnimationRepeat(Animation animation) {
  40. }
  41. });
  42. mImageView.startAnimation(animation);
  43. }
         4、动画的缩放
   
   
  1. /**
  2. * 缩放
  3. */
  4. private void scale(){
  5. //按照宽和高的倍数来进行缩放
  6. //默认的缩放点是左上角
  7. // Animation animation=new ScaleAnimation(0.5f,1,0.5f,1);
  8. //后面的两个参数是像素,确定缩放点的坐标
  9. // Animation animation=new ScaleAnimation(0,1,0,1,mImageView.getWidth()/2,mImageView.getHeight()/2);
  10. //后面的四个参数是用来确定缩放点的坐标
  11. Animation animation=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  12. animation.setDuration(3000);
  13. animation.setRepeatCount(5);
  14. animation.setRepeatMode(Animation.REVERSE);
  15. mImageView.startAnimation(animation);
  16. }
         5、动画的平移
   
   
  1. /**
  2. *平移
  3. */
  4. private void traslate(){
  5. /**
  6. * 坐标0,0是以自身左上角定点的坐标为坐标点;移动的单位是像素。
  7. */
  8. // Animation animation=new TranslateAnimation(0,100,0,100);
  9. /**
  10. * 相对点的确定:
  11. * Animation.RELATIVE_TO_SELF:相对于自己
  12. * Animation.ABSOLUTE:相对于绝对点(很少用)
  13. * Animation.RELATIVE_TO_PARENT:相对于父容器
  14. *
  15. * 值的确定:
  16. * 是以当前宽和该乘以倍数后所确定的坐标
  17. */
  18. Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f);
  19. animation.setDuration(2000);
  20. animation.setRepeatMode(Animation.REVERSE);
  21. animation.setRepeatCount(5);
  22. mImageView.startAnimation(animation);
  23. }
         6、动画的旋转
   
   
  1. /**
  2. * 旋转
  3. */
  4. private void rotation(){
  5. //默认的旋转点就是以左上角为旋转点
  6. // Animation animation=new RotateAnimation(0,180);
  7. //后面的两个参数表示的像素,确定的是旋转点的位置
  8. // Animation animation=new RotateAnimation(0,180,mImageView.getWidth()/2,mImageView.getWidth()/2);
  9. //后面的四个参数,都是为了确定的旋转点的位置,是按照宽和高的倍数来确定位置
  10. Animation animation=new RotateAnimation(0,180,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  11. animation.setDuration(3000);
  12. animation.setRepeatCount(5);
  13. animation.setRepeatMode(Animation.REVERSE);
  14. mImageView.startAnimation(animation);
  15. }
         7、多动画同时执行
                   步骤:
                   1.获取一个AnimationSet对象;
                   2.定义一系列动画(一个或多个);
                   3.将定义的动画添加到动画集中;
                   4.设置动画的参数(时间,循环次数,循环方式);
                   5.使用View对象的startAnimation方法启动动画集;
   
   
  1. /**
  2. * 多动画同时执行
  3. */
  4. private void manyAnima(){
  5. Animation animation=new AlphaAnimation(0,1);
  6. Animation animation1=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  7. Animation animation2=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  8. AnimationSet animationSet=new AnimationSet(true);
  9. animationSet.addAnimation(animation);
  10. animationSet.addAnimation(animation1);
  11. animationSet.addAnimation(animation2);
  12. animationSet.setDuration(5000);
  13. mImageView.startAnimation(animationSet);
  14. }
         8、通过配置文件实现动画的步骤:
                   1.在res的目录下建立一个anim的文件夹;
                   2.在xml文件中配置相应的动画属性;
                   3.通过AnimationUtils的LoadAnimation()方法加载动画;
                   4.设置动画的相应属性;
                   5.运行动画。
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:duration="5000"
  5. android:fromAlpha="0"
  6. android:toAlpha="1" />
  7. </set>
    
    
  1. private void alpin1(){
  2. Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.animo_01);
  3. mImageView.startAnimation(animation);
  4. }
二、Frame动画
         使用的步骤:
         1.在res目录下右键-->new --> Android resource file -->填写名字,选择Drawable,将根节点给成animation-list -->OK;
         2.在创建的xml文件中配置一系列item;
   
   
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/girl_1" android:duration="150"/>
  4. <item android:drawable="@drawable/girl_2" android:duration="150"/>
  5. <item android:drawable="@drawable/girl_3" android:duration="150"/>
  6. <item android:drawable="@drawable/girl_4" android:duration="150"/>
  7. <item android:drawable="@drawable/girl_5" android:duration="150"/>
  8. <item android:drawable="@drawable/girl_6" android:duration="150"/>
  9. <item android:drawable="@drawable/girl_7" android:duration="150"/>
  10. <item android:drawable="@drawable/girl_8" android:duration="150"/>
  11. <item android:drawable="@drawable/girl_9" android:duration="150"/>
  12. <item android:drawable="@drawable/girl_10" android:duration="150"/>
  13. <item android:drawable="@drawable/girl_11" android:duration="150"/>
  14. </animation-list>
         3.给需要帧动画的控件设置setBackgroundResource(R.drawable.frame_01);
         4.通过当前对象.getBackground()获取其背景,强制类型转换成AnimationDrawable对象;
         5.通过animationDrawable.start()直接运行帧动画;
   
   
  1. private void frameAnima(){
  2. //设置内容,内容是帧动画的布局文件
  3. mImageView1.setBackgroundResource(R.drawable.frame_01);
  4. //获取当前控件的背景
  5. AnimationDrawable animationDrawable = (AnimationDrawable) mImageView1.getBackground();
  6. //打开帧动画
  7. animationDrawable.start();
  8. }
三、属性动画
         1、通过给定的时间间隔来完成属性的动态改变的动画;
         2、属性动画改变的是属性,只不过默认的情况下是匀速的改变当前对象的属性;
         3、属性动画的原理:
                   每次开始执行的时候都会首先通过调用当前对象的get属性名的方法获取当前的值,然后调用set方法来不断的进行设置对象的值,给定一个时间通过计算出每个单位时间所移动的距离,然后开始移动。
         4、属性动画的应用:
1.缩放
   
   
  1. /**
  2. * 属性动画的缩放
  3. */
  4. private void scale(){
  5. ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 0.5f);
  6. ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 0.5f);
  7. scaleX.setDuration(3000);
  8. scaleY.setDuration(3000);
  9. scaleX.start();
  10. scaleY.start();
  11. }
2.渐变
   
   
  1. /**
  2. * 属性动画的渐变
  3. */
  4. private void alphi(){
  5. ObjectAnimator alpha = ObjectAnimator.ofFloat(mImageView, "alpha", 1, 0.8f, 0.2f);
  6. alpha.setDuration(3000);
  7. alpha.setRepeatCount(5);
  8. alpha.setRepeatMode(Animation.REVERSE);
  9. alpha.start();
  10. }
3.旋转
   
   
  1. /**
  2. * 属性动画的旋转
  3. */
  4. private void rotation(){
  5. ObjectAnimator rotation = ObjectAnimator.ofFloat(mImageView, "rotation", 0, 360);
  6. rotation.setDuration(3000);
  7. rotation.setRepeatCount(3);
  8. rotation.setRepeatMode(Animation.REVERSE);
  9. rotation.start();
  10. }
4.平移
   
   
  1. /**
  2. * 属性动画的平移
  3. */
  4. private void translate(){
  5. ObjectAnimator translationX = ObjectAnimator.ofFloat(mImageView, "translationX", 0, 500);
  6. ObjectAnimator translationY = ObjectAnimator.ofFloat(mImageView, "translationY", 0, 500);
  7. translationX.setDuration(3000);
  8. translationY.setDuration(3000);
  9. translationX.setRepeatCount(5);
  10. translationY.setRepeatCount(5);
  11. translationX.setRepeatMode(Animation.REVERSE);
  12. translationY.setRepeatMode(Animation.REVERSE);
  13. translationX.start();
  14. translationY.start();
  15. }
5.多个动画同时执行
   
   
  1. /**
  2. * 多动画同时执行
  3. */
  4. private void many(){
  5. AnimatorSet animatorSet=new AnimatorSet();
  6. ObjectAnimator translationX = ObjectAnimator.ofFloat(mImageView, "translationX", 0, 100);
  7. translationX.setDuration(3000);
  8. ObjectAnimator rotation = ObjectAnimator.ofFloat(mImageView, "rotation", 0, 360);
  9. rotation.setDuration(3000);
  10. ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 0.5f);
  11. scaleX.setDuration(3000);
  12. ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 0.5f);
  13. scaleY.setDuration(3000);
  14. //按照顺序执行
  15. /* List<Animator> list=new ArrayList<>();
  16. list.add(translationX);
  17. list.add(rotation);
  18. list.add(scaleX);
  19. list.add(scaleY);
  20. animatorSet.playSequentially(list);*/
  21. animatorSet.playSequentially(translationX);
  22. animatorSet.playSequentially(rotation);
  23. animatorSet.playSequentially(scaleX);
  24. animatorSet.playSequentially(scaleY);
  25. animatorSet.start();
  26. }
6.设置动画对象的宽度
   
   
  1. /**
  2. * 因为这个属性是没有Set和Get方法的,所以我们需要通过封装一个类来提供Set和Get方法
  3. */
  4. private void setWidth(){
  5. Wrapper wrapper=new Wrapper(mImageView);
  6. ObjectAnimator.ofInt(wrapper,"width",0,500).setDuration(3000).start();
  7. }
  8. class Wrapper{
  9. View target=null;
  10. public Wrapper(View target){
  11. this.target=target;
  12. }
  13. public int getWidth(){
  14. return this.target.getLayoutParams().width;
  15. }
  16. public void setWidth(int width){
  17. this.target.setLayoutParams(new LinearLayout.LayoutParams(width,target.getLayoutParams().height));
  18. }
  19. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值