1

When I drawVertices, I need to put the 6 array of colors instead of 3 even though I have only 3 coordinates.

        setLayerType(View.LAYER_TYPE_SOFTWARE, null)

        val verticesColors = intArrayOf(
            Color.RED, Color.GREEN, Color.BLUE,
            -0x1000000, -0x1000000, -0x1000000)

        val verts = floatArrayOf(0f, 0f, width.toFloat(), 0f, width/2f, height.toFloat())

        canvas.drawVertices(
            Canvas.VertexMode.TRIANGLES,
            verts.size, verts, 0,
            null, 0,
            verticesColors, 0,
            null, 0, 0,
            paint
        )

The image is as below enter image description here

However I'm confuse why I need to have the -0x1000000 value there as below?

    val verticesColors = intArrayOf(
        Color.RED, Color.GREEN, Color.BLUE,
        -0x1000000, -0x1000000, -0x1000000)

If I remove the -0x1000000, it will crash internally

    --------- beginning of crash
2019-04-05 00:27:34.793 15461-15461/? A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 15461 (anvasexperiment), pid 15461 (anvasexperiment)
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: Build fingerprint: 'google/sdk_gphone_x86/generic_x86:9/PSR1.180720.075/5124027:user/release-keys'
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: Revision: '0'
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: ABI: 'x86'
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: pid: 15461, tid: 15461, name: anvasexperiment  >>> com.elyeproj.canvasexperiment <<<
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG: Abort message: 'bad length'
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG:     eax 00000000  ebx 00003c65  ecx 00003c65  edx 00000006
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG:     edi 00003c65  esi ffc8f7ec
2019-04-05 00:27:34.824 15496-15496/? A/DEBUG:     ebp ffc8f7b8  esp ffc8f748  eip e9ff8b39
2019-04-05 00:27:34.967 15496-15496/? A/DEBUG: backtrace:
2019-04-05 00:27:34.968 15496-15496/? A/DEBUG:     #00 pc 00000b39  [vdso:e9ff8000] (__kernel_vsyscall+9)
2019-04-05 00:27:34.968 15496-15496/? A/DEBUG:     #01 pc 0001fdf8  /system/lib/libc.so (syscall+40)
2019-04-05 00:27:34.968 15496-15496/? A/DEBUG:     #02 pc 00022ed3  /system/lib/libc.so (abort+115)
2019-04-05 00:27:34.968 15496-15496/? A/DEBUG:     #03 pc 00006c84  /system/lib/liblog.so (__android_log_assert+292)

If I replace it with some other thing e.g. Color.RED, it has no impact. So I'm curious what's its function?

e.g. in the example in https://stackoverflow.com/a/3509969/3286489, also show the extra 3 0xFF000000 needed in the color array. Not sure for what purpose?

Update

I check under the hood drawVertices function, the assert code is as below checkRange(colors.length, colorOffset, vertexCount / 2); This means that, color.length should be equal or more than vertexCount / 2 Hence for 3 coordinates, 3 colors should be sufficient. So it is strange that we need 6 elements of color.

Elye
  • 53,639
  • 54
  • 212
  • 474

0 Answers0