Android开发UI之补间动画-Tween Animation

Tween Animation-补间动画

官网链接-http://developer.android.com/reference/android/view/animation/Animation.html

Animation,继承Object,实现Cloneable接口

实现的直接子类:AlphaAnimation,TransalteAnimation,ScaleAnimation,RotateAnimation,AnimationSet

 

1.透明动画---AlphaAnimation

a.动态java代码实现button按钮的透明效果

 1 rootView.findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {
 2                 
 3     @Override
 4     public void onClick(View v) {
 5     // TODO Auto-generated method stub
 6          AlphaAnimation aa=new AlphaAnimation(0, 1);
 7          aa.setDuration(1000);
 8          v.startAnimation(aa);
 9 
10         }
11  });

b.通过XML属性设置实现透明动画

新建一个Android XML fils,在新建页面,Resource Type选择为Tween Animation,Root element选择为alpha.

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

java代码

1             rootView.findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {
2                 
3                 @Override
4                 public void onClick(View v) {
5                     // TODO Auto-generated method stub
6                     v.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.aa));
7                 }
8             });

 

2.旋转动画-RotateAnimation

a.动态java代码实现button按钮的旋转效果

 1         findViewById(R.id.btnRotate).setOnClickListener(new OnClickListener() {
 2             @Override
 3             public void onClick(View v) {
 4                 // TODO Auto-generated method stub
 5                 RotateAnimation ra=new RotateAnimation(0, 360);
 6                 ra.setDuration(1000);
 7                 v.setAnimation(ra);
 8 
 9             }
10         });

b.通过XML属性设置实现旋转动画

新建一个Android XML fils,在新建页面,Resource Type选择为Tween Animation,Root element选择为rotate.

1 <?xml version="1.0" encoding="utf-8"?>
2 <rotate xmlns:android="http://schemas.android.com/apk/res/android"
3     android:fromDegrees="0" 
4     android:toDegrees="360"
5     android:duration="1000"
6     android:pivotX="50%"                //自身的50%
7     android:pivotY="50%">               //自身的50%
8 </rotate>

java代码

1         findViewById(R.id.btnRotate).setOnClickListener(new OnClickListener() {
2             @Override
3             public void onClick(View v) {
4                 // TODO Auto-generated method stub
5                 v.setAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.ra));
6             }
7         });

 

3.移动动画-TranslateAnimation

a.动态java代码实现button按钮的移动效果

 1         findViewById(R.id.translateAnimation).setOnClickListener(new OnClickListener() {
 2             
 3             @Override
 4             public void onClick(View v) {
 5                 // TODO Auto-generated method stub
 6                 TranslateAnimation ta=new TranslateAnimation(0, 0, 300, 300);
 7                 ta.setDuration(1000);
 8                 v.setAnimation(ta);
 9 
10             }
11         });

b.通过XML属性设置实现移动动画

新建一个Android XML fils,在新建页面,Resource Type选择为Tween Animation,Root element选择为translate.

1 <?xml version="1.0" encoding="utf-8"?>
2 <translate xmlns:android="http://schemas.android.com/apk/res/android"
3     android:fromXDelta="0"
4     android:toXDelta="100"
5     android:fromYDelta="0"
6     android:toYDelta="100" 
7     android:duration="1000">
8 </translate>

java代码

1         findViewById(R.id.translateAnimation).setOnClickListener(new OnClickListener() {
2             
3             @Override
4             public void onClick(View v) {
5                 // TODO Auto-generated method stub
6                 v.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ta));
7             }
8         });

 

4.缩放动画-ScaleAnimation

a.动态java代码实现button按钮的缩放效果

 1         findViewById(R.id.btnScale).setOnClickListener(new OnClickListener() {
 2             
 3             @Override
 4             public void onClick(View v) {
 5                 // TODO Auto-generated method stub
 6                 ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1);
 7                 sa.setDuration(1000);
 8                 v.setAnimation(sa);10             }
11         });

b.通过XML属性设置实现缩放动画

新建一个Android XML fils,在新建页面,Resource Type选择为Tween Animation,Root element选择为scale.

1 <?xml version="1.0" encoding="utf-8"?>
2 <scale xmlns:android="http://schemas.android.com/apk/res/android"
3     android:fromXScale="0"
4     android:toXScale="1"
5     android:fromYScale="0"
6     android:toYScale="1"
7     android:duration="1000" android:pivotX="50%"
8     android:pivotY="50%">
9 </scale>

java代码

1         findViewById(R.id.btnScale).setOnClickListener(new OnClickListener() {
2             
3             @Override
4             public void onClick(View v) {
5                 // TODO Auto-generated method stub
6                 v.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sa));
7             }
8         });

 

5.混合动画--AnimationSet

a.动态java代码实现button按钮的混合动画效果

 1         findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {
 2             
 3             @Override
 4             public void onClick(View v) {
 5                 // TODO Auto-generated method stub
 6                 AnimationSet as=new AnimationSet(true);
 7                 as.setDuration(1000);
 8                 
 9                 AlphaAnimation aa=new AlphaAnimation(0, 1);
10                 aa.setDuration(1000);
11                 as.addAnimation(aa);
12                 
13                 TranslateAnimation ta= new TranslateAnimation(200,0,200,0);
14                 ta.setDuration(1000);
15                 as.addAnimation(ta);
16                 
17                 v.setAnimation(as);
18 
19             }
20         });

b.通过XML属性设置实现混合动画效果

新建一个Android XML fils,在新建页面,Resource Type选择为Tween Animation,Root element选择为set.

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:shareInterpolator="true" 
 4     android:duration="1000">
 5     
 6     <alpha android:fromAlpha="0" android:toAlpha="1"/>
 7     
 8     <translate android:fromXDelta="200"
 9         android:toXDelta="0"
10         android:fromYDelta="200"
11         android:toYDelta="0"/>
12 </set>

java代码:

1         findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {
2             
3             @Override
4             public void onClick(View v) {
5                 v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim));
6             }
7         });

 

你可能感兴趣的:(animation)