canvas.clipPath()在少数设备中不起作用

时间:2014-05-07 06:37:03

标签: android canvas clip

我在少数设备中遇到canvas.clipPath()的问题。它在模拟器和一些高端设备(如MotoG和nexus平板电脑)上运行良好。但是当我在我的朋友手机中测试时,我添加的Square视图没有添加,应用程序崩溃,给出了以下错误。

当我在问题的ans中添加if语句时,我尝试了这个问题Android UnsupportedOperationException at Canvas.clipPath的解决方案,方形视图也没有添加到我的其他设备中(MotoG和nexus)。所以我不能在我的情况下使用soln。

然而,如果我只是注释掉canvas.clipPath();我的朋友手机中会添加方形视图。但正如预期的那样,我的一些功能不起作用。 我该如何解决这个问题。请指教。在此先感谢:)

05-07 11:45:54.656: E/AndroidRuntime(5997): FATAL EXCEPTION: main
05-07 11:45:54.656: E/AndroidRuntime(5997): java.lang.UnsupportedOperationException
05-07 11:45:54.656: E/AndroidRuntime(5997): at  android.view.GLES20Canvas.clipPath(GLES20Canvas.java:429)
05-07 11:45:54.656: E/AndroidRuntime(5997): at example.Square.addText(Square.java:260)
05-07 11:45:54.656: E/AndroidRuntime(5997): at example.Square.onDraw(Square.java:140)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.draw(View.java:11054)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.getDisplayList(View.java:10493)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.draw(View.java:11057)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.getDisplayList(View.java:10493)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.draw(View.java:11057)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.getDisplayList(View.java:10493)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.getDisplayList(View.java:10491)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.getDisplayList(View.java:10491)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.drawChild(ViewGroup.java:2958)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2596)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.draw(View.java:11057)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.widget.FrameLayout.draw(FrameLayout.java:463)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2145)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.View.getDisplayList(View.java:10493)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:880)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:1911)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1635)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.os.Looper.loop(Looper.java:137)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
05-07 11:45:54.656: E/AndroidRuntime(5997):     at dalvik.system.NativeStart.main(Native Method)

我在方形视图类的这个函数中遇到错误我在onDraw方法中调用此函数传递它的画布。

private void addText(Canvas canvas){
    //setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    //Setting text

    canvas.clipPath(path);

    bitmapWidth = right+x+ mWidth;
    bitmapHeight = bottom+y+mHeight;

    if(bitmapWidth <= 10){
        bitmapWidth = 10;
    }
    if(bitmapHeight <= 10){
        bitmapHeight = 10;
    }

    bitMap1 = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas1 = new Canvas(bitMap1);

    tp.setColor(textColor);
    txtWidth = (right+x+ mWidth)-(left+x);
    txtHeight = (bottom+y+mHeight)-(top+y);

    if(left+x < 0 ){
        txtWidth = (right+x+ mWidth);
    }
    if(top+y < 0){
        txtHeight = (bottom+y+mHeight);
    }
    pdl = new PlacableDynamicLayout(string,tp,txtWidth, txtHeight,Alignment.ALIGN_CENTER,1.0f,1.0f,false,Anchor.ANCHOR_TOP,1);
    pdl.draw(canvas1);

    canvas.drawBitmap(bitMap1, (left+x), (top+y) , paint);
}

1 个答案:

答案 0 :(得分:4)

Android具有悲惨(缺少硬件)后备功能。您的确切情况不适用于具有&#34;使用主机GPU&#34;检查(至少在我的4.2.2。是这样)。所以实际的硬件会以两种方式做出反应(我已经看到了):

  1. 抛出异常(例如在您的情况下)
  2. 尽量做到最好(在我的情况下,路径边界计算并应用为剪切矩形而不是路径)
  3. 软件渲染将解决我所看到的问题(但可能会更慢)。