Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old January 13th, 2010, 03:19 PM   #1 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Jan 2010
Posts: 17
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving a png with transparency

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.

Advertisements
android88 is offline  
Reply With Quote
sponsored links
Old January 13th, 2010, 03:51 PM   #2 (permalink)
Member
 
Join Date: Nov 2009
Posts: 103
 
Device(s): Droid
Carrier: Not Provided

Thanks: 0
Thanked 4 Times in 4 Posts
Default

Quote:
Originally Posted by android88 View Post
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.

Quote:
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
KeithG is offline  
Reply With Quote
Old January 14th, 2010, 03:52 AM   #3 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Jan 2010
Posts: 17
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
android88 is offline  
Reply With Quote
Old January 14th, 2010, 10:22 AM   #4 (permalink)
Member
 
Join Date: Nov 2009
Posts: 103
 
Device(s): Droid
Carrier: Not Provided

Thanks: 0
Thanked 4 Times in 4 Posts
Default

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.
KeithG is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:24 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.