1.添加布局界面代碼:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <Button 8 android:id="@+id/btnGetSVG"
9 android:layout_width="120dp"
10 android:layout_height="50dp"
11 android:text="Getmage" />
12
13 <WebView 14 android:id="@+id/webView"
15 android:layout_width="600dp"
16 android:layout_height="400dp" />
17
18 </LinearLayout>
2.添加java代碼:
1 package com.example.testdemo; 2
3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Environment; 6 import android.view.View; 7 import android.webkit.WebSettings; 8 import android.webkit.WebView; 9 import android.widget.Button; 10
11 public class MainActivity extends Activity { 12
13 private Button btnGetSVG; 14 private WebView webView; 15
16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20
21 btnGetSVG = (Button) findViewById(R.id.btnGetSVG); 22 webView = (WebView) findViewById(R.id.webView); 23 btnGetSVG.setOnClickListener(new View.OnClickListener() { 24 public void onClick(View v) { 25 readHtmlFormAssets(); 26 } 27 }); 28
29 } 30
31 // 讀取SVG文件方法
32 private void readHtmlFormAssets() { 33 WebSettings webSettings = webView.getSettings(); 34 webSettings.setLoadWithOverviewMode(true); 35 webSettings.setJavaScriptEnabled(true); 36 webSettings.setUseWideViewPort(true); 37 webView.getSettings().setBuiltInZoomControls(true);// 會出現放大縮小的按鈕
38 webView.getSettings().setSupportZoom(true); 39 webView.getSettings().setSupportMultipleWindows(true); 40 webView.setInitialScale(75); 41
42 try { 43 // SVG圖所在路徑
44 String svg_path = "file://"
45 + Environment.getExternalStorageDirectory() 46 + "/svg/115.svg"; 47
48 if (svg_path.contains("#")) { 49 svg_path = svg_path.replaceAll("#", "%23"); 50 } 51 webView.loadUrl(svg_path); 52
53 } catch (Exception e) { 54 e.printStackTrace(); 55 } 56 } 57 }