編輯:關於 Android 程式設計
系統內建的 ProgressBar 樣式有限,若想做出完全貼合設計稿的橫向進度條,最簡單的切入點是「自訂一個容器,讓內部 View 的寬度隨進度改變」。這個做法不需要自繪 Canvas,也不必處理 NinePatch,程式碼量極小。
外層是自訂的 ProgressView(繼承 RelativeLayout),內含一個真正顯示顏色的 View。注意自訂 View 的標籤必須寫成「完整套件名稱.類別名稱」,與 java 檔案的 package 一致,否則 LayoutInflater 找不到類別:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.jltxgcy.progressbar.ProgressView
android:id="@+id/rootview"
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="#d6d6d6" >
<View
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="15dp" />
</com.jltxgcy.progressbar.ProgressView>
</RelativeLayout>
兩個關鍵點:第一,透過 onFinishInflate 取得內部子 View 的參照,這是自訂 ViewGroup 找到子元素的標準時機;第二,setProgress 用 postDelayed 延遲 20 毫秒執行,因為在這個時間點之前 View 尚未完成測量,getWidth() 會回傳 0,算出來的寬度自然全錯。
package com.jltxgcy.progressbar;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
public class ProgressView extends RelativeLayout {
private View mProgress;
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mProgress = findViewById(R.id.progress);
}
public void setBackgroud() {
mProgress.setBackgroundColor(Color.BLUE);
}
public void setProgress(final int progress) {
postDelayed(new Runnable() {
@Override
public void run() {
int width = getWidth();
int height = getHeight();
int curProgress = (int) (progress / 100.0 * width);
LayoutParams params = new LayoutParams(curProgress, height);
mProgress.setLayoutParams(params);
}
}, 20);
}
}
用 LayoutParams 重新指定寬度之所以會生效,是因為 RelativeLayout 會在 requestLayout 之後重新為子 View 配置位置。若換成 LinearLayout 搭配 weight,寫法會更乾淨,也不必延遲取值。
透過 findViewById 取得自訂 View,再用 postDelayed 建立一個每 500 毫秒遞增的迴圈:
package com.jltxgcy.progressbar;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
private ProgressView mProgressView;
private int progressRate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressView = (ProgressView) findViewById(R.id.rootview);
mProgressView.postDelayed(new UpdateProgressBar(), 500);
}
class UpdateProgressBar implements Runnable {
@Override
public void run() {
mProgressView.setBackgroud();
mProgressView.setProgress(progressRate);
progressRate += 2;
mProgressView.postDelayed(new UpdateProgressBar(), 500);
}
}
}
這個實作的進度上限沒有做檢查,progressRate 持續遞增會讓寬度超過父容器。實務上應在 setProgress 裡夾住範圍(例如 Math.max(0, Math.min(100, progress)))。另外,若只是要顯示進度而非自訂外觀,直接繼承 ProgressBar 並覆寫 onDraw,或使用 ClipDrawable 搭配 progressDrawable,都能少寫不少程式碼。
Android AsyncTask 源碼解析:從 execute 到 onPostExecute
一、概述 對做過 Android 的人來說,AsyncTask 是個老朋友。要跑一段耗時工作、跑完再把結果貼回介面上,它確實是一把順手的瑞士刀,也是 Thread + H
Android 常見 UI 元件之 ListView(二):客製化清單與多選、篩選功能
Android 常見 UI 元件之 ListView(二)——客製化 ListView這一篇承接上一篇,示範如何在 ListView 中一次選取多個項目,並實作搜尋篩選的
使用 NDK 與 libjpeg 將 JPEG 顯示到 SurfaceView
為何使用 NDK 與 SurfaceView 影像處理往往需要大量運算與記憶體。若使用 Java 層反覆解碼與縮放,可能會造成效能瓶頸。透過 NDK 使用 C 語言處理
Android 如何導入外部預建數據庫
當 App 需要處理大量數據時,通常會把數據放進數據庫,再透過查詢與修改操作來管理。多數情況下我們在代碼裡即時建庫建表,但也有另一種場景:數據庫本身已經在外部做好並錄入完