Canvas.drawVertices(...)什么也没画 [英] Canvas.drawVertices(...) draws nothing

查看:52
本文介绍了Canvas.drawVertices(...)什么也没画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下一课是一个红色三角形的视图:

The next class is a view for a red triangle:

public class FreeStyleViewII extends View {

private final Paint paint = new Paint();
private final int[] colors = new int[] {
        Color.RED,
        Color.RED,
        Color.RED,
        0xFF000000, 0xFF000000, 0xFF000000
};
private final float[] verts = new float[] {
    1f/2f * 200f, 1f/4f * 200f,
    1f/4f * 200f, 3f/4f * 200f,
    3f/4f * 200f, 3f/4f * 200f
};
private final Path path = new Path();
{
    path.moveTo(1/2 * 200, 1/4 * 200);
    path.lineTo(1/4 * 200, 3/4 * 200);
    path.lineTo(3/4 * 200, 3/4 * 200);
    path.lineTo(1/2 * 200, 1/4 * 200);
}

private final RectF bounds = new RectF();

    public FreeStyleViewII(Context context) {
        super(context);
    }

    public FreeStyleViewII(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public FreeStyleViewII(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.clipRect(bounds);
        canvas.drawRGB(0, 0, 0);

        paint.setStyle(Style.FILL);
        paint.setColor(Color.RED);

        // HERE. WHY DRAWVERTICES DOESN'T WORK BUT DRAWPATH DOES?... 
        canvas.drawVertices(Canvas.VertexMode.TRIANGLES, verts.length, verts, 0, null, 0, colors, 0, null, 0, 0, paint);
        // canvas.drawPath(path, paint);

        paint.setStyle(Style.STROKE);
        paint.setColor(Color.LTGRAY);
        canvas.drawLine(0, bounds.bottom / 2, bounds.right, bounds.bottom / 2, paint);
        canvas.drawLine(bounds.right / 2, 0, bounds.right / 2, bounds.bottom, paint);

        // Delay
    try {  
        Thread.sleep(30);  
    } catch (InterruptedException e) { }

        invalidate();

    }

@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
    bounds.set(1, 1, w - 1, h - 1);
    System.out.println(bounds.left + " " + bounds.top + " " + bounds.right + " " + bounds.bottom);

    verts[0] = bounds.left + ((bounds.right - bounds.left) * (1f / 2f));
    verts[1] = bounds.top + ((bounds.bottom - bounds.top)  * (1f / 4f));
    System.out.println(" Point: X ----> " + verts[0] + " | Y ----> " + verts[1]);

    verts[2] = bounds.left + ((bounds.right - bounds.left) * (1f / 4f));
    verts[3] = bounds.top + ((bounds.bottom - bounds.top)  * (3f / 4f));
    System.out.println(" Point: X ----> " + verts[2] + " | Y ----> " + verts[3]);

    verts[4] = bounds.left + ((bounds.right - bounds.left) * (3f / 4f));
    verts[5] = bounds.top + ((bounds.bottom - bounds.top)  * (3f / 4f));
    System.out.println(" Point: X ----> " + verts[4] + " | Y ----> " + verts[5]);

    path.reset();

    path.moveTo(verts[0], verts[1]);
    path.lineTo(verts[2], verts[3]);
    path.lineTo(verts[4], verts[5]);
    path.lineTo(verts[0], verts[1]);

}
}

当我使用Canvas.drawPath方法时,它可以正常工作.但是,如果我更改为Canvas.drawVertices,则不会绘制任何内容.我已经检查了在Canvas.drawVertices中发现的错误吗? (带有repro代码和logcat)方法drawVertices()没有在Android Canvas上绘制任何内容,但是对于我来说,结果是相同的.

When I use Canvas.drawPath method, it works fine. However, if I change to Canvas.drawVertices, it draws nothing. I have checked what is said in Bug in Canvas.drawVertices? (with repro code and logcat) and Method drawVertices() didn't drawing anything on Android Canvas, but the result is the same in my case.

我正在 AndroVM(v 4.1.1)://www.virtualbox.org/wiki/Downloads"rel =" nofollow noreferrer> VirtualBox(v 4.1.22)进行测试.可以是模拟器吗?

I am using AndroVM(v 4.1.1) in VirtualBox(v 4.1.22) for testing. Could it be the emulator?

有什么主意吗?

推荐答案

简短答案:

禁用硬件加速,就可以了!

Disable hardware acceleration and you will be fine!

详细答案:

我遇到了完全相同的问题.在我的自定义查看"代码中,我有一个对Canvas.drawVertices()的调用,如下所示:

I had the exact same problem. In my custom View code, I have a call to Canvas.drawVertices() that looks like this:

canvas.drawVertices(Canvas.VertexMode.TRIANGLES, mTriVerts.length, mTriVerts, 0, null, 0, mTriangleColors, 0, null, 0, 0, mTriPaint);

这与许多设备上的预期工作完全相同,但是在我的4.x测试设备上,我试图绘制的三角形完全消失了,并且logcat中没有任何痕迹来指示原因.

This works exactly like expected on many devices, but on my 4.x test device the triangle which I am trying to draw simply disappears, and there are no traces in the logcat to indicate as to why.

在S.O找不到解决方案后.我开始四处研究,并偶然发现了以下令人震惊的解决方案:如果启用了硬件加速,包括对Canvas.drawVertices()的任何调用,并且默认情况下在所有支持该功能的设备上都启用了硬件加速,那么一堆标准的东西将无法工作.适用于Android 4.x和更高版本.

After not finding a solution at S.O. I started digging around and found by chance the following shocking solution: a bunch of standard stuff won't work if hardware acceleration is enabled, including any call to Canvas.drawVertices(), and hardware acceleration is turned on by default on all devices that support it for Android 4.x and newer.

官方android文档中概述了问题和好的解决方案.要在此处进行总结,只需将其放在您不受支持的呼叫之前的行即可:

The problem and a good solution is outlined in the official android docs. To summarize it here, just put this on the line preceeding your unsupported calls:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

这是View级别,不幸的是,目前无法在已设置为软件模式的视图上重新启用硬件加速,但是在这种情况下阅读该文档可能是一种选择未来.如果您认为您的代码需要硬件加速,只需避免使用这些调用,或者自己在视图中收集不符合要求的代码,然后对其进行概要分析,以查看您实际上是通过这种方式获得了一些收获.

This is on View level, and it is unfortunately not possible to re-enable hardware acceleration on a view that has been set to software mode as of now, but reading the docs it felt like that might be an option in the future. If you feel your code needs hardware acceleration, simply avoid using the calls, or collect you non compliant code in a view by itself, and profile it to see that you are actually gaining something by doing it this way.

重要

setLayerType()调用仅在11级和更高版本的Android API上受支持,这意味着您必须将对此函数的调用包装在版本检查中,以使其在这样的早期版本中起作用:

The setLayerType() call is only supported on Android API level 11 and later, that means you must wrap your calls to this function in a version check for it to work on earlier versions like this:

if (android.os.Build.VERSION.SDK_INT >= 11) {
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

现在您应该已经准备就绪!

NOW you should be all set!

PS:,我希望这个答案能及时送达您!

PS: I hope this answer reaches you in time!

这篇关于Canvas.drawVertices(...)什么也没画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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