【问题标题】:Android Wear - how to draw custom UI for complication TYPE_RANGED_VALUEAndroid Wear - 如何为复杂功能 TYPE_RANGED_VALUE 绘制自定义 UI
【发布时间】:2018-06-05 11:21:19
【问题描述】:

我浏览了创建支持复杂功能的自定义 Android 2.0 表盘的示例。 ComplicationDrawable 的文档指出我们可以提供自定义进度条并使用 setRangedValueProgressHidden() 来抑制默认 UI。

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

但在将默认进度条设置为隐藏后,我一直无法找到有关如何绘制自定义 UI 的指南。任何指针将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

    1) 创建一个Canvas 和一个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());
    

    这是最终结果:

    【讨论】:

    • 太棒了!!谢谢分享。我会试一试
    • @Toffer 有没有办法扩展 ComplicationDrawable 并覆盖它的 onDraw 方法?我试过了,但这似乎不起作用MyComplicationDrawable tempDrawable = (MyComplicationDrawable) getDrawable(R.drawable.complication_styles); MyComplicationDrawable 是一个空类(目前)扩展 ComplicationDrawable
    • 我不确定你想要达到什么目的。也许最好提出一个新问题并更详细地解释该问题。很高兴看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多