View Single Post
Old January 13th, 2010, 03:19 PM   #1 (permalink)
android88
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.
android88 is offline  
Reply With Quote