首页 文章

ImageView中的透明位图

提问于
浏览
2

我想在保持Bitmaps分离的同时在现有位图上绘制一些东西 . 因此,我们的想法是将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位图,因此它将匹配掩码位图 . 我在面具Bitmap的中间绘制一个红色圆圈,完美显示 . 但是,蒙版位图不会显示背景图像,而是显示黑色背景 .

所以我尝试过:

  • 将ImageView的背景颜色设置为透明

  • 使用.eraseColor将蒙版位图的像素设置为透明

  • 将Bitmap配置设置为ARGB_8888

  • 设置遮罩ImageView的alpha

这似乎都不起作用 . 当我执行eraseColor(Color.BLUE)时,背景为蓝色,中间为红色圆圈 . 当我设置遮罩ImageView的alpha时,背景仍为黑色 . 当我注释掉setImageBitmap(mMaskBitmap)时,背景图像显示 .

我在这里错过了什么?

1 回答

  • 2

    你的背景错了 . 更改

    ImageView mask = (ImageView) findViewById(R.id.photo_mask);
    image.setImageBitmap(mMaskBitmap);
    

    ImageView mask = (ImageView) findViewById(R.id.photo_mask);
    mask.setImageBitmap(mMaskBitmap);
    

相关问题