編輯:關於 Android 程式設計

一個橫向進度條,已完成區域上有一道滑塊從左往右持續移動(當初就是被這個效果吸引,才想弄清楚它怎麼做的);進度條中央顯示百分比,而且文字落在已下載區域的部分是白色;點擊可暫停,此時進度條顏色與提示文字都會改變。
邊框、進度填充這些都很直觀,值得先想清楚的是這兩件事。
這與歌詞逐字變色的原理相同。Canvas 提供 save() 搭配 CLIP_SAVE_FLAG 標誌,其含義是「restore() 被呼叫時還原当前的 clip 區域」;配合 clipRect() 與 restore(),就能只對部分區域重繪文字。
直覺做法是畫一個滑塊(其實就是一張圖片),再依進度不斷改變它的位置。要注意它的移動特性:滑塊從右邊界進入、最後由左邊界離開,而且只在下載完成區域內可見,超出右邊界的部分必須被裁掉。這正是兩個圖層重疊時的合成問題,對應的 Porter-Duff 模式是 SRC_ATOP。

private void initAttrs(AttributeSet attrs) {
if (attrs != null) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.FlikerProgressBar);
textSize = (int) ta.getDimension(R.styleable.FlikerProgressBar_textSize, dp2px(12));
loadingColor = ta.getColor(R.styleable.FlikerProgressBar_loadingColor, Color.parseColor("#40c4ff"));
stopColor = ta.getColor(R.styleable.FlikerProgressBar_stopColor, Color.parseColor("#ff9800"));
ta.recycle();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
int height;
switch (heightSpecMode) {
case MeasureSpec.AT_MOST:
height = (int) dp2px(DEFAULT_HEIGHT_DP);
break;
case MeasureSpec.EXACTLY:
case MeasureSpec.UNSPECIFIED:
default:
height = heightSpecSize;
break;
}
setMeasuredDimension(widthSpecSize, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBorder(canvas); // 1. 邊框
drawProgress(); // 2. 進度(含滑塊)
canvas.drawBitmap(pgBitmap, 0, 0, null);
drawProgressText(canvas); // 3. 進度文字
drawColorProgressText(canvas); // 4. 文字變色
}
邊框就是一個 STROKE 風格的矩形。
private void drawBorder(Canvas canvas) {
bgPaint.setStyle(Paint.Style.STROKE);
bgPaint.setColor(progressColor);
bgPaint.setStrokeWidth(dp2px(1));
canvas.drawRect(0, 0, getWidth(), getHeight(), bgPaint);
}
進度部分先建立一張與已完成區域同寬的 Bitmap 當作畫布,塗滿進度色,再用 SRC_ATOP 把滑塊圖疊上去——這樣滑塊只會出現在已完成區域內。
private void drawProgress() {
bgPaint.setStyle(Paint.Style.FILL);
bgPaint.setStrokeWidth(0);
bgPaint.setColor(progressColor);
float right = (progress / MAX_PROGRESS) * getMeasuredWidth();
pgBitmap = Bitmap.createBitmap((int) Math.max(right, 1),
getMeasuredHeight(), Bitmap.Config.ARGB_8888);
pgCanvas = new Canvas(pgBitmap);
pgCanvas.drawColor(progressColor);
if (!isStop) {
bgPaint.setXfermode(xfermode); // SRC_ATOP
pgCanvas.drawBitmap(flikerBitmap, flickerLeft, 0, bgPaint);
bgPaint.setXfermode(null);
}
}
文字先用進度色畫一遍,再用白色重畫一次,但這次限制在裁剪區域內。
private void drawProgressText(Canvas canvas) {
textPaint.setColor(progressColor);
progressText = getProgressText();
textPaint.getTextBounds(progressText, 0, progressText.length(), textBouds);
float xCoordinate = (getMeasuredWidth() - textBouds.width()) / 2;
float yCoordinate = (getMeasuredHeight() + textBouds.height()) / 2;
canvas.drawText(progressText, xCoordinate, yCoordinate, textPaint);
}
private void drawColorProgressText(Canvas canvas) {
textPaint.setColor(Color.WHITE);
float xCoordinate = (getMeasuredWidth() - textBouds.width()) / 2;
float yCoordinate = (getMeasuredHeight() + textBouds.height()) / 2;
float progressWidth = (progress / MAX_PROGRESS) * getMeasuredWidth();
if (progressWidth > xCoordinate) {
canvas.save(Canvas.CLIP_SAVE_FLAG);
float right = Math.min(progressWidth, xCoordinate + textBouds.width());
canvas.clipRect(xCoordinate, 0, right, getMeasuredHeight());
canvas.drawText(progressText, xCoordinate, yCoordinate, textPaint);
canvas.restore();
}
}

完整專案在 GitHub:https://github.com/LineChen/FlickerProgressBar
回頭看,這個控制項最有意思的地方是它把三個獨立的繪圖技巧疊在一起:裁剪處理文字雙色、Xfermode 處理圖層遮罩、自定義屬性處理外觀。任一個單獨看都不難,組合起來就有了成品感。
事件分發系列:透視 View 的 dispatchTouchEvent 與 onTouchEvent
dispatchTouchEvent廢話不多說,直接上源碼。 /** * Pass the touch screen motion event down to the
我的第一個 Android 項目:偽即時通訊 DemoChat 開發全記錄
學習 Android 到現在大概還不到一個月,我很激動能憑自己的努力與鑽研,做出一個自己想要的 App。雖然它功能還不完善、界面頗為樸素、Bug 也不少,但這畢竟是我親手
Android 4.4 狀態欄透明化與沉浸式效果實作
Android 4.4 之後,系統允許應用程式把狀態欄處理成半透明或全透明,讓下方內容延伸到系統列區域,形成更完整的沉浸式視覺。這種做法常用於首頁、相簿或視差捲動頁面。實
Activity 之間動畫切換學習筆記(一):Transition 框架入門
首先,什麼是 Transition? Android 5.0 中 Activity 與 Fragment 的變換是建立在名為 Transitions 的新特性之上。這個