Tween 动画 : 不断改变场景中的视图 ( 的观察视觉 ) 形成的动画, Animation
透明度动画: AlphaAnimation
// 创建透明度动画
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
// 动画时间
anim.setDuration(1500);
// 使用动画
view.startAnimation(anim);
平移动画 :TranslateAnimation
//( 起始的 x, 目标 x, 起始的 y, 目标 y)
TranslateAnimation anim1 = new TranslateAnimation(0, 200, 0, 300);
anim1.setDuration(2000);
view.startAnimation(anim1);
缩放动画: ScaleAnimation
// 前面 4 个参数分别是 x 方向 ( 宽 ) 的起始比例 , 终止比例 ,y 方向 ( 高 ) 的起始比例 , 终止比例
// 第 5 第 6 个表示中心点 x 的参考方式,参考比例
// 第 7 第 8 个表示中心点 y 的参考方式,参考比例
ScaleAnimation anim2 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
anim2.setDuration(2000);
view.startAnimation(anim2);
Animation.RELATIVE_TO_SELF 参考自身
Animation.RELATIVE_TO_PARENT 参考父容器
旋转动画: RotateAnimation
RotateAnimation anim3 = new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim3.setDuration(2000);
view.startAnimation(anim3);
Tween 动画的其他设置
// 保持动画之后 / 之前的状态
anim1.setFillAfter(true);
anim1.setFillBefore(true);
// 设置动画的重复次数
anim1.setRepeatCount(1);
// 重复方式 RESTART 从头开始 REVERSE 原路返回
anim1.setRepeatMode(Animation.RESTART);
// 使用动画速率 ( 特效 )
anim3.setInterpolator(new OvershootInterpolator(5.0f));
动画速率类型
AccelerateDecelerateInterpolator 刚开始和结束时时较慢,中间越快
AccelerateInterpolator 开始较慢,接着加速
AnticipateInterpolator 开始时向后退然后再向前甩
CycleInterpolator 重复动画时速率按照正弦曲线来改变
OvershootInterpolator 动画到结束时向前甩然后再回到原位置
LinearInterpolator 以常量速率改变
Tween 动画监听
anim1.setAnimationListener(new Animation.AnimationListener() {
// 动画开始
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this, " 开始 ", Toast.LENGTH_SHORT).show();
}
// 动画结束
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, " 结束 ", Toast.LENGTH_SHORT).show();
int left = view.getLeft() + 200;
int top = view.getTop() + 300;
int w = view.getMeasuredWidth();
int h = view.getMeasuredHeight();
Log.e("m_tag","left"+left+" top:"+top+" "+w+" "+h);
view.layout(left, top, left + w, top + h);
view.invalidate();
}
// 动画重复
@Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(MainActivity.this, " 重复 ", Toast.LENGTH_SHORT).show();
}
});
动画集,可以将多个动画一起同时执行
AnimationSet set = new AnimationSet(true);
ScaleAnimation anim4 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation anim5 = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 添加动画
set.addAnimation(anim4);
set.addAnimation(anim5);
set.setDuration(2000);
// 启动
view.startAnimation(set);
动画集,可以将多个动画一起同时执行
1 、在 res 下建立 anim 文件夹 , 然后在 res/anim 文件夹中创建 xml 文件,跟标签使用 set( 表示 AnimationSet)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:repeatMode="restart"
android:shareInterpolator="false">
<alpha
android:duration="1500"
android:fromAlpha="0"
android:toAlpha="1" />
<rotate
android:fromDegrees="0"
android:pivotX="50%p"
android:pivotY="50%"
android:toDegrees="360"
android:duration="2000" />
</set>
注意旋转、缩放中心点的设置如果时固定值 10 100 表示的就是绝对坐标点,如果是百分比 50% 20% 表示相对于自身,如果时百分比后面有 p 如 50%p -100%p 相对于父容器
2 、在 java 中使用 AnimationUtil 加载动画
Animation anim6 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.my_anim);
view.startAnimation(anim6);
实际应用中
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_alpha:
//创建透明度动画
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
//动画时间
anim.setDuration(5000);
//使用动画
view.startAnimation(anim);
break;
case R.id.btn_tran:
//(起始的x,目标x,起始的y,目标y)
TranslateAnimation anim1 = new TranslateAnimation(0, 200, 0, 300);
anim1.setDuration(2000);
//设置动画之前是否停留在原处(保持状态)
//anim1.setFillAfter(true);
//设置动画之后是否停留在原处(保持状态)
//anim1.setFillBefore(true);
//设置重复次数
anim1.setRepeatCount(1);
anim1.setAnimationListener(new Animation.AnimationListener() {
//动画开始
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this,"开始",Toast.LENGTH_LONG).show();
}
//动画结束
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this,"结束",Toast.LENGTH_LONG).show();
int left = view.getLeft()+200;
int top = view.getTop()+300;
int w = view.getMeasuredWidth();
int h = view.getMeasuredHeight();
view.layout(left,top,left+w,top+h);
view.invalidate();
}
//动画重复
@Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(MainActivity.this,"重复",Toast.LENGTH_LONG).show();
}
});
view.startAnimation(anim1);
break;
case R.id.btn_scale:
ScaleAnimation anim2 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
anim2.setDuration(2000);
view.startAnimation(anim2);
break;
case R.id.btn_rotate:
RotateAnimation anim3 = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//使用动画速率(特效)
//anim3.setInterpolator(new OvershootInterpolator(5.0f));
//anim3.setInterpolator(new AccelerateDecelerateInterpolator());
anim3.setInterpolator(new AnticipateInterpolator());
//anim3.setInterpolator(new CycleInterpolator(1));
//anim3.setInterpolator(new OvershootInterpolator());
anim3.setDuration(2000);
view.startAnimation(anim3);
break;
case R.id.btn_set:
AnimationSet set = new AnimationSet(true);
ScaleAnimation anim4 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
RotateAnimation anim5 = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//添加动画
set.addAnimation(anim4);
set.addAnimation(anim5);
set.setDuration(2000);
//启动
view.startAnimation(set);
break;
case R.id.btn_xml:
Animation anim6 = AnimationUtils.loadAnimation(MainActivity.this,R.anim.my_anim);
view.startAnimation(anim6);
break;
}
}
}