Canvas drawCircle在现有图像后面

时间:2017-09-11 08:15:40

标签: android canvas android-bitmap

请帮我弄清楚我做错了什么。我必须根据应用程序逻辑在图像后面(以位图给出)绘制一个不同颜色的圆圈,我使用以下代码。

        Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
        Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        int horizontalPadding = (iconSize - drawingWidth) / 2;
        int verticalPadding = (iconSize - drawingHeight) / 2;

        canvas.drawCircle(120, 120, 100, paint);
        return mutableBitmap;

我得到的是图像上方的一个圆圈,它完全覆盖了图像,如何告诉代码图像有一个上层。

由于

1 个答案:

答案 0 :(得分:1)

在您的代码中,您使用mutableBitmap作为画布并在其上绘制圆圈。如果要在圆圈上方绘制图像,则应在绘制圆形后绘制图像。代码:

    Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
    Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
    // Create an empty bitmap
    Bitmap output = Bitmap.createBitmap(mutableBitmap.getWidth(), mutableBitmap.getHeight(), 
            mutableBitmap.getConfig());
    // Use empty bitmap as canvas
    Canvas canvas = new Canvas(output);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);

    int horizontalPadding = (iconSize - drawingWidth) / 2;
    int verticalPadding = (iconSize - drawingHeight) / 2;

    canvas.drawCircle(120, 120, 100, paint);
    // And now draw image above circle
    canvas.drawBitmap(mutableBitmap, 0, 0, null);
    return output;