以view的左上角作为 起点
创建
1. ABSOLUTE 绝对路径模式 (左上角 + 要移动到的目标)
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;
}
2. 可控搭配模式 (左上角 + 要移动到的目标)
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;
}
根据参数,计算距离
//计算view 要移动的距离(以左上角为起始 值为0)
protected float resolveSize(int type, float value, int size, int parentSize) {
switch (type) {
case ABSOLUTE: //绝对路径模式
return value;
case RELATIVE_TO_SELF: //相对自身模式
return size * value; //size 相对自身的%
case RELATIVE_TO_PARENT: //相对父层模式
return parentSize * value; // parentSize 相对父层的%
default:
return value;
}
}
平移动画实现
//不断调用 0-interpolatedTime (0-动画时间)/动画时间
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float dx = mFromXDelta; //根据位置值 与模式计算出来的值
float dy = mFromYDelta;
if (mFromXDelta != mToXDelta) {
dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
}
if (mFromYDelta != mToYDelta) {
dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
}
t.getMatrix().setTranslate(dx, dy);
}