How to mask out a simple region when painting in Android?

北城余情 提交于 2019-12-05 16:07:01
public class ClippView extends View{
    private Paint paint= new Paint();

    public ClippView(Context context) {
        super(context);
    }


    private Region protect;
    /*
     * (non-Javadoc)
     * @see android.view.View#onDraw(android.graphics.Canvas)
     * @since Apr 12, 2013
     * @author rajeshcp
     */
    @Override
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas);
        // view.buildDrawingCache();      


        int w = canvas.getWidth();
        int h = canvas.getHeight();
        int w3 = w / 3;
        int h3 = h / 3;
        canvas.drawLine(0, 0, w, h, paint);
        protect = (protect == null) ? new Region(w3, h3, 2 * w / 3, 2 * h / 3) : protect;
        canvas.clipRegion(protect, Op.DIFFERENCE);
        canvas.drawColor(Color.MAGENTA);


    }

}

Do this I think this is what you want.

Why not draw four rectangles as the mask like this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!