android应用中插入flash

无论如何,我们需要一个android2.2的平板电脑或者android2.2的手机一部,同时我们的android平台需要安装最新的flash for android 的插件。  具备了这些之后,我们就可以将flash通过webView的方式嵌入到我们自己的程序中了。

  需要的知识:JavaScript知识、java知识、html知识,了解css的话,那最好。

  不多说,和以前一样,直接上代码,上图。不解释太多。。。。

1. 项目结构图



2. 程序运行图

[img]http://hi.csdn.net/attachment/201012/17/91385_1292549973BLl1.png
[/img]




Java代码 
01. package com.geolo.js.falsh;    
02. import android.app.Activity;    
03. import android.os.Bundle;    
04. import android.os.Handler;    
05. import android.os.Message;    
06. import android.view.View;    
07. import android.webkit.WebChromeClient;    
08. import android.webkit.WebSettings.PluginState;    
09. import android.webkit.WebView;    
10. import android.widget.Button;    
11. import android.widget.FrameLayout;    
12. import android.widget.ProgressBar;    
13. public class MainActivity extends Activity {    
14.     private WebView mWebView;    
15.     private Button playButton,pauseButton;    
16.     private ProgressBar mProgressBarHorizontal;    
17.     private final static int PROGRESSBARSIZE = 0x0000;    
18.     @Override   
19.     public void onCreate(Bundle savedInstanceState) {    
20.         super.onCreate(savedInstanceState);    
21.         setContentView(R.layout.main);    
22.         mWebView = (WebView)findViewById(R.id.webView01);     
23.         mProgressBarHorizontal = (ProgressBar)findViewById(R.id.progress_horizontal);    
24.         this.setProgress(mProgressBarHorizontal.getProgress() * 100);    
25.         //this.setSecondaryProgress(mProgressBarHorizontal.getSecondaryProgress() * 100);    
26.         playButton = (Button)findViewById(R.id.playButton);    
27.         pauseButton = (Button)findViewById(R.id.pauseButton);    
28.         playButton.setOnClickListener(buttonListener);    
29.         pauseButton.setOnClickListener(buttonListener);    
30.         mWebView.getSettings().setJavaScriptEnabled(true);      
31.         //mWebView.getSettings().setPluginsEnabled(true);    
32.         mWebView.getSettings().setPluginState(PluginState.ON);    
33.         mWebView.setWebChromeClient(new WebChromeClient());     
34.         mWebView.addJavascriptInterface(new CallJava(), "CallJava");    
35.         mWebView.loadUrl("file:///android_asset/sample/index.html");     
36.         startThread();    
37.     }    
38.     Button.OnClickListener buttonListener = new Button.OnClickListener() {      
39.         @Override   
40.         public void onClick(View v) {    
41.             int buttonID = v.getId();    
42.             switch (buttonID) {    
43.             case R.id.playButton:    
44.                 mWebView.loadUrl("javascript:Play()");    
45.                 showFlashProgress(5);    
46.                 break;    
47.             case R.id.pauseButton:    
48.                 mWebView.loadUrl("javascript:Pause()");    
49.                 break;    
50.             default:    
51.                 break;    
52.             }    
53.         }    
54.     };    
55.     public void showFlashProgress(float progressSize){    
56.         int size = (int)progressSize;    
57.         //Toast.makeText(this, size+"", Toast.LENGTH_SHORT).show();    
58.         mProgressBarHorizontal.setProgress(size);    
59.     }    
60.     @Override   
61.     protected void onPause(){    
62.         super.onPause();    
63.         mWebView.pauseTimers();    
64.         if(isFinishing()){    
65.             mWebView.loadUrl("about:blank");    
66.             setContentView(new FrameLayout(this));    
67.         }    
68.     }    
69.     @Override   
70.     protected void onResume(){    
71.         super.onResume();    
72.         mWebView.resumeTimers();    
73.     }    
74.     private final class CallJava{    
75.         public void consoleFlashProgress(float  progressSize){    
76.             showFlashProgress(progressSize);    
77.         }    
78.     }    
79.     private void startThread(){    
80.         //通过线程来改变ProgressBar的值    
81.         new Thread(new Runnable() {    
82.             @Override   
83.             public void run() {    
84.                 while(!Thread.currentThread().isInterrupted()){    
85.                     try {    
86.                         Thread.sleep(2000);    
87.                         Message message = new Message();    
88.                         message.what = MainActivity.PROGRESSBARSIZE;    
89.                         MainActivity.this.myMessageHandler.sendMessage(message);    
90.                     } catch (Exception e) {    
91.                         Thread.currentThread().interrupt();    
92.                     }    
93.                 }    
94.             }    
95.         }).start();    
96.     }    
97.     Handler myMessageHandler = new Handler() {    
98.         @Override   
99.         public void handleMessage(Message msg) {    
100.             switch (msg.what) {    
101.             case MainActivity.PROGRESSBARSIZE:    
102.                 mWebView.loadUrl("javascript:showcount()");    
103.                 break;    
104.             default:    
105.                 break;    
106.             }    
107.             super.handleMessage(msg);    
108.         }    
109.     };    
110. }   


