我想在现有位图上绘制一些东西,同时将位图分开。因此,我们的想法是将一个RelativeLayout和两个ImageView堆叠在一起,顶部的一个放着要绘制的位图,底部的一个拿着带有背景图片的位图。
layout.xml(仅相关部分)
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/photo_mask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" />
<ImageView
android:id="@+id/photo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
layout.java(仅相关部分)
setContentView(R.layout.layout);
ImageView image = (ImageView) findViewById(R.id.photo);
image.setImageBitmap(mSomeImage);
mMaskPaint = new Paint();
mMaskPaint.setColor(0xFF0000);
mMaskPaint.setAlpha(128);
mMaskBitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888);
mMaskBitmap.eraseColor(Color.TRANSPARENT);
mMaskCanvas = new Canvas(mMaskBitmap);
mMaskCanvas.drawCircle(64, 64, 10, mMaskPaint);
ImageView mask = (ImageView) findViewById(R.id.photo_mask);
image.setImageBitmap(mMaskBitmap);
请注意,mSomeImage是一个128x128位图,因此它将与掩码位图匹配。我正在掩码位图的中间画一个红色的圆圈,它显示得很完美。但是,遮罩"位图"不会显示背景图像,而是显示黑色背景。
所以我尝试了:
- 将ImageView的背景色设置为透明
- 使用.esersColor将遮罩"位图"的像素设置为透明
- 将位图配置设置为ARGB_8888
- 设置遮罩的alpha ImageView
这些似乎都不起作用。当我擦除颜色(Color.BLUE)时,背景是蓝色,红色圆圈在中间。当我设置遮罩ImageView的alpha时,背景仍然是黑色的。当我注释掉setImageBitmap(mMaskBitmap)时,会显示背景图像。
我在这里错过了什么?
您的背景错误。更改
ImageView mask = (ImageView) findViewById(R.id.photo_mask);
image.setImageBitmap(mMaskBitmap);
至
ImageView mask = (ImageView) findViewById(R.id.photo_mask);
mask.setImageBitmap(mMaskBitmap);