当前位置: 移动技术网 > 移动技术>移动开发>Android > 一起学Android之Animation

一起学Android之Animation

2019年05月23日  | 移动技术网移动技术  | 我要评论

本文以一个简单的小例子,简述在android开发中,动画的简单应用,仅供学习分享使用。

概述

android提供了各种强大的apis,用于将动画应用到ui元素中,来丰富应用程序的功能和应用。

动画分类

在android框架中,动画主要分为三类【这三种动画系统都是可行的选择,但一般来说,属性动画系统是首选的使用方法,因为它更灵活,提供了更多的功能】,具体如下:

  • 帧动画:将图像资源按顺序一帧一帧的播放出来,形成动画()。
  • 补间动画:又叫视图动画,是比较旧的系统,只能用于视图组件,相对比较容易设置和提供能力满足程序的需要。
  • 属性动画:在android 3.0(api等级11)中引入的属性动画系统,允许您对任何对象的属性进行动画处理,包括未呈现到屏幕上的属性。该系统是可扩展的,并允许自定义动画类型的属性。

帧动画

将动画资源文件作为图片控件(imageview)的背景图(background)。

帧动画涉及知识点如下:

  • animationdrawable: 用于创建逐帧动画的对象,由一系列可拖动对象,可用作视图对象的背景
  • isrunning() 是否正在运行
  • stop() 停止动画
  • start() 开始运行

帧动画核心代码

 在drawable目录下,新增一个动画资源配置文件【animation-list节点下包含item子节点,item有两个属性,android:drawable=图像资源id,android:duration=周期】,如下:

 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/n0" android:duration="300"></item>
 4     <item android:drawable="@drawable/n1" android:duration="300"></item>
 5     <item android:drawable="@drawable/n2" android:duration="300"></item>
 6     <item android:drawable="@drawable/n3" android:duration="300"></item>
 7     <item android:drawable="@drawable/n4" android:duration="300"></item>
 8     <item android:drawable="@drawable/n5" android:duration="300"></item>
 9     <item android:drawable="@drawable/n6" android:duration="300"></item>
10     <item android:drawable="@drawable/n7" android:duration="300"></item>
11     <item android:drawable="@drawable/n8" android:duration="300"></item>
12     <item android:drawable="@drawable/n9" android:duration="300"></item>
13 </animation-list>

java设置代码如下:

 1 private animationdrawable drawable;
 2 
 3     @override
 4     protected void oncreate(bundle savedinstancestate) {
 5         super.oncreate(savedinstancestate);
 6         setcontentview(r.layout.activity_drawable);
 7         imageview imageview= (imageview) this.findviewbyid(r.id.ivletter);
 8         drawable= (animationdrawable) imageview.getbackground();
 9         drawable.start();
10     }
11 
12     @override
13     public boolean ontouchevent(motionevent event) {
14         if(event.getaction()==motionevent.action_down){
15             if(drawable.isrunning()) {
16                 drawable.stop();
17             }else{
18                 drawable.start();
19             }
20         }
21         return super.ontouchevent(event);
22     }

补间动画

补间动画,又称渐变动画是指定义起始状态,结束状态,中间状态等,然后其他部分由程序自动生成,从而形成动画。

补间动画涉及知识点如下:

  • translateanimation 平移动画 控制对象位置的动画。
  • rotateanimation 旋转动画 控制对象旋转的动画。这个旋转需要放置在xy平面上。您可以指定中心要使用的点,其中(0,0)是左上角。如果未指定,则(0,0)为默认旋转点。
  • scaleanimation 缩放动画 控制对象的比例尺的动画。您可以指定点用于缩放中心。
  • alphaanimation 透明度动画 控制对象的alpha级的动画,通过更改透明度属性,对于对象的淡入淡出,这是一个很有用的方法。
  • animationset 动画集合 上述动画可以组合使用。
  • setfillafter(true); 设置动画结束后的填充
  • setduration(2000); 动画周期
  • setrepeatcount(2); 重复次数
  • setrepeatmode(animation.reverse); 重复模式