4. main.xml




Java代码 
01. <?xml version="1.0" encoding="utf-8"?>   
02. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
03.     android:orientation="vertical" android:layout_width="fill_parent"   
04.     android:layout_height="fill_parent">   
05.     <WebView android:id="@+id/webView01" android:layout_width="wrap_content"   
06.         android:layout_height="wrap_content" />   
07.             
08.     <ProgressBar android:id="@+id/progress_horizontal"   
09.         style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"   
10.         android:layout_height="wrap_content" android:max="100"   
11.         android:progress="0" android:secondaryProgress="0" />   
12.             
13.     <LinearLayout android:orientation="horizontal"   
14.         android:layout_width="fill_parent" android:layout_height="wrap_content">   
15.         <Button android:id="@+id/playButton" android:layout_width="wrap_content"   
16.             android:layout_height="wrap_content" android:text="play" />   
17.         <Button android:id="@+id/pauseButton" android:layout_width="wrap_content"   
18.             android:layout_height="wrap_content" android:text="pause" />   
19.    </LinearLayout>   
20. </LinearLayout>   


5. index.html




Java代码 
01. <mce:script src="play.js" mce_src="play.js"></mce:script>   
02. <table border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">   
03.   <tr>   
04.     <td>   
05.      <object id="movie" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"     
06.          codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"     
07.         align="middle">   
08.        <param name="movie" value="about:blank" />   
09.        <param name="quality" value="high" />   
10.      </object>   
11.     </td>   
12.   </tr>   
13. </table>   
14.        
15.        
16.    <!-- <a href="javascript:CallJava.consoleFlashProgress(3)" mce_href="javascript:CallJava.consoleFlashProgress(3)">add Progress</a>     
17.    <a href="#" mce_href="#" onClick="showcount()">add Progress</a>     
18.     -->   
19. <mce:script type="text/javascript"><!--    
20.   loadSWF("testFlash.swf","800","480"); //loadSWF("flash地址","宽度","高度")    
21. // --></mce:script> 


6.play.js




Java代码 
01. var total;//定义flash影片总桢数    
02. var frame_number;//定义flash影片当前桢数    
03. //以下是滚动条图片拖动程序    
04. var dragapproved=false;    
05. var z,x,y    
06. //动态显示播放影片的当前桢/总桢数(进度条显示)    
07. function showcount(){    
08.     //已测可用CallJava.consoleFlashProgress(5);    
09.     total = movie.TotalFrames;    
10.     frame_number=movie.CurrentFrame();    
11.     frame_number++;    
12.     var progressSize = 100*(frame_number/movie.TotalFrames());    
13.     CallJava.consoleFlashProgress(progressSize);    
14. }    
15. //播放影片     
16. function Play(){    
17.     movie.Play();    
18. }    
19. //暂停播放    
20. function Pause(){    
21.  movie.StopPlay();    
22. }    
23. //开始载入flash影片    
24. function loadSWF(fsrc,fwidth,fheight){    
25.  movie.LoadMovie(0, fsrc);    
26.  movie.width=fwidth;    
27.  movie.height=fheight;    
28.  frame_number=movie.CurrentFrame();    
29.  jindu();    
30. }   

你可能感兴趣的:(android)