1. Welcome to the newer, better version of Android Forums! Be sure to see the announcement and enjoy checking it out!

    Some of you have been having login issues. - Please try now. Sorry for the trouble!
  2. All attachments uploaded on the first day of this new look need to be re-uploaded, or will appear broken. All prior to that, and all going forward, should work fine. We apologize for the inconvenience!

Saving a png with transparency


  1. android88

    android88 Member

    Hi, i'm trying to render text by creating a canvas for a bitmap with a transparent background and then drawing onto the canvas. When i save the resulting bitmap however, the resulting image doesn't have a transparent background but a black one. Can it have anything to do with the config i set for the bitmap? Here is the code i use to draw and save as png:

    Code:
    public class TextToImage3 extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
            
            int height = display.getHeight(); // not exactly correct
            int width = display.getWidth(); // not exactly correct
            
            
            Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
            Canvas c = new Canvas(bitmap);
            c.drawColor(Color.TRANSPARENT);         
            
            Paint paint = new Paint();    
            paint.setTextSize(60);
            paint.setColor(Color.WHITE);
            c.drawText("Test", 100, 300, paint);
            
            ImageView iv = new ImageView(this);
            iv.setImageBitmap(bitmap);
            setContentView(iv);    
            try{
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){                                                            
                File f = new File(root, "TAG_test.png");
                FileOutputStream out = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();
            }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    Any help would be appreciated.
    Tnx.

    Advertisement
  2. KeithG

    KeithG Well-Known Member

    your bitmap is opaque probably.

    createBitmap()

    The description about the Bitmap.Config is different is different from the factory call your using.

    You may need to use a different Config. Probably one that supports alpha per pixel :)
  3. android88

    android88 Member

    Thanks alot! All the other configs have alpha per pixel. Do you know anythint about these different configs? Is it going to have consquences that i picked this config, because i couldn't find anythinig about it.
  4. KeithG

    KeithG Well-Known Member

    I can't respond intelligently on that... as I really haven't looked @ the underlying code.

    But I would imaging, the formats define how much information is stored about each pixel, thereby giving you more flexibility on what you can achieve. This usually translates to increased memory pressure, and transform complexity (decreasing overall speed). This of course is highly dependent on your average use case image size.

    But that's probably all academic anyway... if you need to use it.. you need to use it :)

    As far as which one to use.... I'd suggest using a Singleton / or static final dec that copies the Image format configuration from the api, so you can change it in one place, and change that format wholesale if you decide that you need to change to another format that supports alpha per-pixel.

Share This Page