1

只需将此代码粘贴到一个简单的框架 Android 项目中。

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

然后在主Activity的onCreate中将此设置为主视图。

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

这应该使应用程序退出,没有错误,甚至没有“强制关闭”对话框。Logcat 也没有给我任何有用的东西(http://pastebin.com/c67NJnBz)!

但是,以下两个 drawVertices 调用都会产生所需的效果。

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

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

难道我做错了什么?请帮我确定这是否是 Android API 错误。

4

1 回答 1

1

尽管 drawVertices 的文档没有明确说明这一点,但 verts、texs 和 colors 数组的数组大小都必须与 vertexCount 匹配。这个问题的第三个答案似乎也证实了这一点。请记住,只有第一个 (vertexCount / 2) 颜色用于绘制三角形,其他值被忽略。

于 2011-06-22T04:29:07.870 回答