Android中的动画(三)属性动画

    属性动画可以对任何对象做动画。

使用代码设置属性动画

  1. 平移
    例如,改变按钮对象 btn_1 的translationY属性,让其移动一段距离
ObjectAnimator.ofFloat(btn_1, "translationY", btn_1.getHeight()).start();
  1. 改变背景色
    改变按钮 btn_2 的背景色,实现从0xFFFFFFFF到0xFFFF0000的渐变,并且无线循环,而且有反转效果
ValueAnimator anim = ObjectAnimator
                .ofInt(btn_2, "backgroundColor", 0xFFFFFFFF, 0xFFFF0000);
anim.setDuration(3000);
anim.setEvaluator(new ArgbEvaluator());
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.start();
  1. 动画集合
    让控件btn_3旋转、平移、缩放、变化透明度
AnimatorSet set = new AnimatorSet();
set.playTogether(
    ObjectAnimator.ofFloat(btn_3, "rotationX", 0, 360),
    ObjectAnimator.ofFloat(btn_3, "rotationY", 0, 180),
    ObjectAnimator.ofFloat(btn_3, "rotation", 0, -90),
    ObjectAnimator.ofFloat(btn_3, "translationX", 0, 90),
    ObjectAnimator.ofFloat(btn_3, "translationY", 0, 90),
    ObjectAnimator.ofFloat(btn_3, "scaleX", 1, 2),
    ObjectAnimator.ofFloat(btn_3, "scaleY", 1, 2),
    ObjectAnimator.ofFloat(btn_3, "alpha", 1, 0.2f, 1)
);
set.setDuration(5000).start();

使用xml设置属性动画

在res/目录下建立animator文件夹,然后创建xml格式的动画文件
这里写图片描述

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">

    <objectAnimator
        android:duration="3000"
        android:propertyName="backgroundColor"
        android:valueFrom="0xFFFFFFFF"
        android:valueTo="0xFFFF0000"
        android:valueType="colorType" />

</set>

对按钮btn_4使用动画

AnimatorSet set = (AnimatorSet) AnimatorInflater
             .loadAnimator(ThreeActivity.this, R.animator.animator);
set.setTarget(btn_4);
set.start();

放一张动画之后的效果图:
这里写图片描述

源码传送门!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值