ProgressWebView.java 2.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
package com.ydl.webview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ProgressBar;
import com.tencent.smtt.sdk.WebView;
import com.ydl.ydlcommon.utils.ModularUtils;

/**
 * Created by jinkai on 16/5/9.
 */
public class ProgressWebView extends WebView {
    protected ProgressBar progressbar;

    protected String Title;

    public ProgressWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        try {
            progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
            progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 5));
konghaorui committed
28
            progressbar.setProgressDrawable(getResources().getDrawable(R.drawable.platform_web_progressbar));
29 30
            LayerDrawable drawable = (LayerDrawable) progressbar.getProgressDrawable();
            GradientDrawable gradientDrawable = new GradientDrawable();
konghaorui committed
31
            gradientDrawable.setColor(ContextCompat.getColor(context,ModularUtils.Companion.isYDL()? com.ydl.ydlcommon.R.color.platform_google_green : com.ydl.ydlcommon.R.color.platform_google_yellow));
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            ClipDrawable clipDrawable = new ClipDrawable(gradientDrawable, Gravity.START, ClipDrawable.HORIZONTAL);
            drawable.setDrawableByLayerId(android.R.id.progress, clipDrawable);
            drawable.setDrawableByLayerId(android.R.id.secondaryProgress, clipDrawable);


            addView(progressbar);
            setProgressbar(progressbar);
            setWebChromeClient(new WebChromeClient());
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
    }

    public class WebChromeClient extends com.tencent.smtt.sdk.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            Title = title;
        }


    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public ProgressBar getProgressbar() {
        return progressbar;
    }

    public void setProgressbar(ProgressBar progressbar) {
        this.progressbar = progressbar;
    }
}