android webview播放swf文件
今天做了一个通过webview播放flash的文件,以前一直以为可以使用videoview视频播放播放flash的东西,今天才发现不行,好像现在除了自己做一个flash播放器,就是用webview去加载flash了。废话少说,代码如下:
在xml里面设置webview布局
<WebView
然后在activity里面加载进来
在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; } } }