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 8521243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:46:02+00:00 2026-06-11T06:46:02+00:00

I started my android app using png files for objects that required alpha, but

  • 0

I started my android app using png files for objects that required alpha, but I quickly realized that the space required was simply too much. So, I wrote a program that took a png with alpha and created a b&w alpha mask png file and a jpeg. This gives me a tremendous amount of space savings, but the speed is not great.

The following is the code in my Android app which combines the jpg image (the origImgId in the code) and the png mask (the alphaImgId in the code).

It works, but it’s not fast. I already cache the result and I’m working on code which will load these images in the menu screen before the game starts, but it would be nice if there was a way to speed this up.

Does anyone have any suggestions? Note that I modified the code slightly so as to make it easily understandable. In the game this is actually a sprite which loads the image on demand and caches the result. Here you just see the code for loading the images and applying the alpha.

public class BitmapDrawableAlpha
{
    public BitmapDrawableAlpha(int origImgId, int alphaImgId) {
        this.origImgId = origImgId;
        this.alphaImgId = alphaImgId;
    }

    protected BitmapDrawable loadDrawable(Activity a) {
        Drawable d = a.getResources().getDrawable(origImgId);
        Drawable da = a.getResources().getDrawable(alphaImgId);

        Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(),d.getIntrinsicHeight(),Bitmap.Config.ARGB_8888);
        {
            Canvas c = new Canvas(b);
            d.setBounds(0,0,d.getIntrinsicWidth()-1,d.getIntrinsicHeight()-1);
            d.draw(c);
        }

        Bitmap ba = Bitmap.createBitmap(d.getIntrinsicWidth(),d.getIntrinsicHeight(),Bitmap.Config.ARGB_8888);
        {
            Canvas c = new Canvas(ba);
            da.setBounds(0,0,d.getIntrinsicWidth()-1,d.getIntrinsicHeight()-1);
            da.draw(c);
        }

        applyAlpha(b,ba);

        return new BitmapDrawable(b);
    }

    /** Apply alpha to the specified bitmap b. */
    public static void applyAlpha(Bitmap b, Bitmap bAlpha) {
        int w = b.getWidth();
        int h = b.getHeight();
        for(int y=0; y < h; ++y) {
            for(int x=0; x < w; ++x) {
                int pixel = b.getPixel(x,y);
                int finalPixel = Color.argb(Color.alpha(bAlpha.getPixel(x,y)), Color.red(pixel), Color.green(pixel), Color.blue(pixel));
                b.setPixel(x,y,finalPixel);
            }
        }
    }

    private int origImgId;
    private int alphaImgId;
}
  • 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-06-11T06:46:04+00:00Added an answer on June 11, 2026 at 6:46 am

    If you are going to be manipulating every multiple pixels you could call getPixels() and setPixels() to get them all at once. This will prevent additional method calls and memory references in your loop.

    Another thing you could do is do the pixel addition with bitwise or instead of the helper methods. Preventing method calls should increase efficiency:

    public static void applyAlpha(Bitmap b, Bitmap bAlpha) {
        int w = b.getWidth();
        int h = b.getHeight();
        int[] colorPixels = new int[w*h];
        int[] alphaPixels = new int[w*h];
        b.getPixels(colorPixels, 0, w, 0, 0, w, h);
        bAlpha.getPixels(alphaPixels, 0, w, 0, 0, w, h);
        for(int j = 0; j < colorPixels.length;j++){
            colorPixels[j] = alphaPixels[j] | (0x00FFFFFF & colorPixels[j]);
        }
        b.setPixels(colorPixels, 0, w, 0, 0, w, h);
    }
    

    That being said the process you are trying to undertake is fairly simple, I can’t imagine these will provide a huge performance boost. From this point the only suggestion I can provide would be to goto a native implementation with the NDK.

    EDIT: Also since a Bitmap doesn’t have to be mutable to use getPixels() or getPixel() you can get the alpha bitmap with BitmapFactory.decodeResource():

    Bitmap ba = BitmapFactory.decodeResource(a.getResources(), alphaImgId);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started making graphics for my Android app using Adobe Photoshop. But I
I started developing an android app that have to interact with MMS attachements, in
I have just started using android, and have about 5 layout files finished. However,
I have a web app, that also has an iPhone and Android app using
I've started using JSoup today to use for an android app so I have
I've started building an android app that will connect to a sql server and
I've started work on an Android App that will work with Google Docs. UPDATE
I'm working on an Android app using Eclipse and just started noticing a weird
I have created a Quiz app for Android using the tutorial here: http://automateddeveloper.blogspot.co.uk/2011/06/getting-started-complete-android-app.html For
I have recently started making an Android app that requires a list of 20

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.