重写webView之 X5WebView

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsoluteLayout;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.yuanxin.clan.R;
import com.yuanxin.clan.core.app.UserNative;
import com.yuanxin.clan.core.company.utils.MyWebChromeClient;
import com.yuanxin.clan.core.util.FastJsonUtils;
import com.yuanxin.clan.mvp.entity.UserAgentParam;
import com.yuanxin.clan.mvp.utils.CommonString;import java.util.ArrayList;
import java.util.List;public class X5WebView extends WebView {private ProgressBar progressbar;  //进度条private int progressHeight = 10;  //进度条的高度,默认10pxTextView title;private ActionMode mActionMode;private List<String> mActionList = new ArrayList<>();private WebViewClient client = new WebViewClient() {/*** 防止加载网页时调起系统浏览器*/public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}// 重写 WebViewClient  的  shouldInterceptRequest ()// API 21 以下用shouldInterceptRequest(WebView view, String url)// API 21 以上用shouldInterceptRequest(WebView view, WebResourceRequest request)// 下面会详细说明// API 21 以下用shouldInterceptRequest(WebView view, String url)
//        @Override
//        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
//            if (url.contains("jquery.js"))
//       {
//          String localPath = url.replaceFirst("^http.*[tag]\\]", "");
//          try
//          {
//             InputStream is = getContext().getAssets().open(localPath);
//             String mimeType = "text/javascript";
//             if (localPath.endsWith("css"))
//             {
//                mimeType = "text/css";
//             }
//             return new WebResourceResponse(mimeType, "UTF-8", is);
//          }
//          catch (Exception e)
//          {
//             e.printStackTrace();
//          }
//       }
//            return super.shouldInterceptRequest(view, url);
//        }
//
//
//        // API 21 以上用shouldInterceptRequest(WebView view, WebResourceRequest request)
//        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
//        @Override
//        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
//            String url = request.getUrl().toString();
//            if (url.contains("[tag]"))
//            {
//                String localPath = url.replaceFirst("^http.*[tag]\\]", "");
//                try
//                {
//                    InputStream is = getContext().getAssets().open(localPath);
//                    String mimeType = "text/javascript";
//                    if (localPath.endsWith("css"))
//                    {
//                        mimeType = "text/css";
//                    }
//                    return new WebResourceResponse(mimeType, "UTF-8", is);
//                }
//                catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
//            }
//            return super.shouldInterceptRequest(view, request);
//        }};public OnScrollListener listener;/*** This is called in response to an internal scroll in this view (i.e., the* view scrolled its own contents). This is typically as a result of* {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been* called.** @param l Current horizontal scroll origin.* @param t Current vertical scroll origin.* @param oldl Previous horizontal scroll origin.* @param oldt Previous vertical scroll origin.*/@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (listener != null){if (t - oldt <= 2){listener.onScrollDown();}if(oldt - t >= 2) {listener.onScrollUp();}listener.scrollHeight(t);}}public void setListener(OnScrollListener listener){this.listener = listener;}public interface OnScrollListener{void onScrollUp();//上滑void onScrollDown();//下滑void scrollHeight(int h);}//这两个方法会在用户长按选择web文本时,在弹出菜单前被调用。@Overridepublic ActionMode startActionMode(ActionMode.Callback callback) {ActionMode actionMode = startActionMode(callback);Log.e("hxw", actionMode.toString());return resolveActionMode(actionMode);}@Overridepublic ActionMode startActionMode(ActionMode.Callback callback, int type) {ActionMode actionMode = startActionMode(callback, type);Log.e("hxw", actionMode.toString() + " " + type);return resolveActionMode(actionMode);}//处理item,处理点击private ActionMode resolveActionMode(ActionMode actionMode) {if (actionMode != null) {final Menu menu = actionMode.getMenu();mActionMode = actionMode;menu.clear();Log.e("hxw", mActionList.toString());for (int i = 0; i < mActionList.size(); i++) {menu.add(mActionList.get(i));}for (int i = 0; i < menu.size(); i++) {MenuItem menuItem = menu.getItem(i);menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {// getSelectedData((String) item.getTitle());// releaseAction();return true;}});}}mActionMode = actionMode;return actionMode;}//设置弹出action列表public void setActionList(List<String> actionList) {mActionList = actionList;}@SuppressLint("SetJavaScriptEnabled")public X5WebView(Context arg0, AttributeSet arg1) {super(arg0, arg1);initWebViewSettings(arg0);this.setWebViewClient(client);this.setWebChromeClient(new MyWebChromeClient(arg0, progressbar));// this.setWebChromeClient(chromeClient);// WebStorage webStorage = WebStorage.getInstance();this.getView().setClickable(true);}public void setProgressbarDrawable(Drawable d) {progressbar.setProgressDrawable(d);}private void initWebViewSettings(Context context) {//创建进度条progressbar = new ProgressBar(context, null,android.R.attr.progressBarStyleHorizontal);//设置加载进度条的高度progressbar.setLayoutParams(new AbsoluteLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, progressHeight, 0, 0));Drawable drawable = context.getResources().getDrawable(R.drawable.progressbar_blue);progressbar.setProgressDrawable(drawable);//添加进度到WebViewaddView(progressbar);WebSettings webSetting = this.getSettings();webSetting.setJavaScriptEnabled(true);webSetting.setJavaScriptCanOpenWindowsAutomatically(true);webSetting.setAllowFileAccess(true);webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);webSetting.setSupportZoom(true);webSetting.setBuiltInZoomControls(true);webSetting.setUseWideViewPort(true);webSetting.setSupportMultipleWindows(true);// webSetting.setLoadWithOverviewMode(true);webSetting.setAppCacheEnabled(false);// webSetting.setDatabaseEnabled(true);webSetting.setDomStorageEnabled(true);webSetting.setGeolocationEnabled(true);webSetting.setAppCacheMaxSize(Long.MAX_VALUE);webSetting.setTextSize(WebSettings.TextSize.NORMAL);// webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);// webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);webSetting.setDisplayZoomControls(false);this.setWebContentsDebuggingEnabled(true);String ua = webSetting.getUserAgentString();UserAgentParam up = new UserAgentParam(CommonString.appTag, UserNative.getId(), UserNative.getName(), UserNative.getPhone(), UserNative.getPwd(), UserNative.getEpId());webSetting.setUserAgent(ua + "&" + FastJsonUtils.toJSONString(up));webSetting.setLoadsImagesAutomatically(true);Log.e("hxw", webSetting.getUserAgentString());// this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension// settings 的设计}// @Override
// protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
//    boolean ret = super.drawChild(canvas, child, drawingTime);
//    canvas.save();
//    Paint paint = new Paint();
//    paint.setColor(0x7fff0000);
//    paint.setTextSize(24.f);
//    paint.setAntiAlias(true);
//    if (getX5WebViewExtension() != null) {
//       canvas.drawText(this.getContext().getPackageName() + "-pid:"
//             + android.os.Process.myPid(), 10, 50, paint);
//       canvas.drawText(
//             "X5  Core:" + QbSdk.getTbsVersion(this.getContext()), 10,
//             100, paint);
//    } else {
//       canvas.drawText(this.getContext().getPackageName() + "-pid:"
//             + android.os.Process.myPid(), 10, 50, paint);
//       canvas.drawText("Sys Core", 10, 100, paint);
//    }
//    canvas.drawText(Build.MANUFACTURER, 10, 150, paint);
//    canvas.drawText(Build.MODEL, 10, 200, paint);
//    canvas.restore();
//    return ret;
// }/*public X5WebView(Context arg0) {super(arg0);setBackgroundColor(85621);}*/
}

