Android滑动动画效果Demo_TranslateAnimation使用例子
2014-09-04 10:39:25 By: shinyuu
TranslateAnimation简介
Android JDK为我们提供了4种动画效果,分别是: AlphaAnimation,RotateAnimation, ScaleAnimation, TranslateAnimation
今天我想讲解的是TranslateAnimation这个动画效果。也是本人在做一个移动图片的动画效果的项目时,遇到了一些问题
在网上查了很多资料,搞了好几天。终于明白怎么使用这个TranslateAnimation,在本文中记录下来,以便以后忘记了可以查阅
TranslateAnimation是移动的动画效果、它有三个构造函数
public TranslateAnimation(Context context,AttributeSet attrs)
这个方法比较简单、这里就不做过多讲解了
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
这个是我们最常用的一个构造方法
float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;
float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值;
float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值;
float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值;
如果view在A(x,y)点 那么动画就是从B点(x+fromXDelta, y+fromYDelta)点移动到C 点(x+toXDelta,y+toYDelta)点.
public TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
fromXType:第一个参数是x轴方向的值的参照(Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF,or Animation.RELATIVE_TO_PARENT);fromXValue:第二个参数是第一个参数类型的起始值;
toXType,toXValue:第三个参数与第四个参数是x轴方向的终点参照与对应值;
后面四个参数就不用解释了、如果全部选择Animation.ABSOLUTE,其实就是第二个构造函数
以x轴为例介绍参照与对应值的关系:
如果选择参照为Animation.ABSOLUTE,那么对应的值应该是具体的坐标值,比如100到300,指绝对的屏幕像素单位
如果选择参照为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相对于自身或父控件
对应值应该理解为相对于自身或者父控件的几倍或百分之多少
使用事例
//定义从右侧进入的动画效果 protected Animation inFromRightAnimation() { Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromRight.setDuration(500); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } //定义从左侧进入的动画效果 protected Animation inFromLeftAnimation() { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromLeft.setDuration(500); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } //定义从左侧退出的动画效果 protected Animation outToLeftAnimation() { Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outtoLeft.setDuration(500); outtoLeft.setInterpolator(new AccelerateInterpolator()); return outtoLeft; } // 定义从左侧进入的动画效果 protected Animation inFromLeftAnimation2() { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.7f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); inFromLeft.setDuration(500); inFromLeft.setFillAfter(true); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } //定义从左侧退出的动画效果 protected Animation outToLeftAnimation2() { Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); outtoLeft.setDuration(500); outtoLeft.setFillAfter(true); outtoLeft.setInterpolator(new AccelerateInterpolator()); return outtoLeft; }
若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力
想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)
或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)
如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教
为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)
感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛
征文服务评论 2017-01-24 20:46:19 1 评 | 回复
感谢楼主的分享