編輯:關於 Android 程式設計
本文拆解一個可直接複用的線性載入進度條元件,效果就是我們在瀏覽器上常見的那種細長進度條。

<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 背景 -->
<item android:id="@android:id/background">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
<!-- 進度 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@android:color/holo_green_light" />
</shape>
</clip>
</item>
</layer-list>
這裡刻意把背景設為透明,讓進度條看起來像是浮在網頁頂端,而不是壓出一條實色帶。
@SuppressWarnings("deprecation")
public class ProgressWebView extends WebView {
private final static String TAG = ProgressWebView.class.getSimpleName();
private ProgressBar progressBar;
private Context context;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
progressBar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
// 高度 3px、貼齊頂端
progressBar.setLayoutParams(new AbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.MATCH_PARENT, 3, 0, 0));
progressBar.setProgressDrawable(
getResources().getDrawable(R.drawable.wevbview_progressbar));
addView(progressBar);
setWebChromeClient(new WebChromeClient());
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
Log.d(TAG, "newProgress" + newProgress);
if (newProgress == 100) {
progressBar.setVisibility(GONE);
} else {
if (progressBar.getVisibility() == GONE) {
progressBar.setVisibility(VISIBLE);
}
progressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
// 攔截 console.log
@Override
public boolean onConsoleMessage(ConsoleMessage cm) {
android.util.Log.d(TAG, "webview console " + cm.lineNumber()
+ " of " + cm.sourceId() + " : " + cm.message());
return true;
}
// 攔截 alert()
@Override
public boolean onJsAlert(WebView view, String url,
String message, JsResult result) {
result.cancel();
return true;
}
}
// 進度條隨內容位置同步移動
@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);
}
}
<com.etoury.webviewprogress.ProgressWebView
android:id="@+id/them_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ProgressWebView webView = (ProgressWebView) findViewById(R.id.them_webview);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
if (url != null && url.startsWith("https://")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
});
webView.loadUrl("https://www.cnblogs.com/hubli/p/4835549.html");
整體結構不複雜,重點是兩件事:onProgressChanged() 負責同步進度並在 100% 時隱藏進度條,onScrollChanged() 則讓進度條永遠貼在內容頂端。若之後想改成圓形或加上淡出動畫,只要替換 drawable 或在隱藏時套用 alpha 動畫即可。
Android 切圖注意事項:多解析度、9Patch 與標註規範一次懂
做 Android 版本設計時尺寸繁多,切圖是介面還原的關鍵環節。以下整理切圖時的注意事項與規範。 1. 切圖需要兩套解析度的圖:480*800 與 720*1280,
Android Studio 系列教程(一):從下載安裝到第一個 HelloWorld
背景 Android Studio 是 Google 在 2013 年 I/O 大會上為 Android 開發推出的新一代 IDE,基於 IntelliJ IDEA 改造
Android 逆向入門:用 Xposed Hook 攔截與修改方法
前言Xposed 是 Android 逆向分析中常見的框架,能在不修改 APK 的前提下攔截方法執行。本文以簡單登入 App 為例,說明如何建立 Xposed 模組,攔截
從源碼看 Android Scroller 的平滑滾動實作原理
研究過 Launcher 的朋友對 Scroller 應該不陌生。它是 Android 對「滾動」這件事的封裝類,能讓一個 View 在指定時間內從 A 點平滑移動到 B