您的位置:首页 > 移动开发

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

2015-10-30 15:23 591 查看
1.首先是欢迎界面布局文件,只有一个背景图片:welcome.xml:

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

</LinearLayout>


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

package com.example.appshell;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

public class WelcomeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 取消标题

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.welcome);
startAnim();// 淡入效果跳转
// mHandler.sendEmptyMessageDelayed(GOTO_MAIN_ACTIVITY, 0);// 延迟3秒跳转
}

private void startAnim() {
LinearLayout welcomeImg = (LinearLayout) findViewById(R.id.ll_img);
AlphaAnimation animail = new AlphaAnimation(0.1f, 1.0f);
animail.setDuration(3000);
welcomeImg.startAnimation(animail);
animail.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent();
intent.setClass(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
}

private static final int GOTO_MAIN_ACTIVITY = 0;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {

switch (msg.what) {
case GOTO_MAIN_ACTIVITY:
Intent intent = new Intent();
intent.setClass(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
finish();
break;

default:
break;
}
};
};
}


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: