TranslateAnimation动画
16lz
2021-01-26
众所周知,TranslateAnimation是android中重要的一个动画函数,很多时候我们
都需要使用它来实现更好的UI效果,今天就简单研究下这个TranslateAnimation。
TranslateAnimation这个位移动画主要有三个构造函数,对应着三种不同的参数
形式,接下来使用源码简单分析,个人见解而已。
(1)第一种构造函数TranslateAnimation(Context context, AttributeSet attrs)
通过源码我们可以看出,这个构造函数,主要是当你从一个资源文件中获取一个动画资源时
进行调用,构造函数会自动帮你解析其中相应的参数,然后赋值给mFromXType,mFromXValue
...等8个参数。其实它就相当于接下来将要介绍的第三个构造函数,只不过这个构造函数
传入的是context和资源文件对你的动画属性的封装参数attrs。而第二个构造也是第三个
构造函数的一种特殊情况。
(2)第二种构造函数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坐标上的差值;
也就是说你的TranslateAnimation动画执行的整个过程,需要你指定它的起始位置,而这个起始位置的确定要
依靠你原来的View的位置为参照,如果你View的位置为(x,y),则动画开始的位置坐标为(x+fromXDelta, y+fromYDelta)
它的结束位置为(x+toXDelta,y+toYDelta)。再看源码你会发现构造函数又设置了四个参数为默认的ABSOLUTE,这个参数
表示绝对像素,在接下里的最后一个构造函数中可以由你来自己选定,而这个函数也其实就是下一个构造函数的一种
X方向或者Y方向上的Type选定为Animation.ABSOLUTE的特殊情况。
(3)第三种构造函数TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)
在这个构造函数中共有8个参数
Animation.ABSOLUTE是绝对位移量,也就是上面的第二种构造函数的情况,当使用这个参数时,这两种构造函数并无差别
Animation.RELATIVE_TO_SELF
Animation.RELATIVE_TO_PARENT这两个参数是相对位移量,也就是说相对于这个View自己,或者是相对于这个控件的父控件
的。当使用这两个Type时,相应的fromXValue,toXValue,fromYValue,toYValue的值是double型的数据,可正也可负。
例如当Type为RELATIVE_TO_SELF,且fromXValue为+1.0时,也就代表着你的动画的初始位置x坐标在原来视图X坐标位置的正方向偏移一个自身宽度。而当值为0时
就意味着你这个View的x轴位置相对于你自身原来X轴位置不变。
都需要使用它来实现更好的UI效果,今天就简单研究下这个TranslateAnimation。
TranslateAnimation这个位移动画主要有三个构造函数,对应着三种不同的参数
形式,接下来使用源码简单分析,个人见解而已。
(1)第一种构造函数TranslateAnimation(Context context, AttributeSet attrs)
/** * Constructor used when a TranslateAnimation is loaded from a resource. * * @param context Application context to use * @param attrs Attribute set from which to read values */ public TranslateAnimation(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TranslateAnimation); Description d = Description.parseValue(a.peekValue( com.android.internal.R.styleable.TranslateAnimation_fromXDelta)); mFromXType = d.type; mFromXValue = d.value; d = Description.parseValue(a.peekValue( com.android.internal.R.styleable.TranslateAnimation_toXDelta)); mToXType = d.type; mToXValue = d.value; d = Description.parseValue(a.peekValue( com.android.internal.R.styleable.TranslateAnimation_fromYDelta)); mFromYType = d.type; mFromYValue = d.value; d = Description.parseValue(a.peekValue( com.android.internal.R.styleable.TranslateAnimation_toYDelta)); mToYType = d.type; mToYValue = d.value; a.recycle(); }
通过源码我们可以看出,这个构造函数,主要是当你从一个资源文件中获取一个动画资源时
进行调用,构造函数会自动帮你解析其中相应的参数,然后赋值给mFromXType,mFromXValue
...等8个参数。其实它就相当于接下来将要介绍的第三个构造函数,只不过这个构造函数
传入的是context和资源文件对你的动画属性的封装参数attrs。而第二个构造也是第三个
构造函数的一种特殊情况。
(2)第二种构造函数TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
/** * Constructor to use when building a TranslateAnimation from code * * @param fromXDelta Change in X coordinate to apply at the start of the * animation * @param toXDelta Change in X coordinate to apply at the end of the * animation * @param fromYDelta Change in Y coordinate to apply at the start of the * animation * @param toYDelta Change in Y coordinate to apply at the end of the * animation */ public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) { mFromXValue = fromXDelta; mToXValue = toXDelta; mFromYValue = fromYDelta; mToYValue = toYDelta; mFromXType = ABSOLUTE; mToXType = ABSOLUTE; mFromYType = ABSOLUTE; mToYType = ABSOLUTE; }
这个构造函数的参数
float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;
float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值;
float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值;
float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值;
也就是说你的TranslateAnimation动画执行的整个过程,需要你指定它的起始位置,而这个起始位置的确定要
依靠你原来的View的位置为参照,如果你View的位置为(x,y),则动画开始的位置坐标为(x+fromXDelta, y+fromYDelta)
它的结束位置为(x+toXDelta,y+toYDelta)。再看源码你会发现构造函数又设置了四个参数为默认的ABSOLUTE,这个参数
表示绝对像素,在接下里的最后一个构造函数中可以由你来自己选定,而这个函数也其实就是下一个构造函数的一种
X方向或者Y方向上的Type选定为Animation.ABSOLUTE的特殊情况。
(3)第三种构造函数TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)
/** * Constructor to use when building a TranslateAnimation from code * * @param fromXType Specifies how fromXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param fromXValue Change in X coordinate to apply at the start of the * animation. This value can either be an absolute number if fromXType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param toXType Specifies how toXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param toXValue Change in X coordinate to apply at the end of the * animation. This value can either be an absolute number if toXType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param fromYType Specifies how fromYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param fromYValue Change in Y coordinate to apply at the start of the * animation. This value can either be an absolute number if fromYType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. * @param toYType Specifies how toYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param toYValue Change in Y coordinate to apply at the end of the * animation. This value can either be an absolute number if toYType * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise. */ public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) { mFromXValue = fromXValue; mToXValue = toXValue; mFromYValue = fromYValue; mToYValue = toYValue; mFromXType = fromXType; mToXType = toXType; mFromYType = fromYType; mToYType = toYType; }
在这个构造函数中共有8个参数
Animation.ABSOLUTE是绝对位移量,也就是上面的第二种构造函数的情况,当使用这个参数时,这两种构造函数并无差别
Animation.RELATIVE_TO_SELF
Animation.RELATIVE_TO_PARENT这两个参数是相对位移量,也就是说相对于这个View自己,或者是相对于这个控件的父控件
的。当使用这两个Type时,相应的fromXValue,toXValue,fromYValue,toYValue的值是double型的数据,可正也可负。
例如当Type为RELATIVE_TO_SELF,且fromXValue为+1.0时,也就代表着你的动画的初始位置x坐标在原来视图X坐标位置的正方向偏移一个自身宽度。而当值为0时
就意味着你这个View的x轴位置相对于你自身原来X轴位置不变。
更多相关文章
- android系统启动及wifi框架分析
- 第十三篇 Android(安卓)系统电话管理机制与架构二
- Android(安卓)activity之间的跳转和传参
- android 6.0 logcat机制(二)logcat从logd中获取log保存到文件中
- web app 第三方登录-微博登录(二)
- android 界面传值
- Android(安卓)Wifi驱动--底层
- Android(安卓)中input event的分析
- Android(安卓)4.1 Netd详细分析(五)代码分析3