欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android绘制炫酷引导界面

时间:2021-07-20 08:53:25|栏目:Android代码|点击:

一个超炫的引导界面,分享给大家

代码:
MainActivity.java

package com.bzu.gxs.webview1;

import android.app.Activity;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

  private MyWebView myWebView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myWebView = (MyWebView) findViewById(R.id.webView);
    myWebView.getSettings().setJavaScriptEnabled(true);
    init();
    myWebView.loadUrl("http://h5.eqxiu.com/s/F93iW6fu");
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
      myWebView.goBack();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  public void init(){
    if(Build.VERSION.SDK_INT >= 19) {
      myWebView.getSettings().setLoadsImagesAutomatically(true);
    } else {
      myWebView.getSettings().setLoadsImagesAutomatically(false);
    }
  }

}

MyWebView.java

package com.bzu.gxs.webview1;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * Created by GXS on 2016/5/12.
 */
public class MyWebView extends WebView{

  private ProgressBar progressBar;
  private Context mContext;

  public MyWebView(Context context, AttributeSet attributeSet) {
    super(context,attributeSet);
    mContext = context;

    progressBar = (ProgressBar) LayoutInflater.from(context).inflate(R.layout.progressbar,null);
    progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,6,0,0));
    addView(progressBar);
    setWebChromeClient(new WebChromeClient());
    setWebViewClient(new WebViewClient(){
      @Override
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(mContext,"Gxs"+description,Toast.LENGTH_SHORT).show();
      }
    });
    this.getSettings().setBuiltInZoomControls(true);
    this.getSettings().setUseWideViewPort(true);
  }

  public class WebChromeClient extends android.webkit.WebChromeClient {

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
      if (newProgress == 100) {
        progressBar.setVisibility(GONE);
      } else {

        if (progressBar.getVisibility() == GONE)
          progressBar.setVisibility(VISIBLE);
          progressBar.setProgress(newProgress);
      }
      super.onProgressChanged(view,newProgress);

    }

  }

  @Override
  protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    LayoutParams layoutParams = (LayoutParams) progressBar.getLayoutParams();
    layoutParams.x = l;
    layoutParams.y = t;
    progressBar.setLayoutParams(layoutParams);
    super.onScrollChanged(l, t, oldl, oldt);
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.bzu.gxs.webview1.MainActivity">

  <com.bzu.gxs.webview1.MyWebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World!" />
</RelativeLayout>

progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ProgressBar"
  style="?android:attr/progressBarStyleHorizontal"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:indeterminate="false"
  android:maxHeight="10dip"
  android:minHeight="10dip"
  android:progress="50"
  android:progressDrawable="@drawable/greenprogress" />

注意: 需要在清单文件 AndroidManifest.xml 中加入:
<uses-permission android:name="android.permission.INTERNET"/>

上一篇:关注Ionic底部导航按钮tabs在android情况下浮在上面的处理

栏    目:Android代码

下一篇:解析Android应用程序运行机制

本文标题:Android绘制炫酷引导界面

本文地址:http://www.codeinn.net/misctech/159413.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有