Android开发属性动画

时间:2023-03-09 19:50:27
Android开发属性动画

普通动画效果和属性动画效果区别:

普通动画效果的动画播放后只是产生了视觉欺骗,并没有移动真实的控件。

属性动画直接真实的移动控件

AnimationSet动画:

TextView t1 = (TextView)findViewById(R.id.textView6);
TextView t2 = (TextView)findViewById(R.id.textView7); //设置移动
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,-2f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0f
);
//设置透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
//添加进去
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.setDuration(500);
t1.startAnimation(animationSet);
t1.setVisibility(View.GONE);

属性动画:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.foot);

            float curTranslationY = relativeLayout.getTranslationY();
ObjectAnimator animator = ObjectAnimator.ofFloat(relativeLayout, "translationY", curTranslationY, -300f);
animator.setDuration(200);
animator.start();

总的来说属性动画执行的动画效果后控件的位置也会发生改变,解决了很多麻烦。

ObjectAnimator animator = ObjectAnimator.ofFloat(relativeLayout, "translationY", curTranslationY, -300f);

这里的4个参数分别代表的是

1.要进行操作的控件

2.执行的动画效果,我这里的是沿着Y移动 translationX 是沿着X轴移动 rotationX 就是旋转了

至于后面的几个参数就是移动的位置了如果添加第5个参数那么 3 - 5 的参数代表的就是 (从第3个参数移动到第4个参数,然后返回到第5个参数)

ObjectAnimator animator = ObjectAnimator.ofFloat(relativeLayout, "translationY", curTranslationY, -300f,curTranslationY);

上面的代码代表的就是从curTranslationY开始移动 移动到 -300f 然后 返回到 curTranslationY 的位置。

animator.setDuration(200);
animator.start();

上面第一行代码代表整个动画执行的时间,这里是200毫秒。

最后一行代码开始执行动画效果。