属性动画

含义:通过设置控件属性,达到动画效果。

iv为ImageView

代码实现

  //平移
    public void translate(View v) {
        //-400到0,再从0到400,再400到0
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", -400, 0, 0, 400, 400, 0);
        oa.setDuration(2000);
        oa.start();
    }

    //缩放
    public void scale(View v) {
        //1到0.1,再从0.1到1
        ObjectAnimator sc = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 0.1f, 0.1f, 1f);
        sc.setDuration(2000);
        sc.setRepeatCount(3);
        sc.start();
    }

    //透明
    public void alpha(View v) {
        //1到0,再从0到1
        ObjectAnimator al = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 0, 1);
        al.setDuration(5000);
        al.setRepeatCount(3);
        al.start();
    }

//旋转
    public void rotate(View v) {
        //0°到360°,再从360°到0
       // ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation", 0, 360, 360, 0);
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv,"rotationY",0,360,360,0);
        oa.setDuration(2000);
        oa.setRepeatCount(3);
        oa.start();
    }

    //动画集
    public void fly(View v){
        AnimatorSet as = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", -400, 0, 0, 400, 400, 0);
        ObjectAnimator sc = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 0.1f, 0.1f, 1f);
        ObjectAnimator al = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 0, 1);
        ObjectAnimator oe = ObjectAnimator.ofFloat(iv,"rotationY",0,360,360,0);
        as.setDuration(2000);
        as.setTarget(iv);//设置执行者,imageView
        as.playSequentially(oa,sc,al,oe);//按顺序播放
        //as.playTogether(oa,sc,al,oe);//一起播放
        as.start();//启动
    }

xml实现

//xml文件设置动画属性
public void playxml(View v){
    ObjectAnimator oa= (ObjectAnimator) AnimatorInflater.loadAnimator(this,R.animator.oanimator);//加载xml定义属性
    oa.setTarget(iv);//设置目标
    oa.start();//启动

}

res/animator目录下

<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:propertyName="translationX"
    android:duration="2000"
    android:valueFrom="10"
    android:valueTo="100"
    ></objectAnimator>
</animator>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值