public class MaskView extends View {    /**     * 遮罩颜色     */    private int maskColor = Color.argb(100, 0, 0, 0);    /**     * 镂空矩形     */    private Rect frame = new Rect();    /**     * 镂空边框     */    private Paint border = new Paint(Paint.ANTI_ALIAS_FLAG);    /**     * 镂空区域     */    private Paint eraser = new Paint(Paint.ANTI_ALIAS_FLAG);    private Path path = new Path();    public MaskView(Context context) {        super(context);        init();    }    public MaskView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        init();    }    public MaskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        // 硬件加速不支持,图层混合。        setLayerType(View.LAYER_TYPE_SOFTWARE, null);        // 取景框颜色、线宽        border.setColor(Color.WHITE);        border.setStyle(Paint.Style.STROKE);        border.setStrokeWidth(5);        eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        int width = (int) (w * 0.7f);        int height = (int) (h * 0.7f);        int left = (w - width) / 2;        int top = (h - height) / 2;        int right = width + left;        int bottom = height + top-150;        frame.left = left;        frame.top = top;        frame.right = right;        frame.bottom = bottom;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int left = frame.left;        int top = frame.top;        int right = frame.right;        int bottom = frame.bottom;        fillRectRound(left, top, right, bottom, 200, 200);        canvas.drawColor(maskColor);        canvas.drawPath(path, border);        canvas.drawPath(path, eraser);    }    private void fillRectRound(float left, float top, float right, float bottom, float rx, float ry) {        path.reset();        float width = right - left;        float height = bottom - top;        float lineWidth = (width - (2 * rx));        float lineHeight = (height - (2 * ry));        path.moveTo(left, top + ry);        path.rQuadTo(0, -ry, rx, -ry);        path.rLineTo(lineWidth, 0);        path.rQuadTo(rx, 0, rx, ry);        path.rLineTo(0, lineHeight);        path.rQuadTo(0, ry, -rx, ry);        path.rLineTo(-lineWidth, 0);        path.rQuadTo(-rx, 0, -rx, -ry);        path.rLineTo(0, -lineHeight);        path.close();//        RectF roundRect = new RectF(left, top, right, bottom);//        path.addRoundRect(roundRect, rx, ry, Path.Direction.CW);    }    public Rect getFrameRect() {        return new Rect(frame);    }}

//引用

MaskView maskView =new MaskView(this); addContentView(maskView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

更多相关文章

  1. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  2. Android(安卓)组件资源库
  3. Android(安卓)5.x特性概览二
  4. Android(安卓)设置EditText光标Curso颜色及粗细
  5. android 中使用TextView实现分段显示不同颜色的字符串
  6. android和j2me之清屏(clearScreen)
  7. 【android开发】styles.xml常用的设置属性汇总
  8. android和j2me之清屏(clearScreen)
  9. Android沉浸式状态栏(透明状态栏)

随机推荐

  1. MySqli 中预处理类 stmt
  2. 如何使用另一个表中的多个列替换表中的一
  3. 哪个更好的大查询或多个小查询?
  4. SQL:通过将两个列映射到彼此来更新表。
  5. mysql 基于position 恢复数据库
  6. MySQL导出和导入SQL脚本
  7. 如何获得MySQL中某一行的偏移量?
  8. mysql-5.7.10-winx64 绿色版安装办法
  9. mysql主从复制配置操作以及主主配置宕机
  10. 如何使用PDO从MySQL获取正确的数据类型?