在我们的安卓开发中,加入动画是可以给用户很好的体验,今天就简单的来了解一下动画 ,动画效果有淡入淡出、缩放、平移、旋转,还有组合动画
首先在xml文件中先写一个TextVeiw,接下来就是以TextView为例展示一下这些效果
在MainActivity中获取一下控件
开始,首先展示一下淡入淡出效果(字体逐渐变淡,然诺逐渐还原)
ObjectAnimator animator1 = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f, 1f);
animator1.setDuration(5000);
animator1.start();
旋转效果(旋转360°)
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
animator.setDuration(5000);
animator.start();
平移
float curTranslationX = textView.getTranslationX();
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", curTranslationX, -500f, curTranslationX);
animator.setDuration(5000);
animator.start();
缩放(垂直方向上进行缩放)
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "scaleY", 1f, 3f, 1f);
animator.setDuration(5000);
animator.start();
组合(让TextView先从屏幕外移动进屏幕,然后开始旋转360度,旋转的同时进行淡入淡出操作)
ObjectAnimator moveIn = ObjectAnimator.ofFloat(textView, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textView, "alpha", 1f, 0f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(rotate).with(fadeInOut).after(moveIn);
animSet.setDuration(5000);
animSet.start();