Android 仿微信加载H5页面进度条实现

前言

AndroidWebView打卡前端页面时受到网路环境,页面内容大小的影响有时候会让用户等待很久。显示一个加载进度条可以提升很大的体验。微信内访问H5页面加载效果不错,效仿着写了一个。

1.实现

1-1.自定义类继承WebView类

class ProgressWebView(context: Context, attr: AttributeSet) : WebView(context, attr) {

    /*xml布局中使用,所以用两个构造参数的构造函数*/

    private var progressBar: ProgressBar? = null

    /*初始化属性操作*/
    init {

        /*设置ProgressBar是横向*/
        progressBar = ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal)

        /*设置进度条属性*/
        progressBar!!.progressDrawable = context.resources.getDrawable(R.drawable.webview_hori_progress)

        /*设置ProgressBar的布局参数*/
        val layoutParams = FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 10, 0)

        /*绑定参数*/
        progressBar!!.layoutParams = layoutParams

       /* 将ProgressBar添加到WebView上 默认头部*/
        addView(progressBar)

        /*设置WebView辅助类WebChromeClient,获取实时加载进度*/
        setWebChromeClient(object : WebChromeClient() {
            override fun onProgressChanged(webview: WebView?, progress: Int) {
                super.onProgressChanged(webview, progress)

                Log.d("progressView", progress.toString())
                if (progress == 100)
                    progressBar!!.visibility = View.GONE
                else {
                    progressBar!!.visibility = View.VISIBLE

                    /*设置进度参数*/
                    progressBar!!.progress = progress

                }
            }

        })

    }
}

看下设置的加载进度条的属性,webview_hori_progress.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!--最下层item属性-->
    <item>
        <shape>
            <!--无圆角-->
            <corners android:radius="0dp" />
            <!--线条颜色-->
            <gradient
                android:endColor="#FFE4E3E3"
                android:startColor="#FFE4E3E3" />
        </shape>
    </item>

    <!--上层item属性-->
    <item>
        <clip>
            <shape>
                <!--无圆角-->
                <corners android:radius="0dip" />
                <!--线条颜色 渐变,由深到浅-->
                <gradient
                    android:centerColor="#96EF1627"
                    android:endColor="#50F53D4B"
                    android:startColor="#FFEF1627" />
            </shape>
        </clip>
    </item>
</layer-list>

1-2.xml 布局中引用

 <com.ypl.csdndemo.ProgressWebView
      android:id="@+id/wvProgress"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

1-3.代码实现

/*android kotlin 的拓展,导入此包 使用到的组件不用findViewById*/
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        /*WebView设置属性*/
        val setting = wvProgress.settings

        /*本地缓存无则网络*/
        setting.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

        /*设置识别javascript代码*/
        setting.javaScriptEnabled = true

        /*纵向scrollbar去除*/
        wvProgress.isVerticalScrollBarEnabled =false

        /*加载页面*/
        wvProgress.loadUrl("https://blog.csdn.net/")
    }
}

2.效果图

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常精美的h5 进度条 |DEMO_jQuery之家-自由分享jQuery、html5、css3的插件库 <!----> .ClassyCountdownDemo { margin:0 auto 30px auto; max-width:800px; width:calc(100%); padding:30px; display:block } #countdown2 { background:#FFF } #countdown3 { background:rgb(52, 73, 94) } #countdown4 { background:#222 } #countdown5 { background:#222 } #countdown6 { background:#222 } #countdown7 { background:#222 } #countdown8 { background:#222 } #countdown9 { background:#FFF } #countdown10 { background:#3498db } jQuery炫酷图片预览Lightbox插件 A jQuery plugin designed to provide gallery view for images jQuery之家 返回下载页 Example $(document).ready(function() { $('#countdown15').ClassyCountdown({ theme: "flat-colors", end: $.now() + 10000 }); $('#countdown16').ClassyCountdown({ theme: "flat-colors-wide", end: $.now() + 10000 }); $('#countdown17').ClassyCountdown({ theme: "flat-colors-very-wide", end: $.now() + 10000 }); $('#countdown18').ClassyCountdown({ theme: "flat-colors-black", end: $.now() + 10000 }); $('#countdown1').ClassyCountdown({ theme: "white", end: $.now() + 645600 }); $('#countdown5').ClassyCountdown({ theme: "white", end: $.now() + 10000 }); $('#countdown6').ClassyCountdown({ theme: "white-wide", end: $.now() + 10000 }); $('#countdown7').ClassyCountdown({ theme: "white-very-wide", end: $.now() + 10000 }); $('#countdown8').ClassyCountdown({ theme: "white-black", end: $.now() + 10000 }); $('#countdown11').ClassyCountdown({ theme: "black", style: { secondsElement: { gauge: { fgColor: "#F00" } } }, end: $.now() + 10000 }); $('#countdown12').ClassyCountdown({ theme: "black-wide", labels: false, end: $.now() + 10000 }); $('#countdown13').ClassyCountdown({ theme: "black-very-wide", labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown14').ClassyCountdown({ theme: "black-black", labelsOptions: { style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown4').ClassyCountdown({ end: $.now() + 10000, labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, hours: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, minutes: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, seconds: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown2').ClassyCountdown({ end: '1388468325', now: '1378441323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown9').ClassyCountdown({ end: '1388468325', now: '1380501323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#1abc9c", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#2980b9", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#8e44ad", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#f39c12", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown10').ClassyCountdown({ end: '1397468325', now: '1388471324', labels: true, labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown3').ClassyCountdown({ end: '1390868325', now: '1388461323', labels: true, labelsOptions: { lang: { days: 'Zile', hours: 'Ore', minutes: 'Minute', seconds: 'Secunde' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' } }, onEndCallback: function() { console.log("Time out!"); } }); }); 如果你喜欢这个插件,那么你可能也喜欢: html5+jquery通过鼠标控制的圆形进度条 jQuery和css3旋钮控制按钮-knobKnob
Android仿微信app的三级页面可以理解为在微信app中点击某个功能后进入的二级页面再进一步进入的页面,主要用于展示某项内容或进行某项操作。 在实现这样的三级页面时,可以采用以下步骤: 1. 定义三级页面布局:根据需要的展示内容和操作按钮,创建一个布局文件,并在其中添加合适的控件,如TextView、Button等。 2. 创建三级页面的Activity类:新建一个Java类,继承自Android的Activity类。在该类中,可以通过findViewById()方法获取布局文件中的控件,并对其进行操作。还可以根据需要添加逻辑代码,比如处理按钮点击事件。 3. 在二级页面中跳转到三级页面:在二级页面的相应点击事件中,通过Intent实现页面跳转功能。使用Intent的putExtra()方法可以传递数据给要跳转的三级页面,以便进行相应的展示或操作。 4. 在三级页面中处理接收到的数据:在三级页面的onCreate()方法中,可以通过Intent的getExtra()方法获取前一个页面传递的数据,并根据需要进行处理和展示。 5. 添加返回功能:在三级页面中,可以通过在布局文件中添加返回按钮,并设置其点击事件为finish()来实现返回上一级页面的功能。 通过以上步骤,即可实现一个简单的Android仿微信app的三级页面。如果需要进一步的功能或优化,还可以对布局文件进行修改,添加更多控件和功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值