Покопавшись по глубинам eSO нашел решение, которое для меня работает. Кастомный FrameLayout
, который добавляется в корневой RelativeLayout
последним (поверх всего):
public class OverlayView extends FrameLayout {
private Bitmap bitmap;
private int paddings, offset;
private Rect rect;
public OverlayView(Context context) {
super(context);
init();
}
public OverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public OverlayView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
rect = new Rect(0, 0, 0, 0);
offset = (int) (-24 * getContext().getResources().getDisplayMetrics().density);
paddings = (int) getContext().getResources().getDimension(R.dimen.paddings);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
if (bitmap == null) {
createWindowFrame();
}
canvas.drawBitmap(bitmap, 0, 0, null);
}
protected void createWindowFrame() {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas osCanvas = new Canvas(bitmap);
RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(getResources().getColor(R.color.dark));
osCanvas.drawRect(outerRectangle, paint);
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
osCanvas.drawRect(rect, paint);
}
public void setView(View v) {
int xStart = getRelativeLeft(v) - paddings;
int yStart = getRelativeTop(v) - paddings +offset;
int xEnd = xStart + v.getWidth() + paddings * 2;
int yEnd = yStart + v.getHeight() + paddings * 2;
rect = new Rect(xStart, yStart, xEnd, yEnd);
createWindowFrame();
postInvalidate();
}
@Override
public boolean isInEditMode() {
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
bitmap = null;
}
private int getRelativeLeft(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getLeft();
else
return myView.getLeft() + getRelativeLeft((View) myView.getParent());
}
private int getRelativeTop(View myView) {
if (myView.getParent() == myView.getRootView())
return myView.getTop();
else
return myView.getTop() + getRelativeTop((View) myView.getParent());
}
}
offset
нужен, чтобы изображение не смещалось по оси y.
Использование:
screen.setView(myView);
Надеюсь, кому-нибудь поможет ;)