android 属性动画之 ObjectAnimator

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sqf251877543/article/details/78053889

一、ObjectAnimator展示单个动画效果

1、translate

    //view从原始位置减速下移500,无限循环
    private void translationY1(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0f, 500f);
        animator.setInterpolator(new DecelerateInterpolator());//减速
        animator.setRepeatCount(-1);//设置动画重复次数,-1表示无限
        animator.setRepeatMode(ValueAnimator.REVERSE);//设置动画循环模式
        animator.setDuration(1000);
        animator.start();
    }
    // view从屏幕顶端下移500
    private void translationY(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"Y",0f,500f);
        animator.setDuration(1000);
        animator.start();
    }

2、scale

    //view从原始尺寸缩小为0
    private void scale_smalller(View view){
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(2000);
        animatorSet.play(animatorX).with(animatorY);//两个动画同时开始
        animatorSet.start();
    }
    //view从原始尺寸变大为原来2倍大
    private void scale_bigger(View view){
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 2f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 2f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(2000);
        animatorSet.play(animatorX).with(animatorY);//两个动画同时开始
        animatorSet.start();
    }
    //view先从原始尺寸缩小为0,再变大为原来2倍大
    private void scaleToSmallToBig(View view){
        ObjectAnimator animatorX1 = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f);
        ObjectAnimator animatorY1 = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f);
        ObjectAnimator animatorX2 = ObjectAnimator.ofFloat(view, "scaleX", 0f, 2f);
        ObjectAnimator animatorY2 = ObjectAnimator.ofFloat(view, "scaleY", 0f, 2f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(3000);
        animatorSet.play(animatorX1).with(animatorY1);
        animatorSet.play(animatorX2).with(animatorY2).after(animatorX1);
        animatorSet.start();
    }

3、alpha

    //view透明度从1变到0.3
    private void changeAlpha(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.3f);
        animator.setDuration(1000);
        animator.start();
    }

4、rotate

    //view顺时针旋转360度
    private void rotate(View view){
        ObjectAnimator animator = ObjectAnimator .ofFloat(view, "rotation", 0f, 360f);
        animator.setDuration(1000);
        animator.start();
    }

二、ObjectAnimator展示多个动画效果

1、按顺序播放动画

    //按顺序播放动画:view先右移500,再下移500,最后旋转360,每个动画耗时0.5s
    private void playAnimatorSequentially(View view){
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationX", 0f, 500f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationY", 0f, 500f);
        ObjectAnimator animator3 = ObjectAnimator .ofFloat(view, "rotation", 0f, 360f);
        AnimatorSet animatorSet = new AnimatorSet() ;
        animatorSet.playSequentially(animator1,animator2,animator3);
        animatorSet.setDuration(500);
        animatorSet.start();
    }

2、若干动画同时进行

     //3个动画同时进行,一共耗时2s
    private void playAnimatorTogether(View view){
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationX", 0f, 500f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationY", 0f, 500f);
        ObjectAnimator animator3 = ObjectAnimator .ofFloat(view, "rotation", 0f, 360f);
        AnimatorSet set =new AnimatorSet();
        set.playTogether(animator1,animator2,animator3);
        set.setDuration(2000);
        set.start();
    }

三、为动画添加监听事件

方式1(四个方法全实现):

    //为动画添加监听事件方式一(四个方法全实现)
    private void addAnimatorListener1(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "Y", 0f, 1000f);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                //动画开始前调用此方法
                Toast.makeText(ObjectAnimatorActivity.this, "start", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                //动画结束后调用此方法
                Toast.makeText(ObjectAnimatorActivity.this, "end", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animator.setDuration(3000);
        animator.start();
    }

方式2(选择实现方法)

    //为动画添加监听事件方式二(选择实现方法)
    private void addAnimatorListener2(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "Y", 0f, 1000f);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                //动画开始前调用此方法
                Toast.makeText(ObjectAnimatorActivity.this, "start", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //动画结束后调用此方法
                Toast.makeText(ObjectAnimatorActivity.this, "end", Toast.LENGTH_SHORT).show();
            }
        });
        animator.setDuration(3000);
        animator.start();
    }

这里贴一个使用ObjectAnimator实现经典弹球菜单demo的代码

public class MainActivity extends Activity implements View.OnClickListener{

    private int[] res = {R.id.imageView_a,R.id.imageView_b,
                         R.id.imageView_c, R.id.imageView_d,
                         R.id.imageView_e,R.id.imageView_f,
                          R.id.imageView_g,R.id.imageView_h};

    private List<ImageView> imageViewList = new ArrayList<ImageView>();

    private boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i = 0;i<res.length;i++){
            ImageView imageView = (ImageView) findViewById(res[i]);
            imageView.setOnClickListener(this);
            imageViewList.add(imageView);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageView_a:
                if (flag){ //第一次点击展开菜单
                    startAnima();
                }else{
                    closeAnima();
                }
                break;
            default:
                Toast.makeText(this, "你点击了"+view.getId(),
                Toast.LENGTH_SHORT).show();
                break;
        }
    }

    //展开菜单
    private void startAnima() {
        for (int i = 1;i<res.length;i++){
            ObjectAnimator animator = 
ObjectAnimator.ofFloat(imageViewList.get(i),
                    "TranslationY",0f,i*200f);
            animator.setDuration(1000);
            //添加一个模仿弹球落地的动画效果
            animator.setInterpolator(new BounceInterpolator());
            //每个延时0.3s依次落下
            animator.setStartDelay(i*300);
            animator.start();
            flag = false;
        }
    }

    //回收菜单
    private void closeAnima() {
        for (int i = 1;i<res.length;i++){
            ObjectAnimator animator =  
  ObjectAnimator.ofFloat(imageViewList.get(i),
                    "TranslationY",i*200f,0f);
            animator.setDuration(500);
            //添加一个模仿弹球落地的动画效果
            animator.setInterpolator(new BounceInterpolator());
            //每个延时0.3s依次回收
            animator.setStartDelay(i*300);
            animator.start();
            flag = true;
        }

    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sqf.objectanimatorsample.MainActivity">

    <ImageView
        android:id="@+id/imageView_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/b"/>
    <ImageView
        android:id="@+id/imageView_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/c"/>
    <ImageView
        android:id="@+id/imageView_d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/d"/>
    <ImageView
        android:id="@+id/imageView_e"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/e"/>
    <ImageView
        android:id="@+id/imageView_f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/f"/>
    <ImageView
        android:id="@+id/imageView_g"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/g"/>
    <ImageView
        android:id="@+id/imageView_h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:src="@drawable/h"/>
    <ImageView
        android:id="@+id/imageView_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a"/>
</FrameLayout>

猜你喜欢

转载自blog.csdn.net/sqf251877543/article/details/78053889