-
多个动画一起执行 多个动画顺序执行
-
@param view
*/
public void playWithAfter(View view) {
float cx = mBall.getX(); //获取该mBall的X坐标
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBall, “alpha”, 1.0f,0.5f); //从1.0不透明到0.5半透明
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBall, “scaleX”, 1.0f,2.0f); //X比例 变到2倍
ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBall, “scaleY”, 1.0f,2.0f);
ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBall, “x”, cx,0f); //x 从当前位置到0
ObjectAnimator anim5 = ObjectAnimator.ofFloat(mBall, “x”, cx); //x 位置到cx 恢复初始位置
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(anim1).with(anim2); //anim1-anim4 一起执行
animatorSet.play(anim2).with(anim3);
animatorSet.play(anim3).with(anim4);
animatorSet.play(anim5).after(anim4); //anim5在anim4执行之后才执行
animatorSet.setDuration(1000);
animatorSet.start(); //开始执行
}
1.4 如何使用xml文件来创建属性动画
大家肯定都清楚,View Animator 、Drawable Animator都可以在anim文件夹下创建动画,然后在程序中使用,甚至在Theme中设置为属性值。当然了,属性动画其实也可以在文件中声明:
1.首先在res下建立animator文件夹,然后建立res/animator/scalex.xml
<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android”
android:duration=“1000”
android:propertyName=“scaleX”
android:valueFrom=“1.0”
android:valueTo=“2.0”
android:valueType=“floatType” >
2.如果需要一起执行的动画(或者是顺序执行的动画),则需要在res/animator/
下建立一个xml文件
<set xmlns:android=“http://schemas.android.com/apk/res/android”
android:ordering=“together” >
<objectAnimator
android:duration=“1000”
android:propertyName=“scaleX”
android:valueFrom=“1.0”
android:valueTo=“2.0”
android:valueType=“floatType” >
<objectAnimator
android:duration=“1000”
android:propertyName=“scaleY”
android:valueFrom=“1.0”
android:valueTo=“2.0”
android:valueType=“floatType” >
3.在java代码中的调用
/**
-
单独执行一个X的动画
-
@param view
*/
public void scaleX(View view)
{
// 加载动画
//使用AnimatorInflater加载动画的资源文件,然后设置目标,就ok~~是不是很简单,这只是单纯横向的放大一倍~
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex);
anim.setTarget(mBall); //目标
anim.start(); //开始执行动画
}
/**
-
一起执行的动画
-
@param view