画笔 画布 圆角 遮罩 覆盖

 

 

 

 

1、需求:画一个矩形,再画一个圆形覆盖上去,但这个圆形只展示与矩形重叠的那部分

public class MainActivity extends AppCompatActivity {

    private int SCREEN_W;
    private int SCREEN_H;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        final MyView myView = new MyView(this);
        layout.addView(myView);
        setContentView(layout);
    }

    class MyView extends View {
        private Paint mPaint;
        private Paint mPaint2;

        public MyView(Context context) {
            super(context);
            setFocusable(true);
            setScreenWH();
            initPaint();
        }

        private void setScreenWH() {
            DisplayMetrics dm = new DisplayMetrics();
            dm = this.getResources().getDisplayMetrics();
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            SCREEN_W = screenWidth;
            SCREEN_H = screenHeight;
        }

        private void initPaint() {
            //画笔
            mPaint = new Paint();
            mPaint.setColor(Color.BLUE);

            //paint2
            mPaint2 = new Paint();
            mPaint2.setColor(Color.RED);
            mPaint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        }

        @Override
        protected void onDraw(Canvas canvas) {
            //如果是在这里的Canvas进行操作的话,需要在新的图层上进行操作,所以要调用canvas的saveLayer和restoreToCount,不然的话DST可能会是整个屏幕
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            //先画一个矩形,也就是目标图形DST
            RectF rectF = new RectF(SCREEN_W/2-100, SCREEN_H/2-100, SCREEN_W/2+100, SCREEN_H/2+100);
            canvas.drawRoundRect(rectF, 70, 70, mPaint);
            //再画一个圆形,也就是当前资源图形SRC
            canvas.drawCircle(SCREEN_W/2+100, SCREEN_H/2, 100f, mPaint2);
            canvas.restoreToCount(layerId);
        }
    }
}

 

2、需求:画一个四个角的遮罩层

public class MainActivity extends AppCompatActivity {

    private int SCREEN_W;
    private int SCREEN_H;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        final MyView myView = new MyView(this);
        layout.addView(myView);
        setContentView(layout);
    }

    class MyView extends View {
        private Paint mPaint;
        private Paint mPaint2;

        public MyView(Context context) {
            super(context);
            setFocusable(true);
            setScreenWH();
            initPaint();
            setBackgroundColor(Color.YELLOW);

        }

        private void setScreenWH() {
            DisplayMetrics dm = new DisplayMetrics();
            dm = this.getResources().getDisplayMetrics();
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            SCREEN_W = screenWidth;
            SCREEN_H = screenHeight;
        }

        private void initPaint() {
            //画笔
            mPaint = new Paint();
            mPaint.setColor(Color.BLUE);

            //paint2
            mPaint2 = new Paint();
            mPaint2.setColor(Color.RED);
            mPaint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        }

        @Override
        protected void onDraw(Canvas canvas) {
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            RectF rectF = new RectF(SCREEN_W/2-100, SCREEN_H/2-100, SCREEN_W/2+100, SCREEN_H/2+100);
            canvas.drawRect(rectF, mPaint);
            canvas.drawRoundRect(rectF, 70, 70, mPaint2);
            canvas.restoreToCount(layerId);
        }

    }

}

或者可以用新创建一个画布和一个bitmap来做,看起来会比较清晰,且好拓展

public class MainActivity extends AppCompatActivity {

    private int SCREEN_W;
    private int SCREEN_H;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        final MyView myView = new MyView(this);
        layout.addView(myView);
        setContentView(layout);
    }

    class MyView extends View {
        private Bitmap mBitmap;
        private Paint mPaint = new Paint();

        public MyView(Context context) {
            super(context);
            setFocusable(true);
            setScreenWH();
            setBackgroundColor(Color.YELLOW);
            mBitmap = createBitmap();

        }

        private void setScreenWH() {
            DisplayMetrics dm = new DisplayMetrics();
            dm = this.getResources().getDisplayMetrics();
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            SCREEN_W = screenWidth;
            SCREEN_H = screenHeight;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            if(mBitmap != null){
                canvas.drawBitmap(mBitmap, SCREEN_W/2, SCREEN_H/2, mPaint);
            }
        }
    }

    private Bitmap createBitmap(){
        //由于是在新的画布上进行创造,所以也不需要执行saveLayer和restoreToCount
        Bitmap bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        //先画外部的矩形
        RectF rectF = new RectF(0, 0, 200, 200);
        canvas.drawRect(rectF, paint);
        //先画内部的圆角矩形,并挖空
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawRoundRect(rectF, 70, 70, paint);
        return bitmap;
    }

}

 

posted on 2022-12-16 19:02  赵子隆  阅读(43)  评论(0编辑  收藏  举报

导航