17

我正在玩 GWT 的动画课程作为练习。我的目标不是为 GWT 创建另一个小部件/动画库。另外,我很清楚有很多图书馆可以做到这一点。我想做的是学习如何做到这一点,而不是如何使用某些库。这仅用于教育目的......

话虽如此,我已经实现了一个简单的类,它在一些小部件上执行 4 种不同的动画:淡入、淡出、滑入和滑出(遵循 jQuery 的动画模型)。请参阅下面的代码了解我是如何实现它的。

现场演示: http ://www.rodrigo-silveira.com/gwt-custom-animation/

我的问题:一旦触发另一个动画,我怎样才能顺利[并成功地]停止动画,并在旧动画停止的地方继续当前动画?

例如,如果在调用 slideOut() 时,slideIn() 已完成一半,我如何才能从小部件的 50% 的自然高度开始滑出?我尝试保留一个始终跟踪当前进度的成员变量,以便我可以在新动画中使用它,但似乎无法正确处理。有任何想法吗?

import com.google.gwt.animation.client.Animation;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Element;

public class RokkoAnim extends Animation {

    private Element element;
    private int currentOp;
    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 RokkoAnim(Element element) {
        this.element = element;
    }

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

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

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

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

    @Override
    protected void onUpdate(double progress) {
        switch (currentOp) {
        case RokkoAnim.FADE_IN:
            doFadeIn(progress);
            break;
        case RokkoAnim.FADE_OUT:
            doFadeOut(progress);
            break;
        case RokkoAnim.SLIDE_IN:
            doSlideIn(progress);
            break;
        case RokkoAnim.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) {
        double height = element.getScrollHeight();
        element.getStyle().setHeight(height * (1.0d - progress), Unit.PX);
    }

    private void doSlideOut(double progress) {
            // Hard coded value. How can I find out what
            // the element's max natural height is if it's 
            // currently set to height: 0 ?
        element.getStyle().setHeight(200 * progress, Unit.PX);
    }
}

用法

// 1. Get some widget
FlowPanel div = new FlowPanel();

// 2. Instantiate the animation, passing the widget
RokkoAnim anim = new RokkoAnim(div.getElement());

// 3. Perform the animation

// >> inside mouseover event of some widget:
anim.fadeIn(1500);

// >> inside mouseout event of some widget:
anim.fadeOut(1500);
4

3 回答 3

8

您需要根据当前值(相当于+=-=赋值)更新不透明度:

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);
}
于 2013-01-19T17:36:03.093 回答
1
  1. 添加一个实例方法 bool 取消到您的动画类
  2. 仅在未取消的情况下运行动画。
  3. 添加一个方法 cancelAnnimation 并在每次新动画开始时调用此方法。

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

所以这就是你可以停止动画的方式。但是现在,您需要从当前点开始新的动画。所以最好是再次修改类。

  1. 添加两个实例变量 currentProgress 和 offset (double)
  2. 向构造函数添加双偏移
  3. 取消动画时返回 currentProgress。
  4. 以第一个动画的偏移量开始新动画。

    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);
    
于 2013-01-18T16:08:40.300 回答
0

为了滑入和滑出,我修改了您的课程以使用小部件屏幕坐标:

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);
    }

}

我是水平滑出和滑入,但您可以修改代码。

我希望这会有所帮助。

于 2017-04-13T07:04:11.150 回答