android webview播放swf文件

android webview播放swf文件

今天做了一個通過webview播放flash的文件,以前一直以爲可以使用videoview視頻播放播放flash的東西,今天才發現不行,好像現在除了自己做一個flash播放器,就是用webview去加載flash了。廢話少說,代碼如下:

在xml裏面設置webview佈局

<WebView
       android:id="@+id/web_flash"
    android:layout_width="578px"
    android:layout_height="400px"
    android:background="#00000000"
    android:layout_centerInParent="true"
    />

然後在activity裏面加載進來

 public void loadFlash() {
     WebView mWebFlash = (WebView) findViewById(R.id.web_flash);
     WebSettings settings = mWebFlash.getSettings();
  settings.setPluginsEnabled(true);
  settings.setJavaScriptEnabled(true);
  settings.setAllowFileAccess(true);
  settings.setDefaultTextEncodingName("GBK");
  mWebFlash.setBackgroundColor(0);
  mWebFlash.loadUrl("file:///android_asset/abc.swf");
 }

在activity裏面執行loadFlash就可以播放swf文件了。一開始的時候,我播放swf文件總是在視頻邊緣有一些白邊不管怎麼設置大小都去不掉,後來發現這些白邊是webview的,可是在xml裏面設置background爲透明不管用,找美工發現也不是視頻的問題,最後發現,還有一個參數可以設置webview的背景色透明,那就是mWebFlash.setBackgroundColor(0);果然設置之後,白邊沒有了。但是webview的大小必須和視頻的實際大小一致,不然背景是白色的,這個通過設置xml的background和mWebFlash.setBackgroundColor(0);都沒有辦法去掉,暫時還沒有想到方法解決,但是已經不影響我使用了所以就先這樣呀呵呵

 

 

 

 

首先要說明,必須是Android2.2以及以上版本的手機或者平板電腦才能支持,而且並不是所有的都支持,需要裝插件。不能播放也有可能是系統CUP不支持。 Android結構圖: 

AndroidManifest.xml源代碼  <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.cn"        android:versionCode="1"       android:versionName="1.0">     <uses-permission  android:name="android.permission.INTERNET"></uses-permission>        <application android:icon="@drawable/icon" android:label="@string/app_name">          <activity android:name=".WebViewTest1"                   android:label="@string/app_name">             <intent-filter>                  <action android:name="android.intent.action.MAIN" />                 <category  android:name="android.intent.category.LAUNCHER" /> 

</intent-filter>         </activity> 

</application>       <uses-sdk android:minSdkVersion="7" /> </manifest>  Main.xml源代碼  <?xml version="1.0" encoding="utf-8"?> <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent" android:layout_height="match_parent"   android:orientation="horizontal"  >   <LinearLayout android:id="@+id/linearLayout1"   android:layout_height="match_parent"  android:layout_marginLeft="5dp"       android:layout_weight="3"    android:layout_width="match_parent">    <ListView android:id="@+id/listView1"  android:layout_height="match_parent"   android:layout_width="match_parent"></ListView> 

</LinearLayout>   <LinearLayout android:id="@+id/linearLayout2"   android:layout_height="match_parent"    android:orientation="vertical" android:layout_marginLeft="15dp"    android:layout_weight="1"    android:layout_width="match_parent">     <WebView android:id="@+id/webView1"  android:layout_height="match_parent"  android:layout_width="match_parent"></WebView>  </LinearLayout> </LinearLayout>    Listadapter.xml源代碼  <?xml version="1.0" encoding="utf-8"?> <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_height="match_parent" android:layout_width="match_parent">  

<ImageView android:layout_width="wrap_content"  android:src="@drawable/icon" android:id="@+id/imageViewCar" android:layout_height="wrap_content"></ImageView>     <TextView android:text="TextView" android:layout_width="wrap_content"  android:layout_height="wrap_content" android:layout_marginTop="15dp"  

 

android:id="@+id/textViewName"></TextView> </LinearLayout>    主類源代碼:  public class WebViewTest1 extends Activity {  File name[];  WebView wv;   ListView listview;  String filename;  ImageView iv;   Bitmap flashtobitmap;  byte ImageByte[];    /** Called when the activity is first created. */  

@Override  

public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);    wv = (WebView) findViewById(R.id.webView1);   wv.getSettings().setPluginsEnabled(true);    wv.loadUrl("http://www.baidu.com");    listview = (ListView) findViewById(R.id.listView1);   listview.setAdapter(new MyListAdapter(WebViewTest1.this, getSD()));     listview.setOnItemClickListener(new OnItemClickListener() {     @Override     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,        long arg3) {      // TODO Auto-generated method stub     filename = name[arg2].getName();  

wv.loadUrl("file:///mnt/sdcard/X_flash/" + filename);       }   });  }    File[] getSD() {   File file = new File("/sdcard/X_flash/");    File name[] = file.listFiles(new FileFilter() { 

public boolean accept(File pathname) {     // TODO Auto-generated method stub      if (pathname.getName().lastIndexOf(".swf") > 0) {      return true;      }      return false;      }    });     return name;   }  

public class MyListAdapter extends BaseAdapter {   /* 聲明變量 */    private Context mContext;    int car[] = { R.drawable.car1, R.drawable.car2, R.drawable.car3 };   int i;     MyListAdapter(Context c, File[] file) {    mContext = c;    name = file;     }   

  @Override    public int getCount() {    // TODO Auto-generated method stub    if(name!=null)    {     return name.length;    }     else return 0;   }   

@Override    public Object getItem(int position) {    // TODO Auto-generated method stub    return position;    }     @Override    public long getItemId(int position) {    // TODO Auto-generated method stub       return 0;  

}     @Override    public View getView(int position, View convertView, ViewGroup parent) {     // TODO Auto-generated method stub    /*      * TextView temp=new TextView(mContext);     * temp.setText(name[position].getName());  temp.setSingleLine(true);      * temp.setEllipsize(TruncateAt.MARQUEE);//設置爲跑馬燈     * temp.setTextSize(20); return temp;     */      LayoutInflater inflater = LayoutInflater.from(mContext);    View vw = (View) inflater.inflate(R.layout.listadapter, null);     iv = (ImageView) vw.findViewById(R.id.imageViewCar);    iv.setImageResource(car[position%3]); 

            TextView temp = (TextView)  vw.findViewById(R.id.textViewName);    temp.setSingleLine(true);    temp.setText(name[position].getName());     temp.setEllipsize(TruncateAt.MARQUEE);// 設置爲跑馬燈      temp.setTextSize(20);        return vw;      }   } }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章