Property Animation(属性动画)用法示例

主题:Property Animation(属性动画)用法


前言:在这里只是纯粹地记录一下Property Animation用法,方便日后查找等。一个类似笔记作用吧


目录:Property Animation动画效果主要五种:

  • Scale(缩放)
  • Translation(移动)
  • Rotation(旋转)
  • Alpha(透明度)
  • Set(组合动画)

内容:下面依次给出这五种动画效果以及代码(可根据代码编辑自己想要的动画效  果),和解释动画要用到的知识


依次给出五种动画效果以及代码

Scale(缩放)效果:


ScaleXGif
//进行X轴缩放操作
public static void startScaleX(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 3f, -1f, 2f);
    animator.setDuration(5000);
    animator.start();
}

/

ScaleYGif
//进行Y轴缩放操作
public static void startScaleY(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "scaleY", 1f, 3f, 1f, 4f);
    animator.setDuration(5000);
    animator.start();
}

/

ScaleGif
//进行缩放操作(x轴、y轴同时缩放)
public static void startScale(View view) {
    ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleX", 1f, 3f, 1f);
    ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "scaleY", 1f, 3f, 1f, 6f);
    AnimatorSet set = new AnimatorSet();
    set.setDuration(5000);
    set.playTogether(animator1, animator2);
    set.start();
}

/

Translation(移动)效果:


TranslationXGif
//向左移出屏幕
public static void startTranslationX(View view) {
    float translationX = view.getTranslationX();
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", translationX, -1080f);
    animator.setDuration(2000);
    animator.start();
}

/

TranslationXBGif
//先向左移出屏幕,然后再移动回来
public static void startTranslationXB(View view) {
    float translationX = view.getTranslationX();
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", translationX, -1080f, translationX);
    animator.setDuration(2000);
    animator.start();
}
 
  
/

TranslationYGif
//向上移出屏幕
public static void startTranslationY(View view) {
    float translationY = view.getTranslationY();
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", translationY, -1920f);
    animator.setDuration(3000);
    animator.start();
}

/

TranslationYBGif
//先向上移出屏幕,然后再移动回来
public static void startTranslationYB(View view) {
    float translationY = view.getTranslationY();
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", translationY, -1920f, translationY);
    animator.setDuration(3000);
    animator.start();
}

/

TranslationGif
//移动(x轴,y轴同时移动)
public static void startTranslation(View view) {
    float translationX = view.getTranslationX();
    float translationY = view.getTranslationY();
    ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationX", translationX, 540f, translationX);
    ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationY", translationY, 960f, translationY);
    AnimatorSet set = new AnimatorSet();
    set.setDuration(2000);
    set.playTogether(animator1, animator2);
    set.start();
}

/

Rotation(旋转)效果:


Rotation720Gif

//旋转720
public static void startRotation720(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 720f);
    animator.setDuration(2000);
    animator.start();
}

/

RotationGif
//旋转
public static void startRotation(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f, -360f);
    animator.setDuration(2000);
    animator.start();
}

/

Alpha(透明度)效果:


AlphaGif
//常规变换成全透明
public static void startAlpha(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f);
    animator.setDuration(2000);
    animator.start();
}

/

AlphaBackGif
//常规变换成全透明,再从全透明变换成常规
public static void startAlphaBack(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 1f);
    animator.setDuration(3000);
    animator.start();
}

/

Set(组合动画)效果


AnimatorSetGif
//after(Animator anim)   将现有动画插入到传入的动画之后执行
//after(long delay)   将现有动画延迟指定毫秒后执行
//before(Animator anim)   将现有动画插入到传入的动画之前执行
//with(Animator anim)   将现有动画和传入的动画同时执行
public static void startAnimatorSet(View view) {
    ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "rotation", 0f, 1080f);
    float translationX = view.getTranslationX();
    ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationX",translationX, -540f, 0f);
    ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 1f);
    AnimatorSet animSet = new AnimatorSet();
    animSet.play(animator2).with(animator3).after(animator1);
    animSet.setDuration(5000);
    animSet.start();
}

/

AnimatorSetTogetherGif
//同时播放所有提供的动画
public static void startAnimatorSetTogether(View view) {
    float translationY = view.getTranslationY();
    ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationY", translationY, -800f, 0f);
    ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "rotation", 0f, 3240f);
    ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 1f);
    AnimatorSet set = new AnimatorSet();
    set.setDuration(5000);
    set.playTogether(animator1, animator2, animator3);
    set.start();
}

/


动画要用到的知识:

Scale,Translation,Rotation,Alpha实现动画效果主要是用到ObjectAnimator类。

ObjectAnimator用法:

ObjectAnimator animator =ObjectAnimator.ofFloat(Object target, String propertyName,float...values);

animator.setDuration(longduration);

animator.start();

解释:

1.构造得到ObjectAnimator

 1.1.target:对其属性进行动画的对象

 1.2.propertyName:动画的属性名称(即:缩放或移动或旋转或透明度)

 1.3.动画随时间变化并根据该组值而变化

2.设置动画的时间长度

3.启动动画


实现组合动画效果则使用AnimatorSet类

AnimatorSet用法:

ObjectAnimator animator1 = ObjectAnimator.ofFloat(Object target, String propertyName, float... values);

ObjectAnimator animator2 = ObjectAnimator.ofFloat(Object target, String propertyName, float... values);

AnimatorSet set =newAnimatorSet();

set.setDuration(long duration);
set.playTogether(animator1, animator2);
set.start();

解释:

1.构造得到animator1,animator2

2.构造得到AnimatorSet 

3.设置动画的时间长度

4.设置同时播放所有提供的动画

5.启动动画


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值