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

时间:2023-01-09 23:56:58

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按钮的透明效果

 rootView.findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {

     @Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlphaAnimation aa=new AlphaAnimation(0, 1);
aa.setDuration(1000);
v.startAnimation(aa); }
});

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

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

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

java代码

             rootView.findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {

                 @Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.aa));
}
});

2.旋转动画-RotateAnimation

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

         findViewById(R.id.btnRotate).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RotateAnimation ra=new RotateAnimation(0, 360);
ra.setDuration(1000);
v.setAnimation(ra); }
});

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

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

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

java代码

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

3.移动动画-TranslateAnimation

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

         findViewById(R.id.translateAnimation).setOnClickListener(new OnClickListener() {

             @Override
public void onClick(View v) {
// TODO Auto-generated method stub
TranslateAnimation ta=new TranslateAnimation(0, 0, 300, 300);
ta.setDuration(1000);
v.setAnimation(ta); }
});

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

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

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

java代码

         findViewById(R.id.translateAnimation).setOnClickListener(new OnClickListener() {

             @Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ta));
}
});

4.缩放动画-ScaleAnimation

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

         findViewById(R.id.btnScale).setOnClickListener(new OnClickListener() {

             @Override
public void onClick(View v) {
// TODO Auto-generated method stub
ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1);
sa.setDuration(1000);
v.setAnimation(sa); }
});

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

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

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

java代码

         findViewById(R.id.btnScale).setOnClickListener(new OnClickListener() {

             @Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sa));
}
});

5.混合动画--AnimationSet

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

         findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {

             @Override
public void onClick(View v) {
// TODO Auto-generated method stub
AnimationSet as=new AnimationSet(true);
as.setDuration(1000); AlphaAnimation aa=new AlphaAnimation(0, 1);
aa.setDuration(1000);
as.addAnimation(aa); TranslateAnimation ta= new TranslateAnimation(200,0,200,0);
ta.setDuration(1000);
as.addAnimation(ta); v.setAnimation(as); }
});

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

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

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:duration="1000"> <alpha android:fromAlpha="0" android:toAlpha="1"/> <translate android:fromXDelta="200"
android:toXDelta="0"
android:fromYDelta="200"
android:toYDelta="0"/>
</set>

java代码:

         findViewById(R.id.btnAnimation).setOnClickListener(new OnClickListener() {

             @Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim));
}
});