【问题标题】:how to animate a view as a webview will scroll in android如何为视图设置动画,因为 webview 将在 android 中滚动
【发布时间】:2014-01-17 00:02:42
【问题描述】:

我在布局底部有一个 web 视图和一个按钮。 我想为这个按钮设置动画,因为我的 webview 将被滚动。 但是当我滚动我的 webview 时,我无法跟踪正在调用的函数。

实际上我想跟踪 webview 滚动,我不知道滚动 webview 时调用了哪个回调函数。

提前谢谢...

【问题讨论】:

    标签: android webview


    【解决方案1】:

    使用这个很棒的库:Nineoldandroids

    您将为您的Button 和您的WebView 设置动画,如下所示:

    AnimatorSet set = new AnimatorSet();
    set.playTogether(
        ObjectAnimator.ofFloat(myButton, "rotationY", 0, 360),   /* rotate Y  coordonates of your button from 0 to 360 degre */
        ObjectAnimator.ofFloat(myWebView, "translationY", 0, 500), /* translate Y coordonates of WebView from top to 500 */
    
    );
    set.setDuration(3 * 1000).start(); /* A delay of 3 seconds for this animation */
    

    如果你想添加一个监听器:

    set.addListener(new AnimatorListener() {
    
            @Override
            public void onAnimationStart(Animator animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationRepeat(Animator animation) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO Auto-generated method stub
    
    
            }
    
            @Override
            public void onAnimationCancel(Animator animation) {
                // TODO Auto-generated method stub
    
            }
        });
    

    【讨论】:

    • 没关系,但实际上我想跟踪 webview 滚动,我不知道滚动 webview 时调用了哪个回调函数。
    • 问题是什么时候运行这段代码,这段代码必须在webview滚动时完成,我无法跟踪webview的滚动。我想知道我需要重写哪个回调函数来跟踪滚动。
    【解决方案2】:

    要么继承 WebView 并覆盖 onScrollChanged(不要忘记调用 super!),或者如果由于某种原因不能继承,则使用 ViewTreeObserver

    class MyWebView extends android.webkit.WebView {
        @Override
        protected void onScrollChanged (int l, int t, int oldl, int oldt) {
            super.onScrollChanged(l, t, oldl, oldt);
            // Your code here.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 2015-03-18
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多