天天看點

android 實作靜态圖檔檔案的手勢拉伸縮小

我的方式很簡單,将圖檔放入assets中,然後通過webview來通路本地位址,webview本身自帶放大,縮小手勢,

先附上webview的屬性設定

private void setWebView() {
    WebSettings webSettings = webView.getSettings();
    webSettings.setUseWideViewPort(true);//設定此屬性,可任意比例縮放
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setSupportZoom(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //傳回值是true的時候控制去WebView打開,為false調用系統浏覽器或第三方浏覽器
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            handler.proceed();//接受所有證書
        }
    });
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//使用緩存
    webView.getSettings().setJavaScriptEnabled(true);//使用java腳本
    webView.getSettings().setDefaultTextEncodingName("gb2312");
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            setTitle("頁面加載中,請稍候..." + progress + "%");
            setProgress(progress * 100);
            if (progress == 100) {
                setTitle(R.string.app_name);
            }
        }
    });
}      

然後是通路

webView.loadUrl("file:///android_asset/xclx.png");      

xml檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.scdl.zny.ShowImgActivity">
    <include layout="@layout/qd_include_back"></include>
    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></WebView>
</LinearLayout>
      

其實就這麼簡單,不需要重寫什麼,直接借用webview實作圖檔的放大縮小展示