• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Saving a png with transparency

android88

Newbie
Jan 6, 2010
17
0
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.
 
Code:
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
        Canvas c = new Canvas(bitmap);
        c.drawColor(Color.TRANSPARENT);

your bitmap is opaque probably.

createBitmap()

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

Code:
config	The bitmap config to create. If the config does not support per-pixel
                alpha  (e.g. RGB_565), then the alpha bytes in the colors[] will be
                ignored (assumed to be FF)

You may need to use a different Config. Probably one that supports alpha per pixel :)
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones