1

Simply paste this code into a simple skeleton Android project.

public final class DrawableView extends View
{
    private float[] mVertices = {0, 0, 255, 0, 255, 255, 0, 255};
    private float[] mTexCoords = {0, 0, 255, 0, 255, 255, 0, 255};
    private short[] mIndices = {0, 2, 3, 0, 1, 2};
    private int[] mColors = {Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA};

    Context mContext;
    BitmapShader mShader;

    public DrawableView(Context context)
    {
        super(context);
        mContext = context;
        mShader = new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.icon), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setShader(mShader);

        canvas.drawVertices(Canvas.VertexMode.TRIANGLES, 8, mVertices, 0, mTexCoords, 0, mColors, 0, mIndices, 0, 6, paint);

        invalidate();
    }
}

And then set this as the main view in the onCreate of the main activity.

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(new DrawableView(this));
}

This should make the application exit, with no error or even a "force close" dialog. Logcat gives me nothing useful (http://pastebin.com/c67NJnBz), either!

Both the following drawVertices calls produce the desired effect, though.

canvas.drawVertices(Canvas.VertexMode.TRIANGLES, 8, mVertices, 0, mTexCoords, 0, null, 0, mIndices, 0, 6, paint); // Works!

and

paint.setColor(Color.RED);
// paint.setShader(mShader);

canvas.drawVertices(Canvas.VertexMode.TRIANGLES, 8, mVertices, 0, mTexCoords, 0, mColors, 0, mIndices, 0, 6, paint); // Renders wireframe

Am I doing something wrong? Please help me determine if this an Android API bug.

Kara
  • 6,115
  • 16
  • 50
  • 57
Ani
  • 10,826
  • 3
  • 27
  • 46

1 Answers1

1

Even though the documentation for drawVertices does not spell this out explicitly, the array size of the verts, texs, and colors arrays must all match the vertexCount. The third answer down in this question would also seem to confirm this. Keep in mind, only the first (vertexCount / 2) colors are used to draw the triangles, the other values are ignored.

Community
  • 1
  • 1
Mark
  • 587
  • 3
  • 8
  • Yes, I understand. I have 4 colors, and 4 vertices (x and y coordinates for each). I see that the answer you've linked to specifies 6 colors, one for each co-ordinate! Could you please explain what color would be used for a vertex at 0, 0 that had two colors Color.RED and Color.GREEN specified for the colors array? – Ani Jun 22 '11 at 15:49
  • I know... it doesn't make much sense, and I hadn't actually tried it myself. I tried taking your code, though, and "doubling up" the color array (i.e., "Color.RED, Color.RED, Color.GREEN, Color.GREEN...) and it results in the icon being drawn in red and the InputDispatcher errors going away, and the application doesn't crash, so it seems to be a step in the right direction. – Mark Jun 22 '11 at 20:46
  • Thanks, it works if I provide it as many colors are there are entries in the vertices array, but I still don't understand why. Shouldn't there only be as many colors are vertices, not vertex co-ordinates? Goes against the logic of any graphics API I've ever seen. – Ani Jun 23 '11 at 01:50
  • Agreed... and the way that it fails definitely strikes me as buggy. – Mark Jun 23 '11 at 03:18