Android中WebView显示flash闪白屏解决方案

播放flash(.swf格式)文件通常有两种方式:SWF播放器,WebView,以下仅讨论WebView播放flash的情况:

使用场景:需要对UI做定制,明显,自己写个flash播放器工作量太大,WebView轻巧多了。步骤如下:

1.安装Adobe的flash插件

2.用WebView加载 flash

3.白屏解决办法:用FrameLayout,用一个ImageView把闪白屏的那一下遮住【建议用暗色调的图片】,可以给ImageView设这渐变的动画效果。

PS:

1、WebView播放网络资源很方便,用http协议的url 

2、WebView中加载Assets目录下文件很方便,使用file协议

Avtivity源码:

package com.example.helloworld;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private WebView mWebView;
	private ImageView ic_launcher ;
	private Handler mHandler ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//隐藏标题,设置全屏:必须在添加显示内容之前,否则报错
		requestWindowFeature(Window.FEATURE_NO_TITLE);                  //隐藏标题
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);            //设置全屏
		setContentView(R.layout.activity_main);
		mWebView = (WebView) findViewById(R.id.web);
		ic_launcher = (ImageView) findViewById(R.id.ic_launcher);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);  //JS可用
		//webSettings.setPluginsEnabled(true);//过时
		webSettings.setPluginState(WebSettings.PluginState.ON);//设置adobe插件可用
		//mWebView.loadUrl("file:///android_asset/hht/gxt/gxt.swf");
		//mWebView.loadUrl("file:///sdcard/hht/gxt/国学堂.swf");//ok
		//mWebView.loadUrl("file:///sdcard/hht火火兔/gxt/国学堂.swf"); //swf无法正常跳转
		mWebView.loadUrl("http://img.61flash.com/20101125//999/dlm.swf");//网络资源
		mHandler = new Handler(){
			@SuppressLint("NewApi")
			@Override
			public void handleMessage(Message msg) {
				ic_launcher.setImageAlpha(msg.what);//修改透明度
				super.handleMessage(msg);
			}
		};
		//开启子线程,实现动画,修改透明度
		MThread mThread  = new MThread();
		mThread.start();
	}

	private class  MThread extends Thread{
		public void run(){
			for(int i=255;i>0;i--){
				try {
					Thread.sleep(25);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				mHandler.sendEmptyMessage(i);
			}
		}
	}

	/*
	 * 检查是否安装插件:用包名判断
	 */
	private boolean check() {  
		PackageManager pm = getPackageManager();  
		List<PackageInfo> infoList = pm  
				.getInstalledPackages(PackageManager.GET_SERVICES);  
		for (PackageInfo info : infoList) {  
			if ("com.adobe.flashplayer".equals(info.packageName)) {  
				return true;  
			}  
		}  
		return false;  
	}
}

Layout:布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/web"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black" />

    <ImageView
        android:id="@+id/ic_launcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/p3" />

</FrameLayout>

展开阅读全文

没有更多推荐了,返回首页