Smooth jQuery-like animation in GWT

我怕爱的太早我们不能终老 提交于 2019-12-03 05:05:21
Shedolamack

You need to update the opacity based on the current value (equivalent to += or -= assignments):

private void doFadeOut(double progress) {
    double opacity = element.getStyle().getOpacity();
    element.getStyle().setOpacity(opacity - 1.0d - progress);
}

private void doFadeIn(double progress) {
    double opacity = element.getStyle().getOpacity();
    element.getStyle().setOpacity(opacity + progress);
}
  1. Add a instance method bool canceled to your annimation class
  2. Only run the annimation if not canceled.
  3. Add a method cancelAnnimation and call this method everytime when the new annimation starts.

    private bool canceled = false;
    
    public void cancelAnnimation() {
        canceled = true;
    }
    
    private void doFadeOut(double progress) {
        if(!canceled) {
           element.getStyle().setOpacity(1.0d - progress);
        }
    }
    
    ... etc.
    

So thats the way you can stop the animation. But now, you need to start the new animation from the current point. So the best was to modify the class again.

  1. add two instance variables currentProgress and offset (double)
  2. add an double offset to the constructor
  3. return currentProgress when cancel the annimation.
  4. start the new animation with the offset from the first animation.

    private bool canceled = false;
    private double currentProgress = 0;
    private double offset = 0;
    
    public RokkoAnim(Element element, double offset) {
        this.element = element;
        this.offset = offset;
    }
    
    public double cancelAnimation() {
        canceled = true;
        return currentProgress;
    }
    
    private void doFadeOut(double progress) {
    
        if(!canceled && ((progress + offset) <= 1)) {
           element.getStyle().setOpacity(1.0d - (progress+offset));
           currentProgress = progress+offset;
        }
    }
    
    ... etc.
    
    
    double offset = anim1.cancelAnimation();
    RokkoAnim anim2 = new RokkoAnim(div.getElement(), offset);
    

for sliding in and out I have modified your class in order to use the widget screen cordinates:

public class WidgetAnimation extends Animation {

    private Element element;
    private int currentOp;
    private int startX=-1;
    private int finalX=-1;
    private final static int FADE_OUT = 0;
    private final static int FADE_IN = 1;
    private final static int SLIDE_IN = 2;
    private final static int SLIDE_OUT = 3;


    public WidgetAnimation(Element element) {
        this.element = element;
    }

    public void fadeOut(int durationMilli) {
        cancel();
        currentOp = WidgetAnimation.FADE_OUT;
        run(durationMilli);
    }

    public void fadeIn(int durationMilli) {
        cancel();
        currentOp = WidgetAnimation.FADE_IN;
        run(durationMilli);
    }

    public void slideIn(int durationMilli) {
        cancel();
        currentOp = WidgetAnimation.SLIDE_IN;
        run(durationMilli);
    }

    public void slideOut(int durationMilli) {
        cancel();
        currentOp = WidgetAnimation.SLIDE_OUT;
        run(durationMilli);
    }

    @Override
    protected void onUpdate(double progress) {
        switch (currentOp) {
        case WidgetAnimation.FADE_IN:
            doFadeIn(progress);
            break;
        case WidgetAnimation.FADE_OUT:
            doFadeOut(progress);
            break;
        case WidgetAnimation.SLIDE_IN:
            doSlideIn(progress);
            break;
        case WidgetAnimation.SLIDE_OUT:
            doSlideOut(progress);
            break;
        }
    }

    private void doFadeOut(double progress) {
        element.getStyle().setOpacity(1.0d - progress);
    }

    private void doFadeIn(double progress) {
        element.getStyle().setOpacity(progress);
    }

    private void doSlideIn(double progress) {
        if(startX==-1){
            finalX = element.getAbsoluteLeft();
            startX = - Window.getClientWidth();
        }

        int positionX = (int) (startX + (progress * (this.finalX - startX)));

        this.element.getStyle().setLeft(positionX, Unit.PX);
    }

    private void doSlideOut(double progress) {
        if(startX==-1){
            startX = element.getAbsoluteLeft();
            finalX = - Window.getClientWidth();
        }

        int positionX = (int) (startX + (progress * (this.finalX - startX)));
        GWT.log("StartX:" + startX);
        GWT.log("finalX:" + finalX);
        GWT.log("positionX:" + positionX);
        GWT.log("progress:" + progress);

        this.element.getStyle().setLeft(positionX, Unit.PX);
    }

}

I am sliding out and in horizontally, but you can modify the code.

I hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!