欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android绘制炫酷引导界面

程序员文章站 2024-03-03 17:14:28
一个超炫的引导界面,分享给大家 代码: mainactivity.java package com.bzu.gxs.webview1; impo...

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

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"/>

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持。