zoukankan      html  css  js  c++  java
  • Android:WebView中对图片注册上下文菜单

    前言

      今天一朋友问我一个问题,就是如何在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()取出URL
    13             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

  • 相关阅读:
    笔记:Oracle查询重复数据并删除,只保留一条记录
    64位系统安装ODBC驱动的方法
    批量Excel数据导入Oracle数据库
    Oracle自我补充之Decode()函数使用介绍
    解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
    Linux命令区
    Linux下安装PHP+Nginx+Msql
    Thinkphp时间转换与统计的问题
    phpStydy配置memcache扩展
    Thinkphp+Nginx(PHPstudy)下报的404错误,403错误解决
  • 原文地址:https://www.cnblogs.com/luoaz/p/3756160.html
Copyright © 2011-2022 走看看