浅谈属性动画(上 Java代码实现)

来源:互联网 发布:b2b软件 编辑:程序博客网 时间:2024/05/22 09:48

写在前面:今天在这里我们来浅谈一下属性动画 :

 
它是最为强大的动画,弥补了补间动画的缺点,实现 位置+ 视觉 的变化。并且可以自定义插值器等,实现各种效果分为Java代码实现的形式 与XML文件 实现的形式。

ObjectAnimator 属性动画特点:动画效果会改变控件的位置,且开启动画的是动画对象,而不是控件对象   注意:

  属性动画是在Android3.0以后出现的新特性,所以要把新特性文件最低兼容版本修改为11以上

注意:

  如果你想让你的APP与众不同,就请使用自定义控件,如果你想让你的APP酷炫非凡,就请用Anroid动画 如果你想让APP与众不同,又炫酷非凡,就自定义控价加动画结合使用--------尼古拉斯~赵四

MainActivity布局:

<Button    android:id="@+id/alpha_bt"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="alpha"/><Button    android:id="@+id/translationY_bt"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="translationY"/><Button    android:id="@+id/scaleX_bt"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="scaleX"/><Button    android:id="@+id/rotationY_bt"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="rotationY"/><Button    android:id="@+id/AnimatorSet_bt"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="AnimatorSet"/><ImageButton    android:onClick="yyyy"    android:id="@+id/animation_iv"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:src="@drawable/a8"    android:background="@null"/>

MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private ImageButton imageView;    private Button alpha_bt;    private Button rotationY_bt;    private Button scaleX_bt;    private Button translationX_bt;    private Button AnimatorSet_bt;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        // 调用动画XML文件        /**         * 首先使用 XML 的形式,进行引用         */        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.oanmator);        animator.setTarget(imageView);        animator.start();    }    private void initView() {        //找到ImageView控件对象        imageView = (ImageButton) findViewById(R.id.animation_iv);        //找到Button控件对象.        alpha_bt = (Button) findViewById(R.id.alpha_bt);        rotationY_bt = (Button) findViewById(R.id.rotationY_bt);        scaleX_bt = (Button) findViewById(R.id.scaleX_bt);        translationX_bt = (Button) findViewById(R.id.translationY_bt);        AnimatorSet_bt = (Button) findViewById(R.id.AnimatorSet_bt);        //button设置点击事件        alpha_bt.setOnClickListener(this);        rotationY_bt.setOnClickListener(this);        scaleX_bt.setOnClickListener(this);        translationX_bt.setOnClickListener(this);        AnimatorSet_bt.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.alpha_bt: // 透明动画                // 得到 ObjectAnimator对象,                // 参数1View控件,代表你要修改的控件属性,                // 参数2:字符串:什么类型的动画,                // 参数3:控件修改的参数 float 数组                ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});                // 设置动画执行时长                alpha.setDuration(2000);                // 设置动画执行的模式                alpha.setRepeatMode(ObjectAnimator.RESTART);                // 设置动画的执行次数 注意:执行一次后,再执行几次                alpha.setRepeatCount(0);                // 开始动画效果                alpha.start();                break;            case R.id.rotationY_bt: // 旋转动画                ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", new float[]{90f, 180f, 270f, 360f});                rotationY.setDuration(2000);                rotationY.setRepeatCount(0);                rotationY.setRepeatMode(ObjectAnimator.RESTART);                rotationY.start();                break;            case R.id.scaleX_bt: // 缩放动画                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 5f, 4f, 3f, 2f, 1f});                scaleX.setDuration(2000);                scaleX.setRepeatCount(0);                scaleX.setRepeatMode(ObjectAnimator.RESTART);                scaleX.start();                break;            case R.id.translationY_bt: // 平移动画                ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{10f, 20f, 30f, 40f, 50f, 60f, 50f, 40f, 30f, 20f, 10f});                translationY.setDuration(2000);                translationY.setRepeatCount(0);                translationY.setRepeatMode(ObjectAnimator.RESTART);                translationY.start();                break;            case R.id.AnimatorSet_bt: // 动画集合                AnimatorSet set = new AnimatorSet();                ObjectAnimator or = ObjectAnimator.ofFloat(imageView, "rotationY", new float[]{90f, 180f, 270f, 360f});                or.setDuration(2000);                ObjectAnimator os = ObjectAnimator.ofFloat(imageView, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 5f, 4f, 3f, 2f, 1f});                os.setDuration(2000);                set.playTogether(or,os); // 同时执行所有动画                set.start();                break;        }    }}

简单的实现就这,没了。