Skip to content Skip to sidebar Skip to footer

How To Get The Draw Element Size On A Cavans?

I was working on a 'draw with mask' app. When the user drag on the screen , it cleans part of the mask. I implemented it through cavans and with setXfermode Clear // Specify that

Solution 1:

What you need to do is to convert your canvas in to bitmap and count the number of black pixels in it. Using simple math you can divide the number of black pixels to the number of pixels in the canvas which will give you the percentage of black pixels.

sample taken from this post:

publicfloatpercentTransparent(Bitmap bm){ //pass the converted bitmap of canvasfinalint width = bm.getWidth();
    finalint height = bm.getHeight();

    int totalBlackPixels = 0;
    for(int x = 0; x < width; x++) {
        for(int y = 0; y < height; y++) {
            if (bm.getPixel(x, y) == Color.BLACK) {
                totalBlackPixels ++;
            }
        }
    }
    return ((float)totalBlackPixels )/(width * height); //returns the percentage of black pixel on screen

}

Post a Comment for "How To Get The Draw Element Size On A Cavans?"