补间动画核心代码如下:

 1   /**
 2      * 平移
 3      * @param v
 4      */
 5     protected  void transfer_click(view v){
 6 
 7         //参数是平移的起始坐标和结束坐标(起始x轴位置,结束x轴位置,起始y轴位置,结束y轴位置)的改变量。
 8         //translateanimation trans=new translateanimation(0.0f,300f,0.0f,300f);
 9         //fromxtype 动画平移改变量的类型
10         //animation.relative_to_self,0 表示控件现在的坐标+0*控件本身的宽度或高度
11         //animation.relative_to_self,0.5f 表示控件现在的坐标+0.5*控件本身的宽度或高度
12         //animation.relative_to_parent 相对于父控件,计算方式和animation.relative_to_self一样
13         //fromxvalue 起始坐标值的改变量,如果类型是absolute,则此值为绝对数字,否则则表示百分比(0-1)之间。
14         translateanimation trans=new translateanimation(animation.relative_to_self,0,animation.relative_to_self,1,animation.relative_to_self,0,animation.relative_to_self,1);
15         trans.setduration(2000);//设置周期
16         trans.setfillafter(true);//当结束时保持结束位置
17         trans.setrepeatcount(2);//设置重复次数
18         trans.setrepeatmode(animation.reverse);//重复模式
19         ivtaichi.startanimation(trans);//启动
20     }
21 
22     /**
23      * 旋转
24      * @param v
25      */
26     protected void rotate_click(view v){
27         //参数是旋转的起始偏移量(度数),结束度数,旋转中心点(相对x轴 位置和y轴位置)。
28         //rotateanimation rotate=new rotateanimation(0.0f,90.f,100.0f,100.0f);
29         rotateanimation rotate =new rotateanimation(0.0f,360.0f,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
30         rotate.setfillafter(true);
31         rotate.setduration(2000);
32         rotate.setrepeatcount(2);
33         rotate.setrepeatmode(animation.reverse);
34         ivtaichi.startanimation(rotate);//启动
35     }
36 
37     /**
38      * 缩放
39      * @param v
40      */
41     protected void scale_click(view v){
42         //fromx tox 动画起始和结束时的x轴水平缩放因子
43         //fromy toy 动画起始和结束时的y轴水平缩放因子
44         scaleanimation scale=new scaleanimation(0.5f,1.5f,0.5f,1.5f);
45         scale.setfillafter(true);
46         scale.setduration(2000);
47         scale.setrepeatcount(2);
48         scale.setrepeatmode(animation.reverse);
49         ivtaichi.startanimation(scale);//启动
50     }
51 
52     /**
53      * 透明度动画
54      * @param v
55      */
56     protected void alpha_click(view v){
57         //fromalpha toalpha 动画起始和结束时的透明度。范围(0,1)
58         alphaanimation alpha=new alphaanimation(0,1);
59         alpha.setfillafter(true);
60         alpha.setduration(2000);
61         alpha.setrepeatcount(2);
62         alpha.setrepeatmode(animation.reverse);
63         ivtaichi.startanimation(alpha);//启动
64     }
65 
66     /**
67      * 集合动画
68      * @param v
69      */
70     protected  void set_click(view v){
71         animationset set=new animationset(true);
72         //translateanimation animation1=new translateanimation(0.0f,300.0f,0.0f,300.0f);
73         rotateanimation animation2 =new rotateanimation(0.0f,360.0f,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
74         scaleanimation animation3=new scaleanimation(0.0f,1.0f,0.0f,1.0f);
75         alphaanimation animation4=new alphaanimation(0,1);
76         //set.addanimation(animation1);
77         set.addanimation(animation2);
78         set.addanimation(animation3);
79         set.addanimation(animation4);
80         set.setfillafter(true);
81         set.setduration(2000);
82         set.setrepeatcount(2);
83         set.setrepeatmode(animation.reverse);
84         ivtaichi.startanimation(set);//启动
85     }

属性动画

属性动画主要通过改变对象的属性,来实现动画,可以进行扩展,且功能丰富。

属性动画涉及知识点如下:

  • objectanimator 该valueanimator的子类提供了对目标对象上的动画属性的支持。该类的构造函数使用参数来定义将被动画化的目标对象以及将被动画化的属性的名称。
  • setduration(2000); 动画周期
  • setrepeatcount(2); 重复次数
  • setrepeatmode(animation.reverse); 重复方式
  • start(); 启动

属性动画核心代码如下:

 1 /**
 2      * 平移
 3      * @param v
 4      */
 5     protected  void transfer_click(view v){
 6         //target 属性动画的目标控件
 7         //propertyname 产生动画的属性,所有的属性必须拥有set,get方法
 8         //values 属性动画的范围集合
 9         objectanimator objectanimator =objectanimator.offloat(ivtaichi,"translationx",0,200,-200,0);
10         objectanimator.setduration(2000);
11         objectanimator.setrepeatcount(2);
12         objectanimator.setrepeatmode(animation.reverse);
13         objectanimator.start();
14     }
15 
16     /**
17      * 旋转
18      * @param v
19      */
20     protected void rotate_click(view v){
21         objectanimator objectanimator =objectanimator.offloat(ivtaichi,"rotationx",0,180);
22         objectanimator.setduration(2000);
23         objectanimator.setrepeatcount(2);
24         objectanimator.setrepeatmode(animation.reverse);
25         objectanimator.start();
26     }
27 
28     /**
29      * 缩放
30      * @param v
31      */
32     protected void scale_click(view v){
33         objectanimator objectanimator =objectanimator.offloat(ivtaichi,"scalex",0,1,0);
34         objectanimator.setduration(2000);
35         objectanimator.setrepeatcount(2);
36         objectanimator.setrepeatmode(animation.reverse);
37         objectanimator.start();
38     }
39 
40     /**
41      * 透明度
42      * @param v
43      */
44     protected void alpha_click(view v){
45         objectanimator objectanimator =objectanimator.offloat(ivtaichi,"alpha",0,1);
46         objectanimator.setduration(2000);
47         objectanimator.setrepeatcount(2);
48         objectanimator.setrepeatmode(animation.reverse);
49         objectanimator.start();
50     }
51 
52     /**
53      * 集合动画
54      * @param v
55      */
56     protected  void set_click(view v){
57         animatorset set=new animatorset();
58         list<animator> list=new arraylist<animator>() ;
59         objectanimator objectanimator1 =objectanimator.offloat(ivtaichi,"translationx",0,200);
60         objectanimator objectanimator2 =objectanimator.offloat(ivtaichi,"rotationx",0,180);
61         objectanimator objectanimator3 =objectanimator.offloat(ivtaichi,"scalex",0,1);
62         objectanimator objectanimator4 =objectanimator.offloat(ivtaichi,"alpha",0,1);
63         list.add(objectanimator1);
64         list.add(objectanimator2);
65         list.add(objectanimator3);
66         list.add(objectanimator4);
67         //播放一序列的动画对象
68         set.playsequentially(list);
69         //
70         set.start();
71      }

 

 

备注

学而不思则罔,思而不学则殆!!!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网