Question

I have been battling with trying to draw a bitmap and then highlighting a region on it with a rectangle. Originally, I was drawing a bitmap with alpha black in paint to make image darker and then on top drawing original bitmap in a region creating effect of highlight. I discovered that largest slowdown was because of alpha in Paint. So I have reworked the code and ended up with following in my draw thread:

private synchronized void drawSquare(int xStart, int yStart, int xEnd, int yEnd) {
  Canvas c = holder.lockCanvas();

  if(c != null) {

        // Draw the background picture on top with some changed alpha channel to blend
        Paint paint = new Paint();
        paint.setAntiAlias(true);


        if(bg != null && cWidth > 0 && cHeight > 0) {
            c.clipRect(xStart, yStart, xEnd, yEnd,  Region.Op.DIFFERENCE);
            c.drawBitmap(bg, gTransform, blackSqr); // Draw derker background
            c.clipRect(xStart, yStart, xEnd, yEnd, Region.Op.REPLACE);
            c.drawBitmap(bg, gTransform, paint); ///draw original in selection
            c.clipRect(0, 0, cWidth, cHeight,Region.Op.REPLACE);
        }

        Matrix RTcorner = new Matrix();
        RTcorner.setRotate(90);
        RTcorner.postTranslate(xEnd + 13, yStart - 13);

        Matrix RBcorner = new Matrix();
        RBcorner.setRotate(180);
        RBcorner.postTranslate(xEnd + 13, yEnd + 13);

        Matrix LBcorner = new Matrix();
        LBcorner.setRotate(270);
        LBcorner.postTranslate(xStart - 13, yEnd + 13);

        // Draw the fancy bounding box 
        c.drawRect(xStart, yStart, xEnd, yEnd, linePaintB);

        // Draw corners for the fancy box
        c.drawBitmap(corner, xStart - 13, yStart - 13, new Paint());    
        c.drawBitmap(corner, RBcorner, new Paint());        
        c.drawBitmap(corner, LBcorner, new Paint());    
        c.drawBitmap(corner, RTcorner, new Paint());


    }

    holder.unlockCanvasAndPost(c);
}

So this clips out my selection area, I draw with paint that has this code to make it darker.

blackSqr.setColorFilter(new LightingColorFilter(Color.rgb(100,100,100),0));

And in the area inside the clip I draw my original bitmap. It works. But I am not happy with response time. After profiling Bitmap is what takes the longest. I have scaled the bitmap to the size of the screen already so it's drawing 300x800-ish image. The biggest resource hog seems to be the Lighting effect. Because when I turn it off I get decent response time.

So I was wondering if I have missed anything to improve how quickly bitmap is drawn, maybe caching? Or am I just stuck with this because I want darker image and actually should rethink the "highlighting/selection" altogether? Why is is so expensive to draw a bitmap with alpha colour in 2D image?

Was it helpful?

Solution 2

For anyone that is interested, perhaps facing similar problem. This solution applies to my particular situation, but I have a separate background bitmap with darkened pixels manually set using:

for(int i = 0; i < cWidth; i++){
        for(int j = 0; j < cHeight; j++){
            int c = bg2.getPixel(i, j);

            float mult = 0.15f;

            int r = (int) (Color.red(c) * mult);
            int g = (int) (Color.green(c) * mult);
            int b = (int) (Color.blue(c) * mult);

            bg2.setPixel(i, j, Color.rgb(r, g, b));
        }
    }

Then use the bg2 to draw main part and the original (not darkened) for the clip rectangle of the selection. There is a bit of overhead for creating and maintaining the second bitmap but the draw speed and response time is quick and smooth in comparison to bitmaps with alpha.

OTHER TIPS

if I understand what you want, you want a rectangle (with rounded corners) to highlight a part from another image.

if it is that, then I would use an image with the square wit draw9patch and use it as a floating view over the image view

RelativeLaoyut (Image container) +- ImageView (your actual image) +- view (it has the square as a background, and you only have to move it to the area you want to highlight)

I'm sorry, I'm not good explaining myself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top