2011-11-21 54 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