【问题标题】:Canvas is not working on Android latest version 4+Canvas 不适用于 Android 最新版本 4+
【发布时间】:2013-06-15 00:08:53
【问题描述】:

我想在活动布局的顶部创建层,我需要制作矩形或圆形的孔,类似于附加的图像。我通过使用 SurfaceView 做到了这一点,它在 android 上的较低版本上运行良好,但在版本 4+ 上却没有

[示例屏幕截图] (https://docs.google.com/file/d/0B3yZokwUGKFgTkM1ek1nZHNyZ2s/edit?usp=sharing)

我的代码是

public class SurfacePanel extends SurfaceView implements SurfaceHolder.Callback{

    public SurfacePanel(Context context) {
        super(context);
        init();
    }

    public SurfacePanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.CommonInit();
    }

    private void init() {
        setZOrderOnTop(true); //necessary
        getHolder().setFormat(PixelFormat.TRANSPARENT);
        getHolder().addCallback(this);
        setBackgroundColor(Color.TRANSPARENT);
    }

    public void onDraw(Canvas canvas){
        Path path = new Path();
        path.moveTo(10, 10);
        path.lineTo(115, 10);
        path.lineTo(115, 260);
        path.lineTo(10, 260);
        path.close();

        canvas.clipPath(path, Region.Op.DIFFERENCE);
        canvas.clipRect(150, 30, 200, 100, Region.Op.DIFFERENCE);
        canvas.drawARGB(180, 0, 0, 0);

        super.onDraw(canvas);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

}

请有人帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: android canvas


    【解决方案1】:

    这可能是因为 ICS+ 默认硬件加速为 true,并且您在画布上的某些绘图调用在硬件模式下不受支持,请检查以下内容:

    http://developer.android.com/guide/topics/graphics/hardware-accel.html

    您可以在清单中的应用程序标记中、清单中的每个活动或视图本身中使用setLayerType 将其关闭

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      相关资源
      最近更新 更多