Drawing a filled rectangle with a border in android

后端 未结 3 1036
忘掉有多难
忘掉有多难 2020-12-08 18:43

Is there any way in Android to draw a filled rectangle with say a black border. My problem is that the canvas.draw() takes one paint object, and to my knowledge the paint ob

3条回答
  •  感动是毒
    2020-12-08 18:56

    Try paint.setStyle(Paint.Style.FILL) and paint.setStyle(Paint.Style.STROKE).

    Paint paint = new Paint();
    Rect r = new Rect(10, 10, 200, 100);
    
    @Override
    public void onDraw(Canvas canvas) {
        // fill
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.MAGENTA); 
        canvas.drawRect(r, paint);
    
        // border
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        canvas.drawRect(r, paint);
    }
    

提交回复
热议问题