**************************************XML配置方式**********************************
public void scaleX(View view) { // 加载动画xml文件 Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex); anim.setTarget(mMv); anim.start(); } public void scaleXandScaleY(View view) { // 加载动画 Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scale); anim.setTarget(mMv); anim.start(); }
<?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" > </objectAnimator>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together" > <objectAnimator android:duration="1000" android:propertyName="scaleX" android:valueFrom="1" android:valueTo="0.5" > </objectAnimator> <objectAnimator android:duration="1000" android:propertyName="scaleY" android:valueFrom="1" android:valueTo="0.5" > </objectAnimator> </set>
配置文件就一张图片
public void rotateyAnimRun(final View view) { ObjectAnimator anim = ObjectAnimator// .ofFloat(view, "zhy", 1.0F, 0.2F)// .setDuration(500);// anim.addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { float cVal = (Float) animation.getAnimatedValue(); view.setAlpha(cVal); view.setScaleX(cVal); view.setScaleY(cVal); } }); anim.start(); }
public void togetherRun(View view) { ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX", 1.0f, 2f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY", 1.0f, 2f); AnimatorSet animSet = new AnimatorSet(); animSet.setDuration(2000); // 已均匀的速率改变(定义动画播放的速度的) animSet.setInterpolator(new LinearInterpolator()); // 两个动画同时执行 animSet.playTogether(anim1, anim2); animSet.start(); } public void playWithAfter(View view) { float cx = mBlueBall.getX(); ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX", 1.0f, 2f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY", 1.0f, 2f); ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall, "x", cx, 0f); ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall, "x", 0f,cx); /** * anim1,anim2,anim3同时执行 anim4接着执行 */ AnimatorSet animSet = new AnimatorSet(); animSet.play(anim1).with(anim2); animSet.play(anim2).with(anim3); animSet.play(anim4).after(anim3); animSet.setDuration(1000); animSet.start(); }
public void viewAnim(View view) { //可以返回一个ViewPropertyAnimator实例,作用是用来控制view本身的动画属性。 mBlueBall.animate().alpha(0).y(mScreenHeight / 2).setDuration(1000) .withStartAction(new Runnable() { public void run() { Log.e(TAG, "START"); } }).withEndAction(new Runnable() { public void run() { Log.e(TAG, "END"); runOnUiThread(new Runnable() { public void run() { mBlueBall.setY(0); mBlueBall.setAlpha(1.0f); } }); } }).start(); } public void propertyValuesHolder(View view) { PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f); PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f); ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ) .setDuration(1000).start(); }
/** * 自由落体 */ public void verticalRun(View view) { ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight - mBlueBall.getHeight()); animator.setTarget(mBlueBall); animator.setDuration(1000).start(); //更新动画 animator.addUpdateListener(new AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { mBlueBall.setTranslationY((Float) animation.getAnimatedValue()); } }); } public void fadeOut(View view) { ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f); anim.addListener(new AnimatorListener() { public void onAnimationStart(Animator animation) { } public void onAnimationRepeat(Animator animation) { // TODO Auto-generated method stub } public void onAnimationEnd(Animator animation) { ViewGroup parent = (ViewGroup) mBlueBall.getParent(); if (parent != null) parent.removeView(mBlueBall); } public void onAnimationCancel(Animator animation) { // TODO Auto-generated method stub } }); anim.start(); }