android补间动画有哪几种,Android补间动画、属性动画 常用功能总结

1.补间动画

无需逐一定义每一帧,只要定义开始、结束的帧,和指定动画持续时间。

补间动画有4种(均为Animation抽象类子类):

AlphaAnimation(透明度,0~1)

ScaleAnimation(大小缩放,X、Y轴缩放,还包括缩放中心pivotX、pivotY)

TranslationAnimation(位移,X、Y轴位移)

RotateAnimation(旋转,包括缩放中心pivotX、pivotY)

public void move(View view){

//定义一个位移补间动画,X轴从0变化到100,Y轴不变

TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0);

//设置动画持续时间

animation.setDuration(1000);

//设置动画结束后效果保留

animation.setFillAfter(true);

//控制动画先慢后快

animation.setInterpolator(new AccelerateInterpolator());

//找到对象,开启动画

mImageView = (ImageView) findViewById(R.id.imageView1);

mImageView.startAnimation(animation);

指定3个信息后,动画是匀速的,效果同逐帧动画。

上例中还有一个属性,可以控制速度,即为Interpolator(插值),有以下几种(Interpolator的实现类):

LinearInterpolator(匀速)

AccelerateInterpolator(先慢后快)

AccelerateDecelerateInterpolator(先慢中快后慢)

DecelerateInterpolator(先快后慢)

CycleInterpolator(循环播放,速度为正弦曲线)

AnticipateInterpolator(先回撤,再匀速向前)

OvershootInterpolator(超过,拉回)

BounceInterpolator(回弹)

不仅可以在代码中创建Animation对象,很多情况下,是采用动画资源文件来定义补间动画。资源目录:res/anim/anim.xml

android:fromYScale="1.0"

android:toXScale="0.01"

android:toYScale="0.01"

android:pivotX="50%"

android:pivotY="50%"

android:fillAfter="true"

android:duration="1000"/>

android:toAlpha="0.05"

android:duration="3000"/>

android:toDegrees="1800"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000"/>

然后在代码中使用AnimationUtils工具类加载动画资源,返回一个Animation对象

Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);

2.属性动画

继承关系:

Animator(属性动画的基类)→ValueAnimator→ObjectAnimator

如何使用:

1.调用ObjectAnimator的静态工厂方法创建动画(ofInt、ofFloat、ofObject)

2.调用SetXxx()设置动画持续时间、插值方式、重复次数等。

3.监听事件

4.调用Animator对象的start()方法启动动画

//标准示范,单个动画属性(ObjectAnimator)执行

public void move1(View view){

//直接调用ObjectAnimator的工厂方法(静态方法),可以创建ObjectAnimator实例

//前2个参数分别要指定:操作的具体的对象、操作的对象的属性。本例中,具体对象为ImageView,属性为X轴位移

//第3个参数,是操作的对象的属性,其动画变化的范围的。本例中,X轴位移:0f~200f

ObjectAnimator.ofFloat(mImageView, "translationX", 0f,200f)

.setDuration(2000)

.start();

}

//同一个对象设置多个属性动画,会同时执行。可见,属性动画实例调用start方法后,是一个异步的过程

public void move2(View view){

ObjectAnimator.ofFloat(mImageView, "translationX", 0f,200f).setDuration(2000).start();

ObjectAnimator.ofFloat(mImageView, "translationY", 0f,200f).setDuration(2000).start();

ObjectAnimator.ofFloat(mImageView, "rotation", 0f,360f).setDuration(2000).start();

}

//(多个动画同时时推荐),使用PropertyValuesHolder,先将多个属性动画hold住,再一起开启动画,可以实现同样的效果。如此做法对动画进行了优化,使用多个属性的时候,更加节省系统资源。

public void move3(View view){

PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0f,200f);

PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0f,200f);

PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0f,360f);

ObjectAnimator.ofPropertyValuesHolder(mImageView, p1,p2,p3).setDuration(2000).start();

}

//(多个动画需求不同时时推荐),利用AnimatorSet,组合多个Animation,可以对多个动画属性进行顺序控制

//同时执行:set.playTogether(animator1,animator2,animator3)

//顺序执行:set.playSequentially(animator1,animator2,animator3)

//分布执行:play().with(); play().after();

public void move4(View view){

ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageView, "translationX", 0f,200f);

ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageView, "translationY", 0f,200f);

ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageView, "rotation", 0f,360f);

AnimatorSet set = new AnimatorSet();

set.play(animator1).with(animator2);

set.play(animator3).after(animator1);

set.setDuration(1000).start();

}

利用addListener(listener)给Animator对象设置监听器。监听事件有很多种,如果参数直接给一个new AnimatorListener,系统会直接override全部的监听事件让我们覆写onAnimationStart onAnimationRepeat onAnimationEnd onAnimationCancel。如果只想监听个别事件,参数写成new AnimatorListenerAdapter,然后Alt+Shift+S,选择需要覆写的实现方法即可。

//对动画设置监听事件

public void move5(View view){

ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f,1.0f);

animator.setDuration(1000).start();

animator.addListener(new AnimatorListenerAdapter() {

@Override

public void onAnimationEnd(Animator animation) {

super.onAnimationEnd(animation);

mImageView.setVisibility(View.VISIBLE);

ObjectAnimator.ofFloat(mImageView, "alpha", 0f,1.0f).setDuration(1000).start();

}

});

}

3.属性动画实例(点击父菜单图标,弹出多个子菜单图标)

public class AnimatorPath extends Activity implements OnClickListener{

private int [] res = {R.id.imageView0,R.id.imageView1,R.id.imageView2,

R.id.imageView3,R.id.imageView4,R.id.imageView5,

R.id.imageView6,R.id.imageView7,};

private List imageViewList = new ArrayList();

private boolean flag = true;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.animator_path);

//用集合的方式遍历ImageView,并添加进集合

for(int i =0 ; i< res.length; i++){

ImageView imageView =(ImageView) findViewById(res[i]);

imageView.setOnClickListener(this);

imageViewList.add(imageView);

}

}

//给帧布局文件中ImageView添加点击事件(使用Animator动画,动画后的ImageView也可以设置点击监听)

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.imageView0:

if(flag){

startAnim();

flag = false;

}else{

endAnim();

flag = true;

}

break;

default:

break;

}

}

//设置每个小球向前不同的距离,此处需求几个动画同时执行,就使用了最简单的实现方式

private void startAnim() {

for(int i =0; i< res.length; i++){

ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY",0f, 100f*i);

animator.setDuration(500).setStartDelay(150*i);

animator.setInterpolator(new BounceInterpolator());

animator.start();

}

}

private void endAnim() {

for(int i = 0; i

ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY", 100f*i,0f);

animator.setDuration(250).setStartDelay(150*i);

animator.start();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值