Android:WebView中對圖片註冊操作功能表

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

前言

  今天一朋友問我一個問題,就是如何在WebView控制項中的圖片增加操作功能表,以便增加儲存圖片等功能。今天就給他簡單做了一個示範Demo,現寫下來,給有相同問題的朋友提供些許思路吧。

概要實現

  其實這個功能很簡單,沒有太複雜的東西,就是對WebView的控制項的使用,一是給WebView註冊了操作功能表事件,二是在響應事件中去判斷事件來源的類型,如果是圖片類型,則把url取出來

 註冊操作功能表事件

  這個就比較簡單了通過下面的代碼即可完成。

 

WebView vw = (WebView) findViewById(R.id.wv); this.registerForContextMenu(vw);

取圖片URL

  這部分的重點就是,只響應圖片源的操作功能表以及取出圖片的路徑。實現代碼如下

  

 1 @Override 2     public void onCreateContextMenu(ContextMenu menu, View v, 3             ContextMenuInfo menuInfo) { 4         super.onCreateContextMenu(menu, v, menuInfo); 5  6         WebView w = (WebView)v; 7         HitTestResult result = w.getHitTestResult(); 8         //只檢測圖片格式 9         if(result.getType() == HitTestResult.IMAGE_TYPE){10             menu.addSubMenu(1, 1, 1, "儲存圖片");11 12             //通過result.getExtra()取出URL13             strUrl = result.getExtra();14             Toast.makeText(getApplicationContext(), strUrl, Toast.LENGTH_SHORT).show();15         }16     }

重點是:getHitTestResult,先來看下官方說明

Gets a HitTestResult based on the current cursor node. If a HTML::img tag is found, the HitTestResult type is set to IMAGE_TYPE and the URL is set in the "extra" field.

意思是說:根據當前觸發的節點(node)獲得一個HitTestResult對象。如果觸發事件的是一個img標籤,那麼HitTestResult的類型為IMAGE_TYPE,並且把URL放到了HitTestResult的extra域中。

  所以,通過result.getType() == HitTestResult.IMAGE_TYPE這一句話,就可以實現只響應圖片事件,而result.getExtra();則可以把圖片URL取出來。這樣有了URL就可以進行進一步的操作了,比如儲存圖片等等。

  這樣,這個功能就簡單的實現了。

後記

   這是一個只響應圖片源的例子,同理,可以做出響應超連結、電話、郵件等事件來源的處理動作,下面就把getHitTestResult的完整說明貼下:

Gets a HitTestResult based on the current cursor node. If a HTML::a tag is found and the anchor has a non-JavaScript URL, the HitTestResult type is set to SRC_ANCHOR_TYPE and the URL is set in the "extra" field. If the anchor does not have a URL or if it is a JavaScript URL, the type will be UNKNOWN_TYPE and the URL has to be retrieved through requestFocusNodeHref(Message) asynchronously. If a HTML::img tag is found, the HitTestResult type is set to IMAGE_TYPE and the URL is set in the "extra" field. A type of SRC_IMAGE_ANCHOR_TYPE indicates an anchor with a URL that has an image as a child node. If a phone number is found, the HitTestResult type is set to PHONE_TYPE and the phone number is set in the "extra" field of HitTestResult. If a map address is found, the HitTestResult type is set to GEO_TYPE and the address is set in the "extra" field of HitTestResult. If an email address is found, the HitTestResult type is set to EMAIL_TYPE and the email is set in the "extra" field of HitTestResult. Otherwise, HitTestResult type is set to UNKNOWN_TYPE.

大致意思為:根據當前觸發的節點(node)獲得一個HitTestResult對象。如果事件來源是一個a標籤,並且有一個不是JavaScript的URL,那麼HitTestResult的類型被設定為SRC_ANCHOR_TYPE,同時把URL放到了extra域中。如果這個a標籤沒有url或者是一個JavaScript的URL,那麼類型被設定為UNKNOWN_TYPE同時Url則會重新擷取。如果事件來源是一個img標籤,那麼HitTestResult的類型為IMAGE_TYPE,並且把URL放到了HitTestResult的extra域中。一個SRC_IMAGE_ANCHOR_TYPE類型表明了一個擁有圖片為子物件的超連結。如果是一個手機號,那麼類型將被設定為PHONE_TYPE,同時手機號被設定到extra域中。如果是一個地圖地址,類型則為GEO_TYPE同時地址資訊被放到extra中。如果是一個郵件地址,那麼類型被設定為EMAIL_TYPE同時emal被設定到extra域中。其他的事件來源的類型都為UNKNOWN_TYPE.

 

完整代碼:https://github.com/xiaoai-opensource/WebViewContextMenu

原文串連:http://www.cnblogs.com/luoaz/p/3756160.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.