1

I've got a RelativeLayout with a View and an ImageView overlaying it.
Now, I'm modifying a part of ImageView's bitmap to make it transparent & invalidate the region.
That works, but I don't see the first View underneath, just Activity's background.
Tried invalidating that View and android.R.id.content, no luck.

Question: Given that View A overlays View B, how can I make parts of View B visible when respective parts of View A become transparent?

Sandeep R
  • 2,284
  • 3
  • 25
  • 51
yanchenko
  • 56,576
  • 33
  • 147
  • 165
  • Can you post the xml? – Niko Jan 23 '14 at 11:33
  • considering your question, that should be automatic, recently done some project where I just put imageview over other components, and then you erase image with your finger, nothing special. Provide more info like xml or something you can share there is probably some other problem. – Marko Lazić Jan 23 '14 at 11:37
  • is the bitmap background or source for the ImageView. Are you sure that the views overlap ? – Blackbelt Jan 23 '14 at 11:58

3 Answers3

0

This is my striped solution, first screenshots for the better understanding

Image before deleting

Image after deleting

XML:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/RelativeLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/bck"
   android:orientation="vertical" >

<View
    android:id="@+id/view1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/view" />

<com.example.stackoverflow.CustomView
    android:id="@+id/CustomView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/imageview" />

  </RelativeLayout>

MainActivity:

public class MainActivity extends Activity implements OnClickListener{  

private CustomView imageView;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imageView = (CustomView)findViewById(R.id.CustomView1);
    imageView.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    imageView.drawCircle();
}
}

Custom View:

  public class CustomView extends ImageView {

private Canvas mCanvas;
private Paint mBitmapPaint;
private Paint mPaint;
private Bitmap mBitmap;

public CustomView(Context context) {
    super(context);
    initialize();
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
}

private void initialize() {
    if (!isInEditMode()) {      
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);            
        mBitmapPaint.setStyle(Style.STROKE);

        mPaint = new Paint();
        mPaint.setStyle(Style.FILL);
        mPaint.setStrokeWidth(50);
        mPaint.setMaskFilter(null);             
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        reset();
    }
}

public void reset(){
    Bitmap bmp = ((BitmapDrawable)getDrawable()).getBitmap();
    mBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);

    this.setVisibility(View.VISIBLE);

    mCanvas = new Canvas(mBitmap);
}   

public void drawCircle(){
    mCanvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2, mPaint);

    //this.setImageBitmap(mBitmap);
    this.invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    if (isInEditMode()) {
        super.onDraw(canvas);
    }
    else {
        //super.onDraw(canvas);
           mBitmapPaint.setColor(0xFF000000);
           if(mBitmap != null){
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);         
              }
       }
   }

  }

Notice commented part, that you can just set your transparent image as drawable or redraw it like I did, it depends on your other needs. Also notice that PNG image already had Alpha channel.

Hope this helps and enjoy your work.

Marko Lazić
  • 883
  • 7
  • 9
0

The issue was with the bitmap itself.

Here's how I was converting it to mutable:

Bitmap mutableBitmap = sourceBitmap.copy(bm.getConfig(), true); // getConfig() returned ARGB_8888

and here how it works:

Bitmap mutableBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(),
                    bm.getConfig());
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawBitmap(sourceBitmap, 0, 0, null);
yanchenko
  • 56,576
  • 33
  • 147
  • 165
-1

Try changing Relative layout to Framelayout

Mr.Rao
  • 321
  • 2
  • 6