展会信息港展会大全

Android:WebView中对图片注册上下文菜单
来源:互联网   发布日期:2015-10-02 21:27:04   浏览:1365次  

导读:今天一朋友问我一个问题,就是如何在WebView控件中的图片增加上下文菜单,以便增加保存图片等功能。今天就给他简单做了一个演示Demo,现写下来,给有相同问题的朋友提供些许思路吧。概要实...

今天一朋友问我一个问题,就是如何在WebView控件中的图片增加上下文菜单,以便增加保存图片等功能。今天就给他简单做了一个演示Demo,现写下来,给有相同问题的朋友提供些许思路吧。

概要实现

其实这个功能很简单,没有太复杂的东西,就是对WebView的控件的使用,一是给WebView注册了上下文菜单事件,二是在响应事件中去判断事件源的类型,如果是图片类型,则把url取出来

注册上下文菜单事件

这个就比较简单了通过下面的代码即可完成。

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

this.registerForContextMenu(vw);

取图片URL

这部分的重点就是,只响应图片源的上下文菜单以及取出图片的路径。实现代码如下

复制代码

1 @Override

2public void onCreateContextMenu(ContextMenu menu, View v,

3ContextMenuInfo menuInfo) {

4super.onCreateContextMenu(menu, v, menuInfo);

5

6WebView w = (WebView)v;

7HitTestResult result = w.getHitTestResult();

8//只检测图片格式

9if(result.getType() == HitTestResult.IMAGE_TYPE){

10menu.addSubMenu(1, 1, 1, "保存图片");

11

12//通过result.getExtra()取出URL

13strUrl = result.getExtra();

14Toast.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.

赞助本站

人工智能实验室

相关热词: android开发

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港