我想在活动布局的顶部创建层,我需要像矩形或圆形一样打孔,就像附加的图像一样。我通过使用 SurfaceView 做到了这一点,它在 android 上的较低版本上运行良好,但在 4+ 版本上却没有
[示例屏幕截图] ( https://docs.google.com/file/d/0B3yZokwUGKFgTkM1ek1nZHNyZ2s/edit?usp=sharing )
我的代码是
public class SurfacePanel extends SurfaceView implements SurfaceHolder.Callback{
public SurfacePanel(Context context) {
super(context);
init();
}
public SurfacePanel(Context context, AttributeSet attrs) {
super(context, attrs);
this.CommonInit();
}
private void init() {
setZOrderOnTop(true); //necessary
getHolder().setFormat(PixelFormat.TRANSPARENT);
getHolder().addCallback(this);
setBackgroundColor(Color.TRANSPARENT);
}
public void onDraw(Canvas canvas){
Path path = new Path();
path.moveTo(10, 10);
path.lineTo(115, 10);
path.lineTo(115, 260);
path.lineTo(10, 260);
path.close();
canvas.clipPath(path, Region.Op.DIFFERENCE);
canvas.clipRect(150, 30, 200, 100, Region.Op.DIFFERENCE);
canvas.drawARGB(180, 0, 0, 0);
super.onDraw(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
有人请帮我解决这个问题。
提前致谢。