没有指南,因为没有单一/首选的方法来做到这一点。以下是帮助您入门的几个步骤:
1)创建足够大的aCanvas
和 a以包含您的自定义进度条:Bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
2) 确保并发症有要显示的数据,并且是范围值并发症。(您可以从该方法访问并发症数据onComplicationDataUpdate(int complicationId, ComplicationData complicationData)
。):
if(complicationData != null && complicationData.getType() == ComplicationData.TYPE_RANGED_VALUE) {
// TODO: Get progress data
}
3) 从您的 ComplicationData 对象中获取进度值(所有这些字段都是必需的):
float minValue = complicationData.getMinValue();
float maxValue = complicationData.getMaxValue();
float currentValue = complicationData.getValue();
4) 以任何你想要的方式在你的Canvas
. 下面是我们其中一个表盘的简化示例。
// Calculate the start angle based on the complication ID.
// Don't worry too much about the math here, it's very specific to our watch face :)
float startAngle = 180f + 22.5f + ((complicationId - 2) * 45f);
// Calculate the maximum sweep angle based on the number of complications.
float sweepAngle = 45;
// Translate the current progress to a percentage value between 0 and 1.
float percent = 0;
float range = Math.abs(maxValue - minValue);
if (range > 0) {
percent = (currentValue - minValue) / range;
// We don't want to deal progress values below 0.
percent = Math.max(0, percent);
}
// Calculate how much of the maximum sweep angle to show based on the current progress.
sweepAngle *= percent;
// Add an arc based on the start and end values calculated above.
Path progressPath = new Path();
progressPath.arcTo(getScreenRect(), startAngle, sweepAngle);
// Draw it on the canvas.
canvas.drawPath(progressPath, getProgressPaint());
这是最终结果: