使位图的背景透明android当发送它的Uri到另一个活动



我在这里创建了一个位图,它的背景是透明的,但它不起作用,并将背景设置为黑色:

Bitmap baseBitmap = null;
Canvas canvas;
Bitmap mergedBitmap = null;
baseBitmap = imageViewToBitmap(imgForground);
mergedBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), Bitmap.Config.RGB_565);
canvas = new Canvas(mergedBitmap);
canvas.drawBitmap((baseBitmap), matrix, null);

下面是我用来从imageview中提取位图的函数:

public Bitmap imageViewToBitmap(ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = Bitmap.createBitmap(drawable.getMinimumWidth(), drawable.getMinimumHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

发送给其他活动:

Intent intent = new Intent(EditImageActivity.this, FilterImageActivity.class);
intent.putExtra("uri", getImageUri(getApplicationContext(), bitmap).toString());
startActivity(intent);

函数getImageUri是:

public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
imgForground.setImageBitmap(inImage);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

在另一个活动中使用getintent将图像背景设置为黑色:

Intent intent = getIntent();
Uri uri = Uri.parse(intent.getStringExtra("uri"));
imageView.setImageBitmap(bmp);

添加以下内容使背景透明,

canvas.drawColor(Color.TRANSPARENT)

您使用RGB_565选项创建了位图。

mergedBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), Bitmap.Config.RGB_565); // your code

它不支持透明,所以你需要使用ARGB_8888来设置透明。

Canvas canvas;
Bitmap mergedBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), Bitmap.Config.ARGB_8888); // changed code
Bitmap baseBitmap = imageViewToBitmap(imgForground);
canvas = new Canvas(mergedBitmap);
canvas.drawColor(0);
canvas.drawBitmap(baseBitmap, matrix, null);

相关内容

  • 没有找到相关文章