带有进度条的WebView
2024-08-24 08:19:14
带有进度条的WebView
本篇继于WebView的使用
效果图
自定义一个带有进度条的WebView
package com.kongqw.kbox.view;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.widget.ProgressBar;
import com.kongqw.kbox.R;
import com.kongqw.kbox.listener.WebChromeClientListener;
/**
* Created by kongqw on 2016/3/7.
* 带有进度条的WebView
*/
public class ProgressWebView extends WebView {
private ProgressBar progressbar;
private WebChromeClientListener mWebChromeClientListener;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// 创建一个进度条
progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
// 设置进度条参数(位置)
progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10, 0, 0));
// 添加一个自定义的样式,系统默认的样式上下有边距
Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);
progressbar.setProgressDrawable(drawable);
// 添加进度条
addView(progressbar);
WebChromeClient webChromeClient = new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
// 获取到Title
if (null != mWebChromeClientListener) {
mWebChromeClientListener.onReceivedTitle(title);
}
}
};
// 给WebView添加监听
setWebChromeClient(webChromeClient);
//是否可以缩放
getSettings().setSupportZoom(true);
getSettings().setBuiltInZoomControls(true);
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// newProgress 加载进度
if (newProgress == 100) {
progressbar.setVisibility(GONE);
} else {
if (progressbar.getVisibility() == GONE) {
progressbar.setVisibility(VISIBLE);
}
progressbar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
lp.x = l;
lp.y = t;
progressbar.setLayoutParams(lp);
super.onScrollChanged(l, t, oldl, oldt);
}
public void setWebChromeClientListener(WebChromeClientListener listener) {
mWebChromeClientListener = listener;
}
}
WebChromeClientListener
package com.kongqw.kbox.listener;
/**
* Created by kongqw on 2016/3/7.
*/
public interface WebChromeClientListener {
public void onReceivedTitle(String title);
}
进度条样式(progress_bar_states.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景 -->
<item android:id="@android:id/background">
<shape>
<corners android:radius="2dp" />
<gradient
android:angle="270"
android:centerColor="#E3E3E3"
android:endColor="#E6E6E6"
android:startColor="#C8C8C8" />
</shape>
</item>
<!-- 进度条 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="2dp" />
<gradient
android:centerColor="#4AEA2F"
android:endColor="#31CE15"
android:startColor="#5FEC46" />
</shape>
</clip>
</item>
</layer-list>
布局
<包名.view.ProgressWebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title" />
使用
使用和WebView一样,参考WebView的使用即可
最新文章
- [Python基础知识]正则
- oracle存储过程、函数、序列、包
- 万恶的hao123
- [转载]java的几种对象(PO,VO,DAO,BO,POJO)解释
- 自旋锁spin_lock和raw_spin_lock
- c#浅谈反射内存的处理
- 从Lumia退役看为什么WP走向没落(从程序员与市场开发的角度,讲的真棒!)
- CXF之webservice
- linux查看系统的日志的一些实用操作
- SharePoint 2013 配置InfoPath 列表表单
- JAVA 调用 R 语言之升华篇
- 计算机基础之Windows10操作系统安装U盘制作
- 《我是一只IT小小鸟读后感》
- Hadoop 2.x 版本的单机模式安装
- 如何查看哪个进程,使用了哪个CPU
- WritePrivateProfileString GetPrivateProfileString 读取写 配置文件
- thinkphp---Excel导入!
- D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
- oracle/ms sql 系统表
- Android 解决NestedScrollView 嵌套 RecyclerView出现的卡顿,上拉刷新无效
热门文章
- jacascript 原生选项卡插件
- .Net Core 学习之路-基础
- 我花了 8 小时,";掌握";了一下 Flutter | Flutter 中文站上线
- [LeetCode] Cut Off Trees for Golf Event 为高尔夫赛事砍树
- [LeetCode] Minimum Factorization 最小因数分解
- HTTP你真的懂了吗?
- Oracle12c功能增强新特性之维护&;amp;升级&;amp;恢复&;amp;数据泵等
- pymysql实现从a表过滤出有效信息添加至b表
- [USACO09FEB]庙会班车Fair Shuttle
- bzoj 4894: 天赋