Fill the complete canvas but keep the bound fill area as it is like circle, rectangle

前端 未结 3 1914
灰色年华
灰色年华 2020-12-09 23:42

possible duplicate

Hello friends,

I creating paint application, I have problem in that. If I draw the rectangle without fill and or another like bound area a

3条回答
  •  萌比男神i
    2020-12-10 00:43

    here is the code (you have to bound the shape on touch event otherwise it change the color of shape works):

    public class ttt extends View {
    MyShape myShape;
    public ttt(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        myShape = new MyShape();
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        myShape.setPaint(paint);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        myShape.onDraw(canvas);     
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        int x = (int) event.getX();
        int y = (int) event.getY();
    
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Paint paint = new Paint();
            paint.setColor(Color.BLUE);
            myShape.setPaint(paint);
            invalidate();
            break;
    
        default:
            break;
        }
    
        return super.onTouchEvent(event);
    }
    
    class MyShape {
        private Paint paint;
        public MyShape() {
            // TODO Auto-generated constructor stub
        }
        public void onDraw(Canvas canvas){
            canvas.drawCircle(15, 15, 30, getPaint());
        }
        /**
         * @param paint the paint to set
         */
        public void setPaint(Paint paint) {
            this.paint = paint;
        }
        /**
         * @return the paint
         */
        public Paint getPaint() {
            return paint;
        }
    
    }
    

    }

提交回复
热议问题