如何在Web视图中使用android过渡效果?

时间:2011-11-21 02:02:15

标签: android android-webview android-animation

如果有人能帮助我,我将非常高兴。我有一个使用webview的应用程序。 webview加载了一个网址,我使用Google教程覆盖了我想用webview打开的所有其他链接。我在res/slide_right xml创建了一个动画文件,到目前为止一直很好。我在主java活动中调用了效果,但它仅适用于第一页。我想要的是在webview中链接加载的每个页面中应用的效果。

你能用我的代码帮助我吗?

package com.ihome;

import android.app.Activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class IhomeActivity extends Activity {
    WebView mWebView;
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Animation slideRightAnimation = AnimationUtils.loadAnimation(getBaseContext (), R.anim.slide_right);
            mWebView.startAnimation(slideRightAnimation);
            view.loadUrl(url);
            return true;
        }
    }


@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            Animation slideLeftAnimation = AnimationUtils.loadAnimation(getBaseContext (), R.anim.slide_left);
            mWebView.startAnimation(slideLeftAnimation);
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.setVerticalScrollBarEnabled(false);
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://www.google.com/");
    mWebView.setWebViewClient(new HelloWebViewClient());

1 个答案:

答案 0 :(得分:23)

这是我用本机apis做的最好的,似乎时机不对,因为页面和动画的加载是异步的。编辑:更新,这个稍微好一些。 EDIT2:这是我迄今为止最好的尝试,它允许用户意识到页面正在加载..获得半平滑动画的唯一方法是允许页面预加载,而用户没有看到它。

package com.adeptdev.animwebview;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebViewActivity extends Activity {
    ProgressDialog mProgressDialog;


    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.setVisibility(View.GONE);
            mProgressDialog.setTitle("Loading");
            mProgressDialog.show();
            mProgressDialog.setMessage("Loading " + url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mProgressDialog.dismiss();
            animate(view);
            view.setVisibility(View.VISIBLE);
            super.onPageFinished(view, url);
        }
    }

    private void animate(final WebView view) {
        Animation anim = AnimationUtils.loadAnimation(getBaseContext(),
                android.R.anim.slide_in_left);
        view.startAnimation(anim);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView view = (WebView) findViewById(R.id.webview);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
            view.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mProgressDialog = new ProgressDialog(this);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com/");
        webView.setWebViewClient(new HelloWebViewClient());
    }
}