Android移动和缩放自定义动画

时间:2016-06-08 13:50:07

标签: android animation

我正在尝试创建自己的自定义动画,该动画将移动并缩放视图。

public class MoveAndSizeAnimation extends Animation implements Animation.AnimationListener{

View view;
float fromX;
float fromY;
float fromWidth;
float fromHeight;
float toX;
float toY;
float toWidth;
float toHeight;

public MoveAndSizeAnimation(View v, float toX, float toY, float toWidth, float toHeight, int duration) {
    this.view = v;
    this.toX = toX;
    this.toY = toY;
    this.toWidth = toWidth;
    this.toHeight = toHeight;

    fromX = v.getX();
    fromY = v.getY();
    fromWidth = v.getWidth();
    fromHeight = v.getHeight();

    this.setInterpolator(new AccelerateDecelerateInterpolator());
    setDuration(duration);
}



@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {

    float newX = fromX + (toX - fromX) * interpolatedTime;
    float newY = fromY + (toY - fromY) * interpolatedTime;
    float newWidth = fromWidth + (toWidth - fromWidth) * interpolatedTime;
    float newHeight = fromHeight + (toHeight - fromHeight) * interpolatedTime;
    float scaledWidth = newWidth / fromWidth;
    float scaledHeight = newHeight / fromHeight;

    view.setX(newX);
    view.setY(newY);

    view.requestLayout();

}

@Override
public boolean willChangeBounds() {
    return false;
}}

移动动画没问题,但无法正确缩放。 我试图设置视图layoutParams,但它确实改变了视图的宽度和高度。我只想扩展。

有人可以帮忙吗?

或者我怎样才能看到Android提供的ScaleAnimation类如何工作?

1 个答案:

答案 0 :(得分:0)

好的,所以答案是使用scaleX和scaleY,不要忘记设置setPivotX(0.5f)和setPivotY(0.5f)..就是这样..