Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use android transition effects in web view?

If anyone can help me with this I will be very happy. I have an application that uses webview. The webview loads a url and I have used the Google tutorial to overide all the other links that I want to open with webview also. I have create an animimation file in res/ and a slide_right xml and so far so good. I call the effect in my main java activity but it only applies to the first page. The thing that I want is the effect to apply in every page that links loads in webview.

Can you help my with my code?

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());
like image 425
alexgeorg86 Avatar asked Nov 21 '11 02:11

alexgeorg86


1 Answers

This is the best I could do with the native apis, it seems the timing isn't quite right because the loading of the page and animation are asynchronous.. EDIT: updated, this one is marginally better. EDIT2: This is my best attempt so far, and it allows the user to realize that the page is loading.. The only way to get a semi-smooth animation is to allow the page to preload, while the user does not see it.

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());
    }
}
like image 90
Thomas Dignan Avatar answered Nov 02 '22 10:11

Thomas Dignan