Android 使用WebView控件展示SVG图
16lz
2021-01-23
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 }
更多相关文章
- Android布局属性详解之RelativeLayout
- android排版布局学习
- 【分享】大量android软件开发资料和实例代码【转】
- xmlns:android作用以及自定义布局属性
- android 布局文件 layout_weight用法
- android表格布局
- andriod布局常用控件
- 关于相对布局RelativeLayout的各种属性