Save Bitmap with Alpha

0

I have the following class:

public class TesteMultiImage extends ActionBarActivity {

Uri URI;
String ImagePath;
Button button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teste_multi_image);

        button = (Button)findViewById(R.id.button3);

        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm");
        final String currentDateandTime = sdf.format(new Date());
        final RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rl.buildDrawingCache();
                Bitmap bmap = rl.getDrawingCache();
                ImagePath = MediaStore.Images.Media.insertImage(
                        getContentResolver(),
                        bmap,
                        "banner" + currentDateandTime,
                        "banner"
                );

                URI = Uri.parse(ImagePath);

                Toast.makeText(getApplicationContext(), "Imagem Salva", Toast.LENGTH_LONG).show();
            }
        });
    }

}

It saves my relative layout as a bitmap. But it saves with a black background, and needs to be saved with a transparent background. When trying to configure:

bmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);

It does not work. How to solve?

    
asked by anonymous 09.06.2017 / 20:08

1 answer

0

The mode:

  

ARGB_8888

Supports transparency, but initially the alpha channel is 'set' for opaque. You need to clear this pattern. Try the following:

Canvas c = new Canvas(bm);
c.drawColor(0, Mode.CLEAR);

Font (StackOverflow)

    
09.06.2017 / 21:26