Android 欢迎界面淡入效果并用WebView加载网址

1.首先是欢迎界面布局文件,只有一个背景图片:welcome.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     android:id="@+id/ll_img"
4     android:layout_width="fill_parent"
5     android:layout_height="fill_parent"
6     android:background="@drawable/nav"
7     android:orientation="vertical" >
8 
9 </LinearLayout>

2.然后是欢迎界面java代码,WelcomeActivity.java,有淡入进入界面效果和延迟跳转效果:

 1 package com.example.appshell;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.os.Handler;
 7 import android.view.Window;
 8 import android.view.animation.AlphaAnimation;
 9 import android.view.animation.Animation;
10 import android.widget.LinearLayout;
11 
12 public class WelcomeActivity extends Activity {
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         // 取消标题
17 
18         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
19         setContentView(R.layout.welcome);
20         startAnim();// 淡入效果跳转
21         // mHandler.sendEmptyMessageDelayed(GOTO_MAIN_ACTIVITY, 0);// 延迟3秒跳转
22     }
23 
24     private void startAnim() {
25         LinearLayout welcomeImg = (LinearLayout) findViewById(R.id.ll_img);
26         AlphaAnimation animail = new AlphaAnimation(0.1f, 1.0f);
27         animail.setDuration(3000);
28         welcomeImg.startAnimation(animail);
29         animail.setAnimationListener(new Animation.AnimationListener() {
30             @Override
31             public void onAnimationStart(Animation animation) {
32             }
33 
34             @Override
35             public void onAnimationRepeat(Animation animation) {
36             }
37 
38             @Override
39             public void onAnimationEnd(Animation animation) {
40                 Intent intent = new Intent();
41                 intent.setClass(WelcomeActivity.this, MainActivity.class);
42                 startActivity(intent);
43                 finish();
44             }
45         });
46     }
47 
48     private static final int GOTO_MAIN_ACTIVITY = 0;
49     private Handler mHandler = new Handler() {
50         public void handleMessage(android.os.Message msg) {
51 
52             switch (msg.what) {
53             case GOTO_MAIN_ACTIVITY:
54                 Intent intent = new Intent();
55                 intent.setClass(WelcomeActivity.this, MainActivity.class);
56                 startActivity(intent);
57                 finish();
58                 break;
59 
60             default:
61                 break;
62             }
63         };
64     };
65 }

3.跳转界面布局文件activity_main.xml,包括WebView控件和一个进度条:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

4.跳转界面java代码MainActivity.xml,展示webview加载网页的进度条效果

package com.example.appshell;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private static final String URL = "http://www.cnblogs.com/_ymw/";

    WebView webView;
    ProgressBar bar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 取消标题

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        // 实例化WebView
        webView = (WebView) this.findViewById(R.id.webView);
        /** 调用loadUrl()方法进行加载内容 */

        /** 设置WebView的属性,此时可以去执行JavaScript脚本 */

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setVerticalScrollbarOverlay(true); // 指定的垂直滚动条有叠加样式
        WebSettings settings = webView.getSettings();
        settings.setUseWideViewPort(true);// 设定支持viewport
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);// 设定支持缩放

        if (webView != null) {

            bar = (ProgressBar) findViewById(R.id.pb);
            webView.setWebChromeClient(new WebChromeClient() {

                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if (newProgress == 100) {
                        bar.setVisibility(View.GONE);
                    } else {
                        if (View.INVISIBLE == bar.getVisibility()) {
                            bar.setVisibility(View.VISIBLE);
                        }
                        bar.setProgress(newProgress);
                    }
                    super.onProgressChanged(view, newProgress);
                }

            });

            webView.loadUrl(URL);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // 返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
                    view.loadUrl(url);
                    return true;
                }
            });
        }

    }

    // 改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();// 返回上一页面
                return true;
            } else {
                System.exit(0);// 退出程序
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}

云盘分享链接:http://yunpan.cn/cFzLptV942f4q  访问密码 c0da

posted @ 2015-10-30 15:23  _YMW  阅读(610)  评论(0编辑  收藏  举报