属性动画Demo

来源:互联网 发布:魔兽数据库7.0手机 编辑:程序博客网 时间:2024/06/16 08:35
package com.saiermeng.easypropertyanim;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ImageView;public class MainActivity extends Activity {    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv = (ImageView) findViewById(R.id.iv);    }    public void alpha(View view) {        // 透明度        // target 把动画作用于哪个目标对象上        // propertyName 属性名称        // values 让控件发送动画的值        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.1f, 0.3f,                0.5f, 0.7f, 0.9f, 1.0f);        oa.setDuration(3000);        oa.setRepeatCount(2);        oa.setRepeatMode(ObjectAnimator.REVERSE);        // 开始播放动画        oa.start();    }    public void rotate(View view) {        // 旋转        // target 把动画作用于哪个目标对象上        // propertyName 属性名称        // values 让控件发送动画的值        ObjectAnimator ra = ObjectAnimator.ofFloat(iv, "rotation", 0, 30, 60,                90, 120, 150, 180, 210, 240, 270, 300, 330, 360);        ra.setDuration(3000);        ra.setRepeatCount(2);        ra.setRepeatMode(ObjectAnimator.REVERSE);        // 开始播放动画        ra.start();    }    public void scale(View view) {        // 缩放        // target 把动画作用于哪个目标对象上        // propertyName 属性名称        // values 让控件发送动画的值        ObjectAnimator sa = ObjectAnimator.ofFloat(iv, "scaleX", 0, 0.2f, 0.4f,                0.6f, 0.8f, 1.0f);        sa.setDuration(3000);        sa.setRepeatCount(2);        sa.setRepeatMode(ObjectAnimator.REVERSE);        // 开始播放动画        sa.start();        ObjectAnimator say = ObjectAnimator.ofFloat(iv, "scaleY", 0, 0.2f, 0.4f,                0.6f, 0.8f, 1.0f);        say.setDuration(3000);        say.setRepeatCount(2);        say.setRepeatMode(ObjectAnimator.REVERSE);        // 开始播放动画        say.start();    }    public void translate(View view) {        // 旋转        // target 把动画作用于哪个目标对象上        // propertyName 属性名称        // values 让控件发送动画的值        ObjectAnimator ta = ObjectAnimator.ofFloat(iv, "translationY", 0, 30, 60,                90);        ta.setDuration(3000);        ta.setRepeatCount(2);        ta.setRepeatMode(ObjectAnimator.REVERSE);        // 开始播放动画        ta.start();    }    public void set(View view) {//      alpha(view);//      rotate(view);//      scale(view);//      translate(view);        AnimatorSet set = new AnimatorSet();        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.1f, 0.3f,                0.5f, 0.7f, 0.9f, 1.0f);        oa.setDuration(3000);        oa.setRepeatCount(2);        oa.setRepeatMode(ObjectAnimator.REVERSE);        ObjectAnimator ra = ObjectAnimator.ofFloat(iv, "rotation", 0, 30, 60,                90, 120, 150, 180, 210, 240, 270, 300, 330, 360);        ra.setDuration(3000);        ra.setRepeatCount(2);        ra.setRepeatMode(ObjectAnimator.REVERSE);        ObjectAnimator sa = ObjectAnimator.ofFloat(iv, "scaleX", 0, 0.2f, 0.4f,                0.6f, 0.8f, 1.0f);        sa.setDuration(3000);        sa.setRepeatCount(2);        sa.setRepeatMode(ObjectAnimator.REVERSE);        ObjectAnimator ta = ObjectAnimator.ofFloat(iv, "translationY", 0, 30, 60,                90);        ta.setDuration(3000);        ta.setRepeatCount(2);        ta.setRepeatMode(ObjectAnimator.REVERSE);        // 按照顺序播放//      set.playSequentially(oa,ra,sa,ta);        // 综合播放        set.playTogether(oa,ra,sa,ta);        set.start();    }}
0 0