Android属性动画-简单实例
1.ValueAnimator
//在2000毫秒内,将值从0过渡到1的动画 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); anim.setDuration(2000); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { float currentValue = (float) valueAnimator.getAnimatedValue(); Log.e("tag", "currentValue="+currentValue); String textStr = ((int) (currentValue * 60)) + ""; text1_tv.setText(textStr); } }); anim.start();
2.ObjectAnimator
//将TextView从常规变换成全透明,再从全透明变换成常规 ObjectAnimator anim1 = ObjectAnimator.ofFloat(text2_tv, "alpha", 1f, 0f, 1f); anim1.setDuration(3000); anim1.start(); //将TextView进行一次360度的旋转 ObjectAnimator anim2 = ObjectAnimator.ofFloat(text2_tv, "rotation", 0f, 360f); anim2.setDuration(3000); anim2.start(); //将TextView先向左移出屏幕,然后再移动回来 float curTranslationX = text2_tv.getTranslationX(); ObjectAnimator anim3 = ObjectAnimator.ofFloat(text2_tv, "translationX", curTranslationX, -500f, curTranslationX); anim3.setDuration(3000); anim3.start(); //将TextView在垂直方向上放大3倍再还原 ObjectAnimator anim4 = ObjectAnimator.ofFloat(text2_tv, "scaleY", 1f, 3f, 1f); anim4.setDuration(5000); anim4.start();
3.组合动画
//让TextView先从屏幕外移动进屏幕,然后开始旋转360度,旋转的同时进行淡入淡出操作 ObjectAnimator moveIn = ObjectAnimator.ofFloat(text3_tv, "translationX", -500f, 0f); ObjectAnimator rotate = ObjectAnimator.ofFloat(text3_tv, "rotation", 0f, 360f); ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(text3_tv, "alpha", 1f, 0f, 1f); AnimatorSet animSet = new AnimatorSet(); animSet.play(rotate).with(fadeInOut).after(moveIn); animSet.setDuration(5000); animSet.start();