以view中心为缩放点,X和Y方法各扩大一倍。
// 以view中心为缩放点,由初始状态放大两倍
ScaleAnimation animation = new ScaleAnimation(
1.0f, 2.0f, 1.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
);
animation.setDuration(3000);
view.startAnimation(animation);
// 以view左上角,X轴增加100px,Y轴增加200px,为缩放点,由初始状态放大两倍
ScaleAnimation animation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, 100.0f, 200.0f);
animation.setDuration(3000);
view.startAnimation(animation);
// 以view左上角为缩放点,由初始状态放大两倍
ScaleAnimation animation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
animation.setDuration(3000);
view.startAnimation(animation);
public class ScaleAnimation extends Animation {
private float mFromX;
private float mToX;
private float mFromY;
private float mToY;
private int mFromXType = TypedValue.TYPE_NULL;
private int mToXType = TypedValue.TYPE_NULL;
private int mFromYType = TypedValue.TYPE_NULL;
private int mToYType = TypedValue.TYPE_NULL;
private int mPivotXType = ABSOLUTE;
private int mPivotYType = ABSOLUTE;
private float mPivotXValue = 0.0f;
private float mPivotYValue = 0.0f;
private float mPivotX;
private float mPivotY;
}
private float mPivotX;
private float mPivotY;
private void initializePivotPoint() {
if (mPivotXType == ABSOLUTE) {
mPivotX = mPivotXValue;
}
if (mPivotYType == ABSOLUTE) {
mPivotY = mPivotYValue;
}
}
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
mResources = null;
mFromX = fromX;
mToX = toX;
mFromY = fromY;
mToY = toY;
mPivotXValue = pivotXValue;
mPivotXType = pivotXType;
mPivotYValue = pivotYValue;
mPivotYType = pivotYType;
initializePivotPoint();
}
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
float pivotX, float pivotY) {
mResources = null;
mFromX = fromX;
mToX = toX;
mFromY = fromY;
mToY = toY;
mPivotXType = ABSOLUTE;
mPivotYType = ABSOLUTE;
mPivotXValue = pivotX;
mPivotYValue = pivotY;
initializePivotPoint();
}
public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
mResources = null;
mFromX = fromX;
mToX = toX;
mFromY = fromY;
mToY = toY;
mPivotX = 0;
mPivotY = 0;
}
private int mFromXData = 0;
private int mToXData = 0;
private int mFromYData = 0;
private int mToYData = 0;
public ScaleAnimation(Context context, AttributeSet attrs) {
super(context, attrs);
mResources = context.getResources();
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.ScaleAnimation);
// fromXScale
TypedValue tv = a.peekValue(
com.android.internal.R.styleable.ScaleAnimation_fromXScale);
mFromX = 0.0f;
if (tv != null) {
if (tv.type == TypedValue.TYPE_FLOAT) {
// This is a scaling factor.
mFromX = tv.getFloat();
} else {
mFromXType = tv.type;
mFromXData = tv.data;
}
}
// toXScale
tv = a.peekValue(
com.android.internal.R.styleable.ScaleAnimation_toXScale);
mToX = 0.0f;
if (tv != null) {
if (tv.type == TypedValue.TYPE_FLOAT) {
// This is a scaling factor.
mToX = tv.getFloat();
} else {
mToXType = tv.type;
mToXData = tv.data;
}
}
// fromYScale
tv = a.peekValue(
com.android.internal.R.styleable.ScaleAnimation_fromYScale);
mFromY = 0.0f;
if (tv != null) {
if (tv.type == TypedValue.TYPE_FLOAT) {
// This is a scaling factor.
mFromY = tv.getFloat();
} else {
mFromYType = tv.type;
mFromYData = tv.data;
}
}
// toYScale
tv = a.peekValue(
com.android.internal.R.styleable.ScaleAnimation_toYScale);
mToY = 0.0f;
if (tv != null) {
if (tv.type == TypedValue.TYPE_FLOAT) {
// This is a scaling factor.
mToY = tv.getFloat();
} else {
mToYType = tv.type;
mToYData = tv.data;
}
}
// pivotX
Animation.Description d = Animation.Description.parseValue(a.peekValue(
com.android.internal.R.styleable.ScaleAnimation_pivotX));
mPivotXType = d.type;
mPivotXValue = d.value;
// pivotY
d = Animation.Description.parseValue(a.peekValue(
com.android.internal.R.styleable.ScaleAnimation_pivotY));
mPivotYType = d.type;
mPivotYValue = d.value;
a.recycle();
initializePivotPoint();
}
float resolveScale(float scale, int type, int data, int size, int psize) {
float targetSize;
if (type == TypedValue.TYPE_FRACTION) {
targetSize = TypedValue.complexToFraction(data, size, psize);
} else if (type == TypedValue.TYPE_DIMENSION) {
targetSize = TypedValue.complexToDimension(data, mResources.getDisplayMetrics());
} else {
return scale;
}
if (size == 0) {
return 1;
}
return targetSize/(float)size;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mFromX = resolveScale(mFromX, mFromXType, mFromXData, width, parentWidth);
mToX = resolveScale(mToX, mToXType, mToXData, width, parentWidth);
mFromY = resolveScale(mFromY, mFromYType, mFromYData, height, parentHeight);
mToY = resolveScale(mToY, mToYType, mToYData, height, parentHeight);
mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);
mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float sx = 1.0f;
float sy = 1.0f;
float scale = getScaleFactor();
if (mFromX != 1.0f || mToX != 1.0f) {
sx = mFromX + ((mToX - mFromX) * interpolatedTime);
}
if (mFromY != 1.0f || mToY != 1.0f) {
sy = mFromY + ((mToY - mFromY) * interpolatedTime);
}
if (mPivotX == 0 && mPivotY == 0) {
t.getMatrix().setScale(sx, sy);
} else {
t.getMatrix().setScale(sx, sy, scale * mPivotX, scale * mPivotY);
}
}