错误的Canvas.drawVertices? (与摄制code和logcat的) [英] Bug in Canvas.drawVertices? (with repro code and logcat)

查看:82
本文介绍了错误的Canvas.drawVertices? (与摄制code和logcat的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要复制粘贴此code到一个简单的框架Android项目。

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

然后设置这个作为主要活动的OnCreate主视图。

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

这应该使应用程序退出,没有错误,甚至是强制关闭的对话框。 logcat的给我什么有用的(http://pastebin.com/c67NJnBz),无论是!

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!

以下两个drawVertices呼叫产生预期的效果,虽然

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!

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的bug。

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

推荐答案

虽然<一href="http://developer.android.com/reference/android/graphics/Canvas.html#drawVertices%28android.graphics.Canvas.VertexMode,%20int,%20float%5B%5D,%20int,%20float%5B%5D,%20int,%20int%5B%5D,%20int,%20short%5B%5D,%20int,%20int,%20android.graphics.Paint%29"相对=nofollow>为drawVertices文档并没有说明这一点明确的绿党,texs的数组大小和颜色数组都必须符合vertexCount。第三个答案倒在<一个href="http://stackoverflow.com/questions/3501126/how-to-draw-a-filled-triangle-in-android-canvas">this问题也似乎证实了这一点。 请记住,只有第一个(vertexCount / 2)颜色用于绘制三角形,其他值都被忽略。

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.

这篇关于错误的Canvas.drawVertices? (与摄制code和logcat的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