2013-02-04 205 views
4

下一個類是一個紅色三角形的視圖:Canvas.drawVertices(...)繪製任何東西

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,它什麼也不畫。我已經檢查了Bug in Canvas.drawVertices? (with repro code and logcat)Method drawVertices() didn't drawing anything on Android Canvas中的內容,但結果與我的情況相同。

我在VirtualBox(v 4.1.22)中使用AndroVM(v 4.1.1)進行測試。它可能是模擬器嗎?

有什麼想法?

回答

7

簡短的回答:

禁用硬件加速,你會被罰款!

龍答:

我有完全相同的問題。在我的自定義視圖代碼,我需要打個電話Canvas.drawVertices(),看起來像這樣:

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

操作就像預期在許多設備上,但我的4.x的測試設備,其本人三角形上試圖畫出簡單的消失,並且logcat中沒有跡象表明原因。

在S.O.找不到解決方案之後。我開始四處探索,發現了以下令人震驚的解決方案:如果啓用了硬件加速,包括對Canvas.drawVertices()的任何調用,並且默認情況下在支持Android的所有設備上啓用了硬件加速,一堆標準內容將不起作用4.x和更新。

這個問題和一個好的解決方案概述in the official android docs。在這裏總結一下,只是把這個就行了前述的不支持來電:

setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

這是View水平,這是不幸的是不可能在一個視圖重新啓用硬件加速已經被設置爲軟件模式,但是閱讀文檔可能是未來的選擇。如果你覺得你的代碼需要硬件加速,只需避免使用這些調用,或者單獨在視圖中收集不符合規範的代碼,然後對其進行分析,以確定你正在通過這種方式獲得某些東西。

重要編輯:

setLayerType()通話僅支持Android的API級別11及更高版本,這意味着你必須換你調用該函數的版本檢查它在早期版本那樣幹活:

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

現在你應該全部設置!

PS:我希望這個答案能及時到達你!

相關問題