【Android】【动画】自定义插值器Interpolator

public class ShakeInterpolator implements Interpolator {

    @Override
    public float getInterpolation(float timeElapsed) {
        if (timeElapsed <= 0.25f)
            return timeElapsed / 0.25f;
        else if (timeElapsed <= 0.75f)
            return 1 - (timeElapsed - 0.25f) / 0.25f;
        else
            return -1 + (timeElapsed - 0.75f) / 0.25f;
    }
}
public class ShakeAnimation extends Animation {

    private float dx;
    private float dy;

    public ShakeAnimation(int dx, int dy) {
        this.dx = dx;
        this.dy = dy;
    }

    @Override
    protected void applyTransformation(float interpolatedValue, Transformation transfer) {
        Matrix matrix = transfer.getMatrix();
        matrix.postTranslate(interpolatedValue * dx, interpolatedValue * dy);
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        setDuration(500);
        setFillAfter(true);
        setRepeatCount(-1);
        setInterpolator(new ShakeInterpolator());
    }
}
            ShakeAnimation animator = new ShakeAnimation(200, 0);
            layout.startAnimation(animator);

你可能感兴趣的:(android)