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

WebView 添加gif 加载效果

程序员文章站 2022-06-08 08:54:45
...
package com.careland.bank;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class AnroidBankActivity extends Activity {
    /** Called when the activity is first created. */
	
	private WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        web=(WebView) this.findViewById(R.id.web);
        web.loadUrl("http://www.baidu.com");
        web.setWebViewClient(new WebViewClient(){

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				((CLDWebView)view).showloding();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				((CLDWebView)view).hideloding();
				
			}
        	
        });
        
    }
}

 

package com.careland.bank;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;

import com.ant.liao.GifView;
import com.ant.liao.GifView.GifImageType;

public class CLDWebView extends WebView {

	private GifView gif;
	public CLDWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	
	}

	public CLDWebView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public CLDWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
	}
	@Override
	protected void onMeasure(int w, int h)
	{
		super.onMeasure(w, h);
//		if(gif!=null)
//		{
//			gif.setShowDimension(128, 128);
//		}
	}
	//初始化gif 加入webview
	@Override
    protected void onAttachedToWindow()
	{
		super.onAttachedToWindow();
		if(gif==null)
		{
			gif=new GifView(this.getContext());
			gif.setGifImageType(GifImageType.COVER);
			gif.setShowDimension(128, 128);
			gif.setGifImage(R.drawable.a);
			this.addView(gif);
			
		}
		
	}
	//始终居中显示loading.gif
	@Override 
	protected void onSizeChanged(int w, int h, int ow, int oh)
	{
		super.onSizeChanged(w, h, ow, oh);
		if(gif!=null)
		{
			this.gif.setLayoutParams(new LayoutParams(128,128,(w-128)/2,(h-128)/2));
		}
	}
	public void showloding(){
		if(gif!=null)
		{
			
			gif.setVisibility(View.VISIBLE);
			gif.showAnimation();//显示动画
		}
			
	}
	public void hideloding(){
		if(gif!=null)
		{
			gif.setVisibility(View.INVISIBLE);
			gif.showCover();//gif 显示第一帧 降低内存
		}
	}
	
	
	

}

 

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    
	<!-- 
	<WebView android:id="@+id/web" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
	 -->
    <com.careland.bank.CLDWebView android:id="@+id/web" android:layout_width="fill_parent" android:layout_height="fill_parent"></com.careland.bank.CLDWebView>
</AbsoluteLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.careland.bank"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".AnroidBankActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<permission android:name="android.permission.INTERNET"></permission>
</manifest>