webview 监听滑动距离

        mWebview.setListener(new X5WebView.OnScrollListener() {@Overridepublic void onScrollUp() {Logger.e("up");}@Overridepublic void onScrollDown() {Logger.e("down");}@Overridepublic void scrollHeight(int h) {if (h > 0) {headLayout.setBackgroundResource(R.color.epblueyl);//标题栏颜色渐变}float f = (h + 0f) / 450;//滑动距离450pxif (f > 1) {f = 1f;}if (f < 0) {f = 0;}
//                headLayout.setAlpha(f*1);headLayout.setBackgroundColor(ColorUtils.changeAlpha(ContextCompat.getColor(YxServiceActivity.this, R.color.epblueyl),(int)(f * 1 * 0xff)));}});

通用标题栏渐变色demo:https://blog.csdn.net/meixi_android/article/details/78124913

实现效果:渐变显示

demo链接:https://download.csdn.net/download/meixi_android/10966003

demo云盘链接:https://pan.baidu.com/s/1bNGSzYitvXTUk4x_3PgPWw

云盘密码:回复QQ——1085220040

android 监听webView滑动距离和标题栏颜色渐变相关推荐

  1. Android监听ScrollView滑动到顶端和底部

    package cn.testscrollview; import android.os.Bundle; import android.view.MotionEvent; import android ...

  2. Android监听WebView网页跳转,webview怎么监听跳转到二级页面啊?

    onReady() { // #ifdef APP-PLUS var currentWebview = this.$mp.page.$getAppWebview(); //获取当前页面的webview ...

  3. 获取listView上滑距离,根据这个距离改变标题栏颜色

    1.继承ListView 2.重写onScrollchanged方法,在这个方法中调接口,在主页面实现接口 3.得到liostView的滑动距离,不断在onScrollchanged方法中调用 下面是 ...

  4. Android中监听ScrollView滑动停止和滑动到底部

    1.监听ScrollView滑动停止: [java] view plaincopy /********************监听ScrollView滑动停止********************* ...

  5. android SwipeRefreshLayout嵌套Webview滑动冲突问题解决

    最近有一个需求,需要在网页中实现下拉刷新功能,这里遇到一个坑,加载网页的时候webview向上滑动不了了,看了一下网上的资料尝试过后都没有用,所以在这里做一下记录,希望可以帮到大家,由于两个控件都有滑 ...

  6. 仿网易云音乐日推界面(监听AppBarLayout滑动+动态高斯模糊)

    首先来看下日推界面的效果: 网易云音乐日推界面的亮点就是在上拉的时候,banner页面逐渐模糊.字体透明度下降,最后左上角显示出"每日推荐"的字体. 这个界面用户会觉得很清晰.便捷 ...

  7. 如何监听WebView完成加载URL?

    我有一个从Internet加载页面的WebView . 我想在加载完成之前显示ProgressBar . 我如何监听WebView的页面加载完成? #1楼 我非常偏爱@NeTeInStEiN(和@po ...

  8. puppeteer 鼠标定位滑块_监听滑块滑动或滑块随鼠标移动 | JShare

    监听滑块滑动或滑块随鼠标移动 | JShare fork from "Column with rotated labels" var data = [29,12,32,28,22, ...

  9. JS实现滚动监听以及滑动到顶部

    效果图: <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8& ...

最新文章

  1. DevExpress的TreeList怎样给树节点设置图标
  2. Linux C 的风格
  3. 关于JSON.parse()方法的使用说明
  4. kata_小规模流处理kata。 第2部分:RxJava 1.x / 2.x
  5. c# 声明类的时候初始化类_使用C#初始化的列表声明
  6. 【指针】C++中指针的使用艺术[转]
  7. 《Adobe Photoshop CS6中文版经典教程(彩色版)》—第1课1.4节在Photoshop中还原操作...
  8. 只有程序猿才能看懂的段子,不笑你拿小拳拳捶我!!!
  9. [ZT]调侃、反讥与诡辩
  10. 怎么把dicom数据转成nifty数据
  11. 51单片机(STC)串口无阻塞发送函数
  12. IDB数据桥提供方用oracle,WinCC与第三方系统数据交互IndustrialDataBridge(54页)-原创力文档...
  13. Exception和继承自Exception的RuntimeException区别
  14. Web网站架构演变历程
  15. 文件传输 SSHSecureShellClient-3.2.9 |CSDN创作打卡
  16. 用python3+ PyQt5写一个NFC模块的串口小工具的一星期
  17. “谷歌百度一键搜索” -- 最方便的Chrome搜索插件
  18. hr 0x800401f0 尚未调用 CoInitialize
  19. 【Linux】必备故障及原因50个
  20. 面试系列--自我了解和自我剖析

热门文章

  1. [css] 使用css实现彩虹的效果
  2. 工作325:uni-格式化日期
  3. 前端学习(2476):表单数据绑定处理
  4. 前端学习(1620):前端系列实战课程之提取行间样式
  5. 前端学习(1499):组件特点
  6. 华为IoT平台NB编解码插件开发详细教程【下篇】
  7. 微信小程序几种常用弹窗提示
  8. linux 2.6 hash表作用,高性能分布式哈希表FastDHT介绍及安装配置
  9. java pdf文件压缩_PDF文件压缩转换教程
  10. android 进度条_Android仿水波纹流球进度条控制器,实现高端大气的主流特效