Demo Minamoto码
Property animation (Property Animation)
- Category: transparent animation, rotation animation, displacement animation, zoom animation, a combination of animation
- Writing: code dynamically set properties, use xml file attributes defined in two ways
And tween different points of animation:
- Property animation can change the position of the controls
- Rotation, displacement, zooming animation can only specify a direction (X-axis, Y-axis)
If you need to use XML define tween
First created in the resource path res directory: animator (name can not be wrong)
Second, create xml file in the res / animator directory, the root node must be:
Transparent animation
propertyName:alpha
Code Example 1. (code set properties)
//不透明-完全透明-半透明(会停留在最后的设置效果上)
ObjectAnimator alpha = ObjectAnimator.ofFloat(ivView, "alpha", 1, 0, 0.5f);
alpha.setDuration(2000);
alpha.start();
//上下代码等效
// ObjectAnimator oa = new ObjectAnimator();
// oa.setTarget(ivView);
// oa.setPropertyName("alpha");
// oa.setFloatValues(1, 0, 0.5f);
// oa.setDuration(2000);
// oa.start();
Code Example 2. (set attributes in XML)
- R.animator.animator_my_alpha
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="alpha"
android:repeatCount="1"
android:valueFrom="1"
android:valueTo="0.5f"
android:repeatMode="reverse">
</objectAnimator>
<!-- 外层可以使用 animator 包裹-->
<!-- 外层也可以不使用 animator 包裹-->
<!--<animator xmlns:android="http://schemas.android.com/apk/res/android">-->
<!--<objectAnimator-->
<!--android:duration="2000"-->
<!--android:propertyName="alpha"-->
<!--android:repeatCount="1"-->
<!--android:valueFrom="1"-->
<!--android:valueTo="0.5f"-->
<!--android:repeatMode="reverse">-->
<!--</objectAnimator>-->
<!--</animator>-->
- Call XML
ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_alpha);
animator.setTarget(ivView);
animator.start();
Rotation animation
propertyName: rotation (about the center of rotation), rotationX (rotation around the X axis), rotationY (rotation around the Y axis)
Code Example 1. (code set properties)
ObjectAnimator rotationX = ObjectAnimator.ofFloat(ivView, "rotation", 0, 180, 90, 360);
rotationX.setDuration(2000);
rotationX.start();
Code Example 2. (set attributes in XML)
- R.animator.animator_my_rotation
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="rotation"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="180">
</objectAnimator>
- Call XML
ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_rotation);
animator.setTarget(ivView);
animator.start();
Displacement animation
propertyName: translationX (X-axis direction), translationY (Y axis direction)
Code Example 1. (code set properties)
ObjectAnimator translationY = ObjectAnimator.ofFloat(ivView, "translationY", 0, 100, 0, 100);
translationY.setDuration(2000);
translationY.start();
Code Example 2. (set attributes in XML)
- R.animator.animator_my_translation_y
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="translationX"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="200">
</objectAnimator>
- Call XML
ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_translation_y);
animator.setTarget(ivView);
animator.start();
Zoom animation
propertyName: the scaleX (X-axis direction), scaleY (Y axis direction)
Code Example 1. (code set properties)
ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivView, "scaleY", 1, 0.5f, 1, 0.5f);
scaleY.setDuration(2000);
scaleY.start();
Code Example 2. (set attributes in XML)
- R.animator.animator_my_scale_y
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="scaleY"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueTo="2"
android:valueFrom="1">
</objectAnimator>
- Call XML
ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_scale_y);
animator.setTarget(ivView);
animator.start();
A combination of animation
playSequentially: sequentially performing all animations
playTogether: performed along all animations
1. Code Example (code set properties)
AnimatorSet set = new AnimatorSet();
ObjectAnimator alpha = ObjectAnimator.ofFloat(ivView, "alpha", 1, 0.5f, 1);
ObjectAnimator rotation = ObjectAnimator.ofFloat(ivView, "rotation", 0, 180, 90, 360);
ObjectAnimator translationY = ObjectAnimator.ofFloat(ivView, "translationY", 0, 100, 0, 100);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivView, "scaleY", 1, 2, 1, 0.5f);
set.setDuration(3000);
//playSequentially:依次执行所有的动画
set.playSequentially(alpha, rotation, translationY, scaleY);
//playTogether:把所有的动画一起执行
//set.playTogether(alpha, rotationX, translationY, scaleY);
set.start(); //切记:要start
Code Example 2. (set attributes in XML)
- R.animator.animator_my_set1
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="200" />
<objectAnimator
android:duration="2000"
android:propertyName="translationY"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="200" />
</set>
- R.animator.animator_my_set2
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<set android:ordering="sequentially">
<objectAnimator
android:duration="1000"
android:propertyName="translationX"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="200"/>
<objectAnimator
android:duration="1000"
android:propertyName="translationY"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="200"/>
</set>
<objectAnimator
android:duration="2000"
android:propertyName="rotationX"
android:repeatCount="2"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="180"/>
</set>
- Call XML
//AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animator_my_set1);
AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animator_my_set2);
animator.setTarget(ivView);
animator.start();