自定义视图无法呈现

时间:2018-05-06 10:37:27

标签: android canvas android-canvas

这是我第一次尝试在Android中创建自定义视图。但是,我似乎错过了一些东西,不知道如何调试问题。我已经编写了一个自定义视图来在Android中绘制条形图,其值低于条形图。我在代码中的onDraw方法如下:

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    if (mBars == null) {
        return
    }
    canvas.save()
    val rectBaseLine = measuredHeight * FRACTIONAL_MAX_BAR_HEIGHT + convertDpToPixel(TOP_MARGIN_FOR_BAR, context);
    canvas.translate(0f, rectBaseLine.toFloat())

    mBars.forEach {
        canvas.drawRoundRect(it.mRect,
                BAR_CORNER_RADIUS, BAR_CORNER_RADIUS,
                mapToPaints.getValue(it.paintType))

        var textWidth = textPaint.measureText(it.num.toString())

        canvas.drawText(it.num.toString(),
                it.mRect.left + (it.mRect.right - it.mRect.left) / 2 - textWidth / 2,
                convertSpToPixel(TEXT_SIZE, context) + convertDpToPixel(DISTANCE_BW_TEXT_N_BAR, context),
                mapToTextPaints.getValue(it.paintType))
    }

    canvas.restore()
}

fun prepareBarsArr(arr: IntArray) {
    val max = arr.max();
    val viewHeight = measuredHeight
    val rectHeight = measuredHeight * FRACTIONAL_MAX_BAR_HEIGHT /*- ( convertSpToPixel(TEXT_SIZE, context) +  convertDpToPixel(DISTANCE_BW_TEXT_N_BAR, context))*/;
    val width = measuredWidth
    widthPerBar = (width - INTER_BAR_GAP * (arr.size - 1)) / arr.size

    var currentX = -widthPerBar - INTER_BAR_GAP
    mBars = Array<BarDetails>(arr.size, {
        val desiredHeight = arr[it] * rectHeight / max!!
        currentX += widthPerBar + INTER_BAR_GAP
        val mRect = RectF(currentX.toFloat(), 0f, (currentX + widthPerBar).toFloat(), (-desiredHeight).toFloat());
        BarDetails(mRect, arr[it], STATEPAINTS.NORMAL)
    });


    invalidate()
}

mBars存储Bar相关信息,例如:Rect对象,数值(有助于确定条的高度),要使用的绘画等。

问题是这个代码在模拟器上完美运行但在我的设备上(使用联想Zuk Z2 Plus),它不会绘制条形码。我检查了一些帖子并尝试使用hardwareAccelerated =“true / false”的不同组合  和drawRoundRect / drawRect等只有一个组合,我的设备上出现了条形,当hardwareAccelerated是假的时候我正在使用drawRect。但是,当我使用RoundedRect时,条形图不显示。不知道这是否是特定于设备的问题,因为我只有1个设备可供测试。无论哪种方式,如何调试这一点都会有所帮助。

1 个答案:

答案 0 :(得分:0)

通过重写逻辑来创建条形图来解决这个问题。在有问题的代码中,我习惯翻译画布,然后从底部到顶部绘制条形/矩形。我刚刚更改了计算,在rectHeight的Rect和Bottom顶部创建了Rect with Top。以下是修改后的代码,甚至渲染了RoundedRect。我有android:hardwareAccelerated =&#34; false&#34;在我的清单文件中。此外,早些时候我正在进行保存和恢复,这是现在不需要的,因为没有完成画布的转换。

fun prepareBarsArr(arr: IntArray) {
    val max = arr.max();
    val viewHeight = measuredHeight
    val rectHeight = measuredHeight * FRACTIONAL_MAX_BAR_HEIGHT /*- ( convertSpToPixel(TEXT_SIZE, context) +  convertDpToPixel(DISTANCE_BW_TEXT_N_BAR, context))*/;
    val width = measuredWidth
    widthPerBar = (width - INTER_BAR_GAP * (arr.size + 1)) / arr.size

    var currentX = INTER_BAR_GAP
    mBars = Array<BarDetails>(arr.size, {
        val desiredHeight = arr[it] * rectHeight / max!!
        val mRect = RectF(currentX.toFloat(),
                rectHeight - desiredHeight,
                (currentX + widthPerBar).toFloat(),
                (rectHeight).toFloat()
        );
        currentX += widthPerBar + INTER_BAR_GAP
        BarDetails(mRect, arr[it], STATEPAINTS.NORMAL)
    });


    invalidate()
}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    if (mBars == null) {
        return
    }

    /*val rectBaseLine = measuredHeight * FRACTIONAL_MAX_BAR_HEIGHT + convertDpToPixel(TOP_MARGIN_FOR_BAR, context);*/ /*- ( convertSpToPixel(TEXT_SIZE, context) +  convertDpToPixel(DISTANCE_BW_TEXT_N_BAR, context))*/;
    /*canvas.translate(0f, rectBaseLine.toFloat())*/

    mBars.forEach {
        canvas.drawRoundRect(it.mRect,
                BAR_CORNER_RADIUS, BAR_CORNER_RADIUS,
                mapToPaints.getValue(it.paintType))

        var textWidth = textPaint.measureText(it.num.toString())

        canvas.drawText(it.num.toString(),
                it.mRect.left + (it.mRect.right - it.mRect.left) / 2 - textWidth / 2,
                convertSpToPixel(TEXT_SIZE, context) + convertDpToPixel(DISTANCE_BW_TEXT_N_BAR, context) + it.mRect.bottom,
                mapToTextPaints.getValue(it.paintType))
    }
}

如果有人愿意补充,我会很感激这背后的推理。我是否在使用画布翻译或保存和恢复时出错?我不知道为什么它在Emulator上工作正常而不在Device上。