ObjectAnimator和ValueAnimator常用的java实例化方法

关于如何在Java中实例化一个ObjectAnimator和ValueAnimator对象我在前几篇博客中并没有重点说一下,只是稍微提了一下!!在这篇博客中,我将演示关于常使用的animator实例化方法,其中包括ofInt、ofFloat、ofObject以及ofPropertyValuesHolder等!!!

这里面主要涉及到的内容包含:估值器、插值器、PropertyValuesHolder以及KeyFrame等。

还是慢慢来吧,其中估值器和插值器在前面几篇的博客中已经讲过了,在本篇博客中就不在赘述了,如果有不清楚的朋友可以去看一下我之前的博客,谢谢各位的体谅!!!!

其实KeyFrame是用来构造PropertyValuesHolder的一种,我们在说到的时候在仔细讲解,路是一步一步走的,还是来看一下最基本的方法吧!!
先看一下动画的效果图:
ObjectAnimator:
这里写图片描述这里写图片描述这里写图片描述这里写图片描述

ValueAnimator:
这里写图片描述这里写图片描述这里写图片描述这里写图片描述

因为动画控制的view属性都是一样,所以动画看起来比较相似,感觉在View中很少有属性是int类型,大多数的属性类型都是float类型,为了好演示动画,这里选择使用自定义的TextView来做动画,里面加了两个属性来满足我们演示动画的需求!!!

先看一下主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.example.objectandvalueanimator.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <Button
            android:onClick="startObjectOfInt"
            style="@style/custom_button"
            android:text="@string/ofInt" />

        <Button
            android:onClick="startObjectOfFloat"
            style="@style/custom_button"
            android:text="@string/ofFloat" />

        <Button
            android:onClick="startObjectOfObject"
            style="@style/custom_button"
            android:text="@string/ofObject" />

        <Button
            android:onClick="startObjectOfHolder"
            style="@style/custom_button"
            android:text="@string/ofHolder" />
    </LinearLayout>

    <com.example.objectandvalueanimator.CustomView
        android:gravity="center"
        android:id="@+id/iv"
        android:textColor="@color/colorAccent"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">

        <Button
            android:onClick="startValueOfInt"
            style="@style/custom_button"
            android:text="@string/ofInt" />

        <Button
            android:onClick="startValueOfFloat"
            style="@style/custom_button"
            android:text="@string/ofFloat" />

        <Button
            android:onClick="startValueOfObject"
            style="@style/custom_button"
            android:text="@string/ofObject" />

        <Button
            android:onClick="startValueOfHolder"
            style="@style/custom_button"
            android:text="@string/ofHolder" />
    </LinearLayout>
</RelativeLayout>

自定义估值器:

package com.example.objectandvalueanimator;

import android.animation.TypeEvaluator;

/**
 * Created by Administrator on 2017/3/28 0028.
 */

public class CharEvalutor implements TypeEvaluator<Character> {
    @Override
    public Character evaluate(float fraction, Character startValue, Character endValue) {
       char c = (char) (startValue+(endValue-startValue)*fraction);
        return new Character(c);
    }
}

其实这个自定义估值器在前几篇博客已经介绍过了,如果还有什么不太清楚的,可以去看一下我之前的博客,在这就劳烦各位自己查看了!!

自定义View:

package com.example.objectandvalueanimator;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Administrator on 2017/3/28 0028.
 */

public class CustomView extends TextView {
    private Character mTextChar;
    private int mTextRotate;

    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public Character getTextChar() {
        return mTextChar;
    }

    public void setTextChar(Character character) {
        this.setText(character.toString());
    }

    public int getTextRotate() {
        return mTextRotate;
    }

    public void setTextRotate(int rotate) {
        mTextRotate = rotate;
        this.setRotation(rotate);
    }

}

主java文件:

package com.example.objectandvalueanimator;

import android.animation.FloatEvaluator;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ObjectAnimator mObjectAnimator;
    private ValueAnimator mValueAnimator;
    private PropertyValuesHolder mHolder;
    private CustomView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (CustomView) findViewById(R.id.iv);
    }

    public void startObjectOfInt(View view) {
        mObjectAnimator = ObjectAnimator.ofInt(iv, "TextRotate", 0, 90, 180, 90, 0);
        mObjectAnimator.setDuration(5000);
        mObjectAnimator.setInterpolator(new BounceInterpolator());
        mObjectAnimator.start();
    }

    public void startObjectOfFloat(View view) {
        mObjectAnimator = ObjectAnimator.ofFloat(iv, "RotationY", 0, 90, 180, 90, 0);
        mObjectAnimator.setDuration(5000);
        mObjectAnimator.setInterpolator(new BounceInterpolator());
        mObjectAnimator.start();
    }

    public void startObjectOfObject(View view) {
        mObjectAnimator = ObjectAnimator.ofObject(iv, "RotationY", new IntEvaluator(), 0, 90, 180, 90, 0);
        mObjectAnimator.setDuration(5000);
        mObjectAnimator.setInterpolator(new BounceInterpolator());
        mObjectAnimator.start();
    }

    public void startObjectOfHolder(View view) {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofInt("TextRotate", 0, 90, 180, 90, 0);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofObject("TextChar", new CharEvalutor(), 'A', 'Z');
        mObjectAnimator = mObjectAnimator.ofPropertyValuesHolder(iv, holder1, holder2);
        mObjectAnimator.setDuration(5000);
        mObjectAnimator.setInterpolator(new BounceInterpolator());
        mObjectAnimator.start();
    }

    public void startValueOfInt(View view) {
        if(mObjectAnimator != null){
            mObjectAnimator.setTarget(null);
        }
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 90, 180, 90, 0);
        valueAnimator.setDuration(5000);
        valueAnimator.setInterpolator(new CycleInterpolator(2.0f));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                iv.setTextRotate((Integer) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

    public void startValueOfFloat(View view) {
        if(mObjectAnimator != null){
            mObjectAnimator.setTarget(null);
        }
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 90f, 180f, 90f, 0f);
        valueAnimator.setDuration(5000);
        valueAnimator.setInterpolator(new CycleInterpolator(2.0f));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                iv.setTextRotate((int) value);
            }
        });
        valueAnimator.start();
    }

    public void startValueOfObject(View view) {
        if(mObjectAnimator != null){
            mObjectAnimator.setTarget(null);
        }
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new FloatEvaluator(), 0f, 90f, 180f, 90f, 0f);
        valueAnimator.setDuration(5000);
        valueAnimator.setInterpolator(new CycleInterpolator(2.0f));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                iv.setTextRotate((int) value);
            }
        });
        valueAnimator.start();
    }

    public void startValueOfHolder(View view) {
        if(mObjectAnimator != null){
            mObjectAnimator.setTarget(null);
        }
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofInt("TextRotate", 0, 90, 180, 90, 0);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofObject("TextChar", new CharEvalutor(), 'A', 'Z');
        ValueAnimator valueAnimator = ValueAnimator.ofPropertyValuesHolder(holder1, holder2);
        valueAnimator.setDuration(5000);
        valueAnimator.setInterpolator(new CycleInterpolator(2.0f));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int rotate = (int) animation.getAnimatedValue("TextRotate");
                Character c = (Character) animation.getAnimatedValue("TextChar");
                iv.setTextChar(c);
                iv.setTextRotate(rotate);
            }
        });
        valueAnimator.start();
    }
}

从上面的代码上可以看出,在java中使用ObjectAnimator和在xml使用ObjectAnimator的区不大,只是通过使用java代码的方式设置动画的相关属性。在本篇博客中,感觉最值得提一下的就是PropertyValuesHolder,如果你在使用属性动画的时候,想同一时刻控制View的多种属性但是又不想使用动画集,那么使用PropertyValuesHolder将是一个不错的选择,具体的还是看一下代码吧!!

PropertyValuesHolder holder1 = PropertyValuesHolder.ofInt("TextRotate", 0, 90, 180, 90, 0);
PropertyValuesHolder holder2 = PropertyValuesHolder.ofObject("TextChar", new CharEvalutor(), 'A', 'Z');
mObjectAnimator = mObjectAnimator.ofPropertyValuesHolder(iv, holder1, holder2);

look,每个PropertyValuesHolder代表对一个属性的控制,使用多个PropertyValuesHolder可以同时控制View的多个属性,感觉这个方法很用用处,另外,我们在做Layout动画的时候也需要使用PropertyValuesHolder等,其实PropertyValuesHolder的实例化方法除了ofInt、ofFloat和ofObject之外,还有一个ofKeyFrame方法!!这个实例化的方法在本篇博客没有使用,在这里给出以下吧!!!

        Keyframe keyframe1 = Keyframe.ofFloat(0.0f,0f);
        Keyframe keyframe2 = Keyframe.ofFloat(1.0f,180f);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofKeyframe("TextRotate",keyframe1,keyframe2);
        Keyframe keyframe3 = Keyframe.ofObject(0.0f,'A');
        Keyframe keyframe4 = Keyframe.ofObject(1.0f,'Z');
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofKeyframe("TextChar",keyframe3,keyframe4);
        holder4.setEvaluator(new CharEvalutor());
        ValueAnimator valueAnimator = ValueAnimator.ofPropertyValuesHolder(holder3, holder4);
        valueAnimator.setDuration(5000);
        valueAnimator.setInterpolator(new CycleInterpolator(2.0f));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float rotate = (float) animation.getAnimatedValue("TextRotate");
                Character c = (Character) animation.getAnimatedValue("TextChar");
                iv.setTextChar(c);
                iv.setTextRotate((int) rotate);
            }
        });
        valueAnimator.start();

在属性动画的时候,如果你使用的ofObject方法,不论是在ObjectAnimator、ValueAnimator、PropertyValuesHolder还是KeyFrame中,你都需要指定相应类型的估值器,不然你就等着抛异常吧!!感觉苦逼的编程都是从各种自定义开始的,好了,不提了!!keyFrame直译过来为关键帧,它可以指定特定的时候动画对应的value值,像我上面那样只是指定了动画开始和动画结束时的动画value值,在使用的时候也可以指定其他时刻,比如动画一半的时候对应的动画值,那么里面的参数就需要传入(0.5,xxxx)!!!

好了,关于本篇博客对动画的介绍就到这里,谢谢大家的支持和关注!!!

这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!

这里写图片描述

代码地址:
https://github.com/zhuyuqiang2017/Animation

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值