最近在写一个项目,需要用到动画效果,前面一帆风顺,但是做到后面问题就出来了,动画执行后控件的位置并没有改变。
一番百度,才知道普通动画效果的动画播放后只是产生了视觉欺骗,并没有移动真实的控件。
但是这个问题必须解决,否则这个项目就继续不下去。
再百度了一番,找到了属性动画这个东西,它的移动是直接移动控件,这样就解决了控件不能移动的问题了。
废话少说,看看代码。
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毫秒。
最后一行代码就是开始执行动画效果。
本博客用来记录学习之间的问题。
原文:http://www.cnblogs.com/adversary/p/5285562.html