Android canvas drawing rectangle

how to draw an empty rectangle with etc. borderWidth = 3 and borderColor = black, and the part inside the rectangle has no content or color. What function to use in canvas

void drawRect(float left, float top, float right, float bottom, Paint paint) void drawRect(RectF rect, Paint paint) void drawRect(Rect r, Paint paint) 

Thank.

I try this example

 Paint myPaint = new Paint(); myPaint.setColor(Color.rgb(0, 0, 0)); myPaint.setStrokeWidth(10); c.drawRect(100, 100, 200, 200, myPaint); 

He draws a rectangle and fills it with black, but I just want a "frame" around like this image:

enter image description here

+84
android android canvas
Sep 08 '11 at 7:18
source share
6 answers

Try paint.setStyle(Paint.Style.STROKE) ?

+130
Dec 22 '11 at 2:58 p.m.
source share

Assuming that “the part inside the rectangle does not have the color of the content” means that you want to distinguish between the fillings inside the rectangle; you need to draw a rectangle inside your rectangle, then with a stroke width of 0 and the desired fill color.

For example:

DrawView.java

 import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class DrawView extends View { Paint paint = new Paint(); public DrawView(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { paint.setColor(Color.BLACK); paint.setStrokeWidth(3); canvas.drawRect(30, 30, 80, 80, paint); paint.setStrokeWidth(0); paint.setColor(Color.CYAN); canvas.drawRect(33, 60, 77, 77, paint ); paint.setColor(Color.YELLOW); canvas.drawRect(33, 33, 77, 60, paint ); } } 

Actions to start:

StartDraw.java

 import android.app.Activity; import android.graphics.Color; import android.os.Bundle; public class StartDraw extends Activity { DrawView drawView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); drawView = new DrawView(this); drawView.setBackgroundColor(Color.WHITE); setContentView(drawView); } } 

... will be as follows:

enter image description here

+114
Sep 08 2018-11-11T00:
source share
 //white background canvas.drawRGB(255, 255, 255); //border properties paint.setColor(Color.BLACK); paint.setStrokeWidth(0); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(100, 100, 200, 200, paint); 
+11
Sep 25 '12 at 18:27
source share
 paint.setStrokeWidth(3); paint.setColor(BLACK); 

and any of your drawRect should work.

+6
Sep 08 2018-11-11T00:
source share

Create a new MyView, Which extends View class MyView, Which extends View . Override the onDraw(Canvas canvas) method to draw a rectangle on Canvas .

 import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; public class MyView extends View { Paint paint; Path path; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init(){ paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawRect(30, 50, 200, 350, paint); canvas.drawRect(100, 100, 300, 400, paint); //drawRect(left, top, right, bottom, paint) } } 

Then move your Java activity to setContentView() using our custom view MyView.Call this way.

  public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } 

You can find more information here.

http://developer.android.com/reference/android/graphics/Canvas.html

+6
May 25 '15 at 10:54
source share

I don’t know if it’s too late, but the way I decided it was to draw four thin rectangles that together made up one big border. Drawing a border with a single rectangle seems invalid because they are all opaque, so you must draw each edge of the border separately.

0
Jan 14 '14 at 17:14
source share



All Articles