Android平移动画
核心方法
public void startAnimation(Animation animation)
执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有。
平移动画的构造方法
public TranslateAnimation(Context context, AttributeSet attrs)
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
public 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 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)
- 首先被移动对象是有一个初始坐标的,下面的参数都是基于这个原点。
- 第一个参数是动画的 X轴 起始坐标 相对于 原点 的位置
- 第二个参数是动画的 X轴 停止坐标 相对于 原点 的位置
- 第三个参数是动画的 Y轴 起始坐标 相对于 原点 的位置
- 第四个参数是动画的 Y轴 停止坐标 相对于 原点 的位置
举例:
TranslateAnimation ta = new TranslateAnimation(-200,100,0,0);
// 设置动画播放的时间
ta.setDuration(3000);
// 设置动画重复播放的次数
ta.setRepeatCount(1);
// 设置动画重复播放的模式
ta.setRepeatMode(TranslateAnimation.REVERSE);
// 开始播放动画
iv.startAnimation(ta);
动画效果如下图:
其它就不一一举例了
第三个构造方法的官方注释说明
/**
* 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)
其实这里的第2、4、6、8个参数就是对应上面四个参数构造里的第1、2、3、4个参数,这里的第1、3、5、7个参数分别是第2、4、6、8个参数的类型,上面的是默认都给设置成了“ABSOLUTE”类型,这里还可以设置RELATIVE_TO_SELF和RELATIVE_TO_PARENT等等,就不举例了