Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:44:07+00:00 2026-05-30T10:44:07+00:00

I have the following scenario: a Bitmap that is used as background and another

  • 0

I have the following scenario: a Bitmap that is used as background and another Bitmap that is used as overlay which can be either 50% transparent or opaque (changeable at run time) and a third Bitmap that contains a mask for this second Bitmap. I’ve tried different Xfermodes configurations and drawing orders but wasn’t able to find the right one.

I’m using the mask as a bitmap because I need to be able to save it between two runs of the program or between configuration changes. It is created as the user draws on the screen, effectively cleaning the fog of war.

Code snippets from by best attempt. Only thing that didn’t work as I wished it did was the transparency of my mask.

@Override
protected void onDraw(Canvas canvas) {      
    canvas.drawBitmap(mFogOfWar, mTransformationMatrix, mPaintFog);
    canvas.drawBitmap(mMaskBitmap, mTransformationMatrix, mPaintMask);
    canvas.drawBitmap(mImage, mTransformationMatrix, mPaintImage);
}

Paint objects:

mPaintImage.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
mPaintFog.setAlpha(127);
mPaintMask.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

This is what I get with the current configuration to be more clear:
screenshot

I’m not sure if I am going to be able to do this setting the alpha on the Paint object; if not, I don’t mind another suggestion or solution for the alpha issue, preferably one where the recreation of the bitmap being used as a fog of war is not necessary.

EDIT:

I was able to get the results I want by doing the following:

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(mImage, mTransformationMatrix, mPaintImage);
    if (mMaskBitmap != null) {
        canvas.drawBitmap(mFogOfWar, mTransformationMatrix, mPaintFog);
        canvas.drawBitmap(mMaskBitmap, mTransformationMatrix, mPaintMask);
        canvas.drawBitmap(mMaskBitmap, mTransformationMatrix, mPaintImage);
        canvas.drawBitmap(mImage, mTransformationMatrix, mPaintImageSecondPass);
    }

Paint Objects:

mPaintImage = new Paint(); // No Xfermode here anymore
mPaintFog.setAlpha(127);
mPaintMask.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaintImageSecondPass.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN));

But drawing the bitmaps five times seems like a waste. As this runs in an OpenGL texture due to Android hardware aceleration (I rescale the bitmaps to the highest resolution accepted by the device’s GPU) and I take a lot of care in my invalidates() it runs surprisingly smooth on both my Nexus S and my A500, but I am not sure about other devices (project is going to be 4.0+ anyways).

But I am convinced there must be a better way to do it. I’d like a way that avoided that much overdrawn or that at least could explain to me correctly what those Xfermodes mean and that I am not overdrawing stuff.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:44:08+00:00Added an answer on May 30, 2026 at 10:44 am

    I’ve tried a completely different approach after having some kind of an epiphany – and realized that the solution for this problem was a much simpler approach – as it usually is. And as I need only two bitmaps, I need much fewer memory to work with it.

    For drawing:

    canvas.drawBitmap(mImage, mTransformationMatrix, mPaintImageRegular);
    if (mFogOfWarState != FOG_OF_WAR_HIDDEN) {
        canvas.drawBitmap(mFogOfWar, mTransformationMatrix, mPaintFog);
    }
    

    The “secret” was that instead of drawing on the mask bitmap, I’m erasing the fog of war using another paint:

    mFogOfWarCanvas.drawPath(mPath, mEraserPaint);
    

    The only Paint that has an Xfermode is the used one for erasing:

    mEraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    

    And for the loading and saving of my mask, I do the following:

    private void createFogAndMask(File dataDir) {
        BitmapDrawable tile = (BitmapDrawable) getResources().getDrawable(R.drawable.fog_of_war); 
        tile.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        mFogOfWar = Bitmap.createBitmap(mImageBounds.width(), mImageBounds.height(), Config.ARGB_8888);
        mFogOfWarCanvas = new Canvas(mFogOfWar);
        tile.setBounds(mImageBounds);
        tile.draw(mFogOfWarCanvas);   
        tile = null;
    
        // Try to load an existing mask
        File existingMask = new File(dataDir, getMaskFileName());
        if (existingMask.exists()) {
            Bitmap existingMaskBitmap = BitmapFactory.decodeFile(existingMask.getAbsolutePath());
            mFogOfWarCanvas.drawBitmap(existingMaskBitmap, new Matrix(), mPaintImageRegular);
            mFogOfWarCanvas.drawPaint(mMaskEraserPaint);
            existingMaskBitmap.recycle();
            System.gc();
        }
    }
    
    public void saveMask(File folder) throws IOException {
        if (!mReady || mImagePath == null) return;
        mImage.recycle();
        System.gc();
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File savedFile = new File(folder, getMaskFileName());
    
        // Change all transparent pixels to black and all non-transparent pixels to transparent
        final int length = mImageBounds.width() * mImageBounds.height();        
        final int[] pixels =  new int[length];
        mFogOfWar.getPixels(pixels, 0, mImageBounds.width(), 0, 0, mImageBounds.width(), mImageBounds.height());
        for (int i = 0; i < length; i++) {
            if (pixels[i] == Color.TRANSPARENT) {
                pixels[i] = Color.BLACK;
            } else {
                pixels[i] = Color.TRANSPARENT;              
            }
        }
        mFogOfWar.setPixels(pixels, 0, mImageBounds.width(), 0, 0, mImageBounds.width(), mImageBounds.height());
    
        FileOutputStream output = new FileOutputStream(savedFile);
        mFogOfWar.compress(CompressFormat.PNG, 80, output);
        output.flush();
        output.close();     
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario that may warrant storing data in a conroller member
I have the following scenario. A cube created in SSAS 2008. I can connected
i have following scenario and can't seem to find anything on the net, or
I have following scenario: alt text http://static.zooomr.com/images/7579022_e64808b855_o.png We have a WebService which poses as
I have the following scenario: a table that is accessed (update, delete, insert and
Have the following scenario. I have a few form, which essentially have a few
I have the following scenario (note that activity A has launchMode=singleTop ): Activity A
i have a following scenario that i have a products, categories and subcategories in
I have the following scenario: A Service that does nothing but sleeps for the
I have the following scenario: 1 List which contains the months of the year:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.