zoukankan      html  css  js  c++  java
  • WebView增加一个水平Progress,位置、长相随意

    实际效果可以参看微信的web页面进度条
    本质就是通过addView及对WebView 页面进度进行监听
    先看看这个自定义的DrawableId,我们参照系统默认实现的方法写一个自己的
     
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
     
        <item android:id="@android:id/background"> //这个属性是指定progress的背景
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#00000000"
                    android:centerColor="#00000000"
                    android:centerY="0.75"
                    android:endColor="#00000000"
                    android:angle="270"
                    />
            </shape>
        </item>
     
     
        <item android:id="@android:id/progress"> //这个属性是指定progress的进度
            <clip>
                <shape>
                    <corners android:radius="5dip" />
                    <gradient
                        android:startColor="#ff7d1a"
                        android:centerColor="#ff7d1a"
                        android:centerY="0.75"
                        android:endColor="#ff7d1a"
                        android:angle="270"
                        />
                </shape>
            </clip>
        </item>
     
    </layer-list>
    实际还有一个secondProgress的属性,这个也随意设置,一般用不到
     
    void init(Context context) {
            if (context == null)
                return;
            Drawable drawable = context.getResources().getDrawable(DrawableId);//该资源以系统原码为参照搞一个自己想要的角度颜色背景
            if (progressbar == null)
                progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);//这里必须要使用系统的attr属性才能正常实例化出来
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams
                    (ViewGroup.LayoutParams.MATCH_PARENT, height);//用相对布局更方便指挥这个progress,这里随意;代码实现的height都为pix级别
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);//这个位置是相对于添加它的父类View
            layoutParams.topMargin = 48;//该属性定义距离父类View顶部多少,pix级别
     
            if (progressbar != null) {
                progressbar.setLayoutParams(layoutParams);
                if (drawable != null) {
                    progressbar.setProgressDrawable(drawable);//将我们自定义的drawable放进来,长相就定了
                    progressbar.setIndeterminate(false);//此值表明进度是不明确的,我们并不知道具体进度是多少
                }
            }
            if (layoutParent == null)
                return;
            viewGroupParent.addView(progressbar);
            if (webView == null)
                return;
            webView.setWebChromeClient(new WebChromeClient());//给webview提供一个可以监听进度的对象,系统内的WebChromeClient 已经有了这个进度通知方法
        }
     
        public class WebChromeClient extends android.webkit.WebChromeClient {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
     
     
                if (newProgress >= 99) {//这个临界值是用来改变显示状态,告诉我们页面加载完了,你progressbar可以隐藏了,此值随意,觉得多少合适就用多少
                    if (progressbar != null)
                        progressbar.setVisibility(View.GONE);
                } else {
                    final int progress = newProgress;//实际能避免新增对象就避免
                    if (progressbar != null && progressbar.getVisibility() != View.VISIBLE)
                        progressbar.setVisibility(View.VISIBLE);
                    if (weakHandler == null)
                        return;
                    handler.post(new Runnable() {//handler的实际意图就是为了避免在非主线程改变UI,如果一定要在子线程改变Ui,记得给子线程一个Looper
                        @Override
                        public void run() {
                            if (progressbar != null) {
                                progressbar.setProgress(progress);
                                progressbar.incrementProgressBy(5);//平缓增加
                            }
                        }
                    });
                }
                super.onProgressChanged(view, newProgress);
            }
     
        }
  • 相关阅读:
    Java.io.outputstream.PrintStream:打印流
    Codeforces 732F. Tourist Reform (Tarjan缩点)
    退役了
    POJ 3281 Dining (最大流)
    Light oj 1233
    Light oj 1125
    HDU 5521 Meeting (最短路)
    Light oj 1095
    Light oj 1044
    HDU 3549 Flow Problem (dinic模版 && isap模版)
  • 原文地址:https://www.cnblogs.com/android-blogs/p/5445491.html
Copyright © 2011-2022 走看看