2011-11-21 83 views
10

如果有人能帮助我,我会很高兴。我有一个使用webview的应用程序。 webview加载一个网址,我已经使用Google教程覆盖了我想用webview打开的所有其他链接。我在res/slide_right xml中创建了一个动画文件,效果非常好。我在我的主要java活动中调用了这个效果,但它仅适用于第一页。我想要的是在webview中链接加载的每个页面中应用的效果。如何在web视图中使用android过渡效果?

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

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()); 
+0

你好,任何人都可以帮助我解决上述问题吗?我应该上传我的代码吗? – alexgeorg86

+0

我上面贴出我的代码,请帮我... – alexgeorg86

+0

希望这有助于 http://stackoverflow.com/q/7641121/577046 – Synxmax

回答

21

这是我能与本机API做的最好的,似乎时机是不完全正确,因为网页和动画的加载是异步..编辑:更新,这一个稍好一点。编辑2:这是迄今为止我的最佳尝试,它允许用户意识到页面正在加载。获得半平滑动画的唯一方法是允许页面预加载,而用户不会看到它。

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()); 
    } 
} 
+0

汤姆非常感谢你!你能告诉我怎么用后退按钮吗? – alexgeorg86

+0

覆盖您的活动子类onBackPressed –

+0

谁能帮我再一次,我要当我按下后退按钮开始与相反方向的动画,所以我添加“@Override \t公共无效onBackPressed(){ \t super.onBackPressed (); \t overridePendingTransition(android.R.anim.slide_out_right,android.R.anim.slide_out_right); \t}'但它不起作用。任何建议? – alexgeorg86