属性动画可以实现补间动画的所有效果,且还能实现补间动画实现不了的效果,接下来就来学习一下属性动画。
二、ValueAnimator的使用 //创建一个从1到100的整型动画 //获取屏幕宽度 int wIDthPixels = getResources().getdisplayMetrics().wIDthPixels;// ; //创建一个从1到100的整型动画 ValueAnimator valueAnimator = ValueAnimator.ofInt(- (int) mTvHello.getPaint().measureText(getResources().getString(R.string.app_name)),wIDthPixels); //给动画设置一个线性的插值器 valueAnimator.setInterpolator(new linearInterpolator()); //设置动画的执行时长 valueAnimator.setDuration(4000);// 设置动画的重复次数 valueAnimator.setRepeatCount(ValueAnimator.INFINITE); //设置动画的重复模式,REVERSE表示逆向重复,RESTART表示连续重复 valueAnimator.setRepeatMode(ValueAnimator.RESTART); //给动画设置一个估值器 valueAnimator.setEvaluator(new IntEvaluator()); //设置动画的当前时间,取值范围为0~duration// valueAnimator.setCurrentPlayTime(1000); //设置动画的时间因子// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP_MR1) {// valueAnimator.setCurrentFraction(0.5f);// } //给动画设置启动延迟,例如当前就是延迟1s启动动画 valueAnimator.setStartDelay(1000); //给动画设置当前动态监听 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedFraction = animation.getAnimatedFraction(); Integer integer = (Integer) animation.getAnimatedValue(); mTvHello.setTranslationX(integer.floatValue()); Log.d(TAG, "onAnimationUpdate: integer:" + integer + " animatedFraction" + animatedFraction); } }); //给动画设置状态监听 valueAnimator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { //动画开始 Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { //动画结束 Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { //取消动画 Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { //重复动画 Log.d(TAG, "onAnimationRepeat: "); } }); //启动动画 valueAnimator.start();
通过代码可以看到,ValueAnimator并不能直接让某个控件平移、缩放、渐变、旋转,而是它能在一段时间内动态的计算出一些值,要实现动画效果,那么可以将控件的属性设置为该值,当这些属性连续变化时,就能出现动画效果。以上代码的动画效果图如下:
ObjectAnimator animator = ObjectAnimator.offloat(mTvHello, "Alpha", 1.0f, 0f); animator.setDuration(3000); animator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { Log.d(TAG, "onAnimationRepeat: "); } }); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedValue = (float) animation.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: "+animatedValue); } }); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.start();
缩放 ObjectAnimator animator = ObjectAnimator.offloat(mTvHello, "ScaleX", 1.0f, 0f); animator.setDuration(3000); animator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { Log.d(TAG, "onAnimationRepeat: "); } }); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedValue = (float) animation.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: "+animatedValue); } }); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.start();
ObjectAnimator animator = ObjectAnimator.offloat(mTvHello, "ScaleY", 1.0f, 0f); animator.setDuration(3000); animator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { Log.d(TAG, "onAnimationRepeat: "); } }); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedValue = (float) animation.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: "+animatedValue); } }); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.start();
平移ObjectAnimator animator = ObjectAnimator.offloat(mTvHello, "TranslationX", 0.0f, getResources().getdisplayMetrics().wIDthPixels/2); animator.setDuration(3000); animator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { Log.d(TAG, "onAnimationRepeat: "); } }); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedValue = (float) animation.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: "+animatedValue); } }); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.start();
ObjectAnimator animator = ObjectAnimator.offloat(mTvHello, "TranslationY", 0.0f, getResources().getdisplayMetrics().wIDthPixels/2); animator.setDuration(3000); animator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { Log.d(TAG, "onAnimationRepeat: "); } }); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedValue = (float) animation.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: "+animatedValue); } }); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.start();
旋转 ObjectAnimator animator = ObjectAnimator.offloat(mTvHello, "Rotation", 0.0f, 360f); animator.setDuration(3000); animator.setInterpolator(new linearInterpolator()); animator.addListener(new Animator.AnimatorListener() { @OverrIDe public voID onAnimationStart(Animator animation) { Log.d(TAG, "onAnimationStart: "); } @OverrIDe public voID onAnimationEnd(Animator animation) { Log.d(TAG, "onAnimationEnd: "); } @OverrIDe public voID onAnimationCancel(Animator animation) { Log.d(TAG, "onAnimationCancel: "); } @OverrIDe public voID onAnimationRepeat(Animator animation) { Log.d(TAG, "onAnimationRepeat: "); } }); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float animatedValue = (float) animation.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: "+animatedValue); } }); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.RESTART); animator.start();
总结 以上是内存溢出为你收集整理的Android动画之属性动画全部内容,希望文章能够帮你解决Android动画之属性动画所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)