Android Wear - 如何为复杂功能TYPE_RANGED_VALUE绘制自定义UI

时间:2018-06-05 11:21:20

标签: android android-wear-2.0 android-wear-complication

我已经完成了创建自定义Android 2.0表盘的示例,并支持并发症。 ComplicationDrawable的文档声明我们可以提供自定义进度条并使用setRangedValueProgressHidden()来禁止默认UI。

  

不保证显示可选字段。如果要绘制自己的进度条,可以使用setRangedValueProgressHidden()方法隐藏ComplicationDrawable类提供的进度条。

但是在将默认进度条设置为隐藏后,我无法找到有关如何绘制自定义UI的指南。任何指针都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

没有指南,因为没有一种/首选方法可以做到这一点。以下是帮助您入门的几个步骤:

1)创建一个足以包含自定义进度条的CanvasBitmap

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());

这是最终结果:

Portions watch face