編輯:Android 資訊
偶然看到一個圓形進度效果的控件,感覺清爽,於是決定自己實現一版。最終效果如下:

整個思路其實是用「遮擋」模擬進度填充,拆成六步:
在 values 目錄下新建 attrs.xml,聲明圓的背景色與半徑兩個屬性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="circlecolor" format="color" />
<attr name="half" format="dimension" />
<declare-styleable name="myCircleImage">
<attr name="circlecolor" />
<attr name="half" />
</declare-styleable>
</resources>import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
public class CirclePro extends View {
private Paint paint;
private int circleBack; // 圓的背景色
private int mschedual = 0; // 控制動態變化的進度值
float circleHalf; // 圓的半徑
String percent = ""; // 繪製百分比的字符串
@SuppressLint("Recycle")
public CirclePro(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
TypedArray array = context.getTheme()
.obtainStyledAttributes(attrs, R.styleable.myCircleImage, defStyleAttr, 0);
// 注意這裡是 R.styleable.myCircleImage_circlecolor,而不是 R.attr.circlecolor
circleBack = array.getColor(R.styleable.myCircleImage_circlecolor, Color.GREEN);
circleHalf = array.getDimension(R.styleable.myCircleImage_half, 200.f);
}
/** 布局文件中引用該 view 時,必須重寫這個構造函數 */
public CirclePro(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/** 測量文本寬度,用於讓百分比文字水平居中 */
public float getTextWidth(String text, float textSize) {
TextPaint textPaint = new TextPaint();
textPaint.setTextSize(textSize);
return textPaint.measureText(text);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float height = getHeight();
float width = getWidth();
// 1. 實心圓
paint.setColor(circleBack);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(width / 2, height / 2, circleHalf, paint);
// 2. 白色矩形遮擋,矩形底邊隨進度上移
if (mschedual <= 100) {
paint.setColor(Color.WHITE);
canvas.drawRect(width / 2 - circleHalf, height / 2 - circleHalf,
width / 2 + circleHalf,
height / 2 + circleHalf - mschedual * circleHalf / 50, paint);
}
// 3. 百分比文字
paint.setColor(Color.BLACK);
paint.setTextSize(30.f);
percent = mschedual + " %";
canvas.drawText(percent,
width / 2 - getTextWidth(percent, 30) / 2,
height / 2 + paint.getTextSize() * 3 / 8, paint);
// 4. 空心圓描邊
paint.setColor(circleBack);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(width / 2, height / 2, circleHalf, paint);
// 5. 遞增並重繪
if (mschedual < 100) {
mschedual++;
invalidate();
}
}
}使用自定義屬性前,先在根布局聲明命名空間:
xmlns:liu="https://schemas.android.com/apk/res/com.example.androidcirclepro"
其中 liu 是隨意命名的前綴,後面跟的是應用包名。完整布局如下:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:liu="https://schemas.android.com/apk/res/com.example.androidcirclepro"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.example.androidcirclepro.CirclePro
android:layout_width="match_parent"
android:layout_height="match_parent"
liu:half="90dp"
liu:circlecolor="#fff0f0" />
</RelativeLayout>至此,一個可配置顏色與半徑的圓形進度條就完成了。核心只有一句話:用一個不斷收縮的白色矩形去「吃」掉實心圓,視覺上就得到了進度填充的效果。
如果希望進度能從外部控制、或者做成順時針扇形填充,可以在後續版本中把 mschedual 改為由 setProgress() 驅動,並用 Canvas.drawArc 替代遮擋方案,效果會更平滑。
優化 Android App 性能的十大技巧
無論錘子還是茄子手機不斷冒出,Android 系統手機的市場佔有率目前來說還是最大的,因此基於 Android 開發的 App 數量也很龐大。那麼,如何才能開發出更高性能
Android 網絡圖片查看顯示的實現方法
前言 許多應用都需要從網路載入圖片並顯示在畫面上。實作時要特別注意執行緒,因為網路請求不能直接放在主線程,否則容易造成 ANR。 畫面元件 範例畫面包含輸入圖片網址的
用 include 標籤拆分 Android 佈局,讓 XML 不再臃腫
做 Android 開發時,幾乎每個人都遇過這樣的場景:好幾個頁面的頂部標題欄、底部功能列長得一模一樣,卻被反覆複製到各個佈局檔案裡。結果就是 XML 動輒幾百行,改一個
Android 模塊化落地實踐:分層設計與路由框架
前言 萬維網發明人 Tim Berners-Lee 談設計原理時說過一句話:「簡單性和模塊化是軟件工程的基石;分佈式和容錯性是互聯網的生命。」模塊化在軟件工程中的地位,由