本篇博客主要讲述了如何使用ObjectAnimator来完成旋转、移动、缩放和透明度动画。其实ObjectAnimator可以完成很多属性动画,这里就不一一讲解了,在使用的过程中,你会发现其实UI跟动画的实现有点分离,整体实现用一句话来说就是,UI根据Animator的value值不断更新自己的属性。ObjectAnimator和视图动画一样,既可以在xml中声明也可以在java代码中声明。好了,废话不多说,直接看效果图吧:
这里所有的动画都是使用属性动画写的,属性动画在xml文件中定义,通过AnimatorInflater解析获取xml中的动画。具体的实现可以看一下代码:
scale_x_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="scaleX"
android:valueFrom="1.0f"
android:valueTo="4.0f"
android:valueType="floatType">
</objectAnimator>
scale_y_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="scaleY"
android:valueFrom="1.0f"
android:valueTo="4.0f"
android:valueType="floatType">
</objectAnimator>
由于View没有直接设置Sale的方法,只能通过设置scaleX和scaleY属性,在下一篇中的ValueAnimator中,我们可以在回调事件中同时设置scaleX和scaleY。在这里我们只能分开设置了,如果想一起改变可以放在动画集里,使用set标签即可。下面会继续说道了!!
rotate_z_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="rotationX"
android:valueFrom="0"
android:valueTo="720"
android:valueType="floatType">
</objectAnimator>
rotate_y_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="720"
android:valueType="floatType">
</objectAnimator>
在objectAnimator的动画中,valueType和propertyName的设置一定要正确,否则动画是不会其效果的,因为系统根据你提供的参数类型和属性名称找不到相应的属性设置方法,当然不会有效果呀,没给你抛异常就算是对的起你的了!!!
alpha_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="alpha"
android:valueFrom="1.0"
android:valueTo="0.3"
android:valueType="floatType">
</objectAnimator>
transilation_x_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="translationX"
android:valueFrom="-200dp"
android:valueTo="240dp"
android:valueType="floatType">
</objectAnimator>
transilation_y_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="translationY"
android:valueFrom="12dp"
android:valueTo="240dp"
android:valueType="floatType">
</objectAnimator>
set_animator文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<set android:ordering="together">
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="rotationX"
android:valueFrom="0"
android:valueTo="720"
android:valueType="floatType"/>
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="720"
android:valueType="floatType"/>
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="720"
android:valueType="floatType"/>
</set>
<set android:ordering="together">
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="scaleX"
android:valueFrom="1.0f"
android:valueTo="4.0f"
android:valueType="floatType"/>
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="scaleY"
android:valueFrom="1.0f"
android:valueTo="4.0f"
android:valueType="floatType"/>
</set>
<set android:ordering="together">
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="translationX"
android:valueFrom="-100dp"
android:valueTo="100dp"
android:valueType="floatType"/>
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="translationY"
android:valueFrom="-100dp"
android:valueTo="100dp"
android:valueType="floatType"/>
</set>
<objectAnimator
android:duration="6000"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="alpha"
android:valueFrom="1.0"
android:valueTo="0.3"
android:valueType="floatType"/>
</set>
在不清楚属性名称和属性值类型的时候可以去找一下对应view的源码,如果本类没有的话,就去他的父类中找,相信只要功夫深肯定找得到的,实在不行自定义一个View,里面的属性还不是由你说了算,哈哈哈~~
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:weightSum="2">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
<Button
style="@style/custom_button"
android:onClick="startScaleXAnimator"
android:text="@string/start_scale_x" />
<Button
style="@style/custom_button"
android:onClick="startScaleYAnimator"
android:text="@string/start_scale_y" />
<Button
style="@style/custom_button"
android:onClick="startAlphaAnimator"
android:text="@string/start_alpha" />
<Button
style="@style/custom_button"
android:onClick="startRotateAnimator"
android:text="@string/start_rotate" />
<Button
style="@style/custom_button"
android:onClick="startRotateXAnimator"
android:text="@string/start_rotate_x" />
<Button
style="@style/custom_button"
android:onClick="startRotateYAnimator"
android:text="@string/start_rotate_y" />
<Button
style="@style/custom_button"
android:onClick="startTranslationXAnimator"
android:text="@string/start_translation_x" />
<Button
style="@style/custom_button"
android:onClick="startTranslationYAnimator"
android:text="@string/start_translation_y" />
</GridLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/custom_button"
android:text="@string/app_name"
android:onClick="startSetAnimator"/>
</LinearLayout>
</LinearLayout>
Java文件:
package com.example.objectanimatordemo;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ObjectAnimator scaleXAnimator, scaleYAnimator, rotateAnimator, alphaAnimator,rotateXAnimator;
private ObjectAnimator rotateYAnimator,translationXAnimator,translationYAnimator;
private AnimatorSet animatorSet;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
initAnimator();
}
public void initAnimator(){
scaleXAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.scale_x_animator);
scaleYAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.scale_y_animator);
rotateAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.rotate_z_animator);
alphaAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.alpha_animator);
rotateXAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.rotate_x_animator);
rotateYAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.rotate_y_animator);
translationXAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.transilation_x_animator);
translationYAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.transilation_y_animator);
animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this,R.animator.set_animator);
}
public void startScaleXAnimator(View view) {
scaleXAnimator.setTarget(iv);
scaleXAnimator.start();
}
public void startScaleYAnimator(View view) {
scaleYAnimator.setTarget(iv);
scaleYAnimator.start();
}
public void startAlphaAnimator(View view) {
alphaAnimator.setTarget(iv);
alphaAnimator.start();
}
public void startRotateAnimator(View view) {
rotateAnimator.setTarget(iv);
rotateAnimator.start();
}
public void startRotateXAnimator(View view) {
rotateXAnimator.setTarget(iv);
rotateXAnimator.start();
}
public void startRotateYAnimator(View view) {
rotateYAnimator.setTarget(iv);
rotateYAnimator.start();
}
public void startTranslationXAnimator(View view) {
translationXAnimator.setTarget(iv);
translationXAnimator.start();
}
public void startTranslationYAnimator(View view) {
translationYAnimator.setTarget(iv);
translationYAnimator.start();
}
public void startSetAnimator(View view){
animatorSet.setTarget(iv);
animatorSet.start();
}
}
看到了吧,代码的实现过程很简单,相信你这么聪明肯定是一看就会的那种。好了关于objectAnimator的内容就讲到这里了,欢迎大家继续关注!!!
这是我的微信公众号,如果可以的话,希望您可以帮忙关注一下,这将是对我最大的鼓励了,谢谢!!