我的应用中,webview是作为一个插件放到布局中的。插件的位置和大小不是固定的,而是由用户指定。
问题来了,当装载webview的layout. X坐标不是0时,flash的右边会被会有一部分显示不出来,
如layout 的x坐标为100, 则flash的右边有100的画面宽度播放不出来。
找遍网络没有找到解决方法,怀疑是flash插件的问题。
最终的解决方法为: 保持Layout的坐标不变,为(0,0). 只移动webview的坐标,而且webview不直接加载swf, 而是通过加载包含SWF的网页来播放。在网页中指定flash的大小。
主要代码如下
//FlashActivity.java
public class FlashActivity extends Activity{
private String strFile;
private WebView wv, webView;
private int playIndex=0;
private AreaBean areaBean=new AreaBean();
private int m_width,m_height;
private LinearLayout webViewLayout;
private LinearLayout webViewLayout1;
private final static String FLASHDRI="file:///mnt/sdcard/Flash/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
InputStream is;
is = getAssets().open("index.html");
strFile = readTextFile(is);
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wv = new WebView(this.getParent()==null? this : this.getParent());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setPluginsEnabled(true);//支持所有版本
wv.getSettings().setPluginState(PluginState.ON);//只支持2.2以上的版本
wv.setWebChromeClient(new WebChromeClient());
wv.addJavascriptInterface(new CallJava(), "CallJava");
webViewLayout = (LinearLayout)findViewById(R.id.webViewLayout);
webViewLayout1 = (LinearLayout)findViewById(R.id.webViewLayout1);
webViewLayout1.addView(wv);
playIndex=0;
Object object=getIntent().getExtras().get("Plugin");
getPluginAreaBean(object);
m_width = areaBean.getWidthRate(); //自定义的flash的宽度
m_height = areaBean.getHeightRate(); //自定义的flash的高度
wv.setX(areaBean.getLeftRate()); //webView的左上角坐标
wv.setY(areaBean.getTopRate());
wv.setBackgroundColor(Color.argb(0, 0, 0,0));
webViewLayout1.setBackgroundColor(Color.argb(0, 0, 0,0));
webViewLayout.setBackgroundColor(Color.argb(0, 0, 0,0));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(m_width+(int)wv.getX(), LayoutParams.MATCH_PARENT); //关键是这一句,原因不解释
webViewLayout1.setLayoutParams(params);
if(areaBean.getAreaplanBeans().size()>0){
setFlashPath(FLASHDRI+areaBean.getAreaplanBeans().get(playIndex).getFileName());
}
}
private void setFlashPath(String swf)
{
wv.loadDataWithBaseURL(null,
strFile.replace("flash.swf", swf)
.replace("height=100", "height="+m_height)
.replace("width=100", "width="+m_width),
"text/html", "UTF-8", null);
}
private final class CallJava{
public void consoleFlashProgress(float progressSize){
}
public void FlashLoaded(){
}
}
private String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
/**
* 调用隐藏的WebView方法 <br />
* 说明:WebView完全退出swf的方法,停止声音的播放。
* @param name
*/
private void callHiddenWebViewMethod(String name){
if (wv != null){
try{
Method method = WebView.class.getMethod(name);
method.invoke(wv); // 调用
} catch (NoSuchMethodException e) { // 没有这样的方法
Log.i("No such method: " + name, e.toString());
} catch (IllegalAccessException e) { // 非法访问
Log.i("Illegal Access: " + name, e.toString());
} catch (InvocationTargetException e) { // 调用的目标异常
Log.d("Invocation Target Exception: " + name, e.toString());
}
}
}
@Override
protected void onPause(){
super.onPause();
wv.pauseTimers();
if (isFinishing()){
wv.loadUrl("about:blank");
setContentView(new FrameLayout(this));
}
callHiddenWebViewMethod("onPause");
}
@Override
protected void onResume(){
super.onResume();
wv.resumeTimers();
callHiddenWebViewMethod("onResume");
}
}
//main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg3"
android:id="@+id/webViewLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webViewLayout1"
>
</LinearLayout>
</LinearLayout>
//index.html
<html>
<head>
<title>Android Flash Player</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var playover=true;//直接播放
var total;//定义flash影片总桢数
var frame_number=-1;//定义flash影片当前桢数
//动态显示播放影片的当前桢/总桢数(进度条显示)
function showcount(){
//已测可用CallJava.consoleFlashProgress(5);
var num=1+fmovie.CurrentFrame();
var millisec=150;
if(num<frame_number || num==total)
{CallJava.consoleFlashProgress(100);millisec=1000;}
else
CallJava.consoleFlashProgress(100*(num/total));
frame_number=num;
//geolo.innerText = "n/m: "+frame_number+"/"+total
setTimeout(showcount,millisec);
}
function isLoad(){
if(fmovie.PercentLoaded()==100)frame_number=0;
}
//加载影片
function Load(){
if(frame_number>=0) {
total = fmovie.TotalFrames();
if(playover && total>1) Play();
CallJava.FlashLoaded();
setTimeout(showcount,100);
}else {
setTimeout(Load,100);
setTimeout(isLoad,50);
}
}
//播放影片
function Play(){
playover=true;
fmovie.Play();
}
//暂停播放
function Pause(){
fmovie.StopPlay();
}
//影片高宽
function SetWH(x,y){
var m=document.getElementById("fmovie")
m.width=x;
m.height=y;//m.width=x;
}
setTimeout(Load,100);
</script>
</head>
<body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"
align="middle">
<param name="movie" value="about:blank" />
<param name="quality" value="high" />
<embed src="flash.swf" bgcolor="#575757" id="fmovie" height=100 width=100></embed>
</object>
</body>
</html>