編輯:關於 Android 程式設計
先看成果:

目標很明確:在一個圓框內部畫一段會持續增長的扇形。整體可以拆成四個步驟。
新增 ProgressView 繼承自 View,並重寫兩個 constructor。之所以要特別實作兩參數版本,是因為在 XML 中使用自定義控制項時,系統會傳入一個 AttributeSet,若沒有這個 constructor 就會直接崩潰。
接著決定哪些參數要開放給使用者設定。這個範例中開放了圓框顏色、扇形顏色、起始角度、增長步長與內距五項。在 res/values 下新增 attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ProgressView">
<!-- circleColor 圓形邊框顏色;sweepColor 扇形顏色
startAngle 起始角度;sweepStep 每次增長的角度 -->
<attr name="circleColor" format="color|reference"></attr>
<attr name="sweepColor" format="color|reference"></attr>
<attr name="startAngle" format="integer"></attr>
<attr name="sweepStep" format="integer"></attr>
<attr name="padding" format="integer"></attr>
</declare-styleable>
</resources>
declare-styleable 的 name 是為了後續透過 R.styleable 取得 AttributeSet 時使用;attr 的 name 則是我們對外開放、可在 XML 中直接設定的屬性名稱,用法等同內建的 android:xxx。format 用來宣告該屬性接受哪一種型別的值。
package com.yztc.customprogressview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class ProgressView extends View {
private int sweepStep = 10; // 扇形每次增長的角度
private int padding = 40; // 外框與扇形之間的間距
private int circleColor = Color.GRAY; // 邊框顏色
private int sweepColor = Color.BLUE; // 扇形顏色
private int startAngle = 90; // 起始角度
private int storke = 20; // 外框粗細
private int sweepAngle = 0; // 目前掃過的角度
private static final int DEFAULT_WIDTH = 200;
private static final int DEFAULT_HEIGHT = 200;
public ProgressView(Context context) {
super(context);
}
// 在 XML 中使用自定義屬性時,系統會傳入 AttributeSet
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
if (array != null) {
sweepStep = array.getInteger(R.styleable.ProgressView_sweepStep, sweepStep);
padding = array.getInteger(R.styleable.ProgressView_padding, padding);
circleColor = array.getColor(R.styleable.ProgressView_circleColor, circleColor);
sweepColor = array.getColor(R.styleable.ProgressView_sweepColor, sweepColor);
startAngle = array.getInteger(R.styleable.ProgressView_startAngle, startAngle);
array.recycle(); // 記得回收
}
}
@Override
protected void onDraw(Canvas canvas) {
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
// 先畫外層圓框
mPaint.setColor(circleColor);
mPaint.setStrokeWidth(storke);
mPaint.setStyle(Paint.Style.STROKE); // 空心圓
// 圓心取寬度的一半,半徑扣掉一半線寬讓邊線不被裁切
canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2 - storke / 2, mPaint);
// 再畫內部扇形
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(sweepColor);
/* drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
* oval 指定弧的外接矩形,決定圓心與半徑
* startAngle 起始角度
* sweepAngle 掃過的角度,順時針為正
* useCenter true 表示連圓心一起填,也就是扇形;false 則只是弧線
*/
RectF rectF = new RectF(padding + storke, padding + storke,
getWidth() - padding - storke, getWidth() - padding - storke);
canvas.drawArc(rectF, startAngle, sweepAngle, true, mPaint);
sweepAngle += sweepStep;
sweepAngle = sweepAngle > 360 ? 0 : sweepAngle;
invalidate(); // 請求重繪,形成連續動畫
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
int wSize = MeasureSpec.getSize(widthMeasureSpec);
int hSize = MeasureSpec.getSize(heightMeasureSpec);
// 因為畫的是圓,只要看寬度這邊即可
switch (wMode) {
case MeasureSpec.AT_MOST: // layout_width="wrap_content"
float density = getResources().getDisplayMetrics().density;
wSize = (int) (DEFAULT_WIDTH * density);
hSize = (int) (DEFAULT_HEIGHT * density);
break;
case MeasureSpec.EXACTLY: // match_parent 或明確數值
wSize = hSize = Math.min(wSize, hSize); // 取短邊,保證是正圓
break;
}
setMeasuredDimension(wSize, hSize); // 未呼叫會拋例外
}
}
幾個座標計算的說明:外框用 drawCircle,圓心取 getWidth()/2 即可。內部扇形的外接矩形用 padding 加上 storke 決定左上點,右下點則是總寬度扣掉 storke 與 padding。把這四個值畫成草圖會立刻清楚。
onMeasure 中取寬高的最小值,是為了保證在 EXACTLY 模式下不會被拉成橢圓。重寫 onMeasure 後一定要呼叫 setMeasuredDimension,否則會直接報錯。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:app="https://schemas.android.com/apk/res-auto"
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="com.yztc.customprogressview.MainActivity">
<com.yztc.customprogressview.ProgressView
android:id="@+id/pv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
app:padding="20"
app:circleColor="#aa0000"
app:sweepColor="#00aa00"
app:sweepStep="1"
app:startAngle="180" />
</RelativeLayout>
根節點底下必須加上 xmlns:app="https://schemas.android.com/apk/res-auto",才能使用 attrs 中自定義的屬性,寫法為 app:屬性名稱。這個命名空間前綴不一定要叫 app,只要與 xmlns 後面的字串一致即可。
Canvas.drawCircle 畫出來的实际半径,是你指定的半徑再加上一半的線寬。也就是說邊框 10、半徑 50 時,空心部分的半徑其實是 55。如果不把這點算進去,圓的四個切點會與其他位置對不齊,看起來像有鋸齒。

以上就是自定義圓形 Progress 的完整實作。這個範例的重點其實不在畫圓,而在於自定義屬性的完整流程:定義 declare-styleable、在 constructor 中用 TypedArray 取值、記得 recycle,最後在佈局中用自訂命名空間套用。掌握這條鏈路之後,要做更複雜的自定義控制項都只是同樣的模式。
手機迅雷怎麼關閉朋友圈 個人中心設定教學
手機迅雷的朋友圈功能並非人人愛用,想關掉的朋友可以按以下步驟操作。 1、打開手機迅雷,切換到「我的迅雷」個人中心,點擊左上角【登錄】,已登錄可直接跳過。 2、在個人中心
Android Notification 通知提醒實作詳解:從常駐通知到自訂樣式
需要跟使用者溝通「一件重要但不急著看的事」時,Toast 並不合適——它兩秒就消失,無法回看。這正是 Notification 的價值所在:它會常駐在狀態欄,使用者下拉即
Android 中實現圖片的平移、縮放、旋轉同步進行
前言 之前因為專案需求,用到了圖片的單擊顯示/取消以及平移縮放功能,前幾天突然想再加個旋轉功能,看了網上不少例子,卻沒找到能同時滿足我所有需求的。我的需求是:(1)圖片經
SwipeListView 實作:打造類 QQ 的滑動側邊操作列表
左右滑動列表項、露出背後的操作按鈕,這種互動從 QQ 之後幾乎成了清單的標準語彙。自己從 ListView 的 onTouch 刻一套並不划算,社群已有成熟方案:47de