Saving a png with transparency

Last Updated:

  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 (Text):
    2. public class TextToImage3 extends Activity {
    3.     /** Called when the activity is first created. */
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.main);
    9.         Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
    11.         int height = display.getHeight(); // not exactly correct
    12.         int width = display.getWidth(); // not exactly correct
    15.         Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
    16.         Canvas c = new Canvas(bitmap);
    17.         c.drawColor(Color.TRANSPARENT);        
    19.         Paint paint = new Paint();    
    20.         paint.setTextSize(60);
    21.         paint.setColor(Color.WHITE);
    22.         c.drawText("Test", 100, 300, paint);
    24.         ImageView iv = new ImageView(this);
    25.         iv.setImageBitmap(bitmap);
    26.         setContentView(iv);    
    27.         try{
    28.         File root = Environment.getExternalStorageDirectory();
    29.         if (root.canWrite()){                                                            
    30.             File f = new File(root, "TAG_test.png");
    31.             FileOutputStream out = new FileOutputStream(f);
    32.             bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    33.             out.close();
    34.         }
    35.         } catch (IOException e) {
    36.             e.printStackTrace();
    37.         }
    38.     }
    39. }
    Any help would be appreciated.

  2. KeithG

    KeithG Well-Known Member

    your bitmap is opaque probably.


    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