Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android canvas draw rectangle

how to draw empty rectangle with etc. borderWidth=3 and borderColor=black and part within rectangle don't have content or color. Which function in Canvas to use

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

Thanks.

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); 

It draws rectangle and fill it with black color but I want just "frame" around like this image:

enter image description here

like image 514
Kec Avatar asked Sep 08 '11 07:09

Kec


People also ask

Which method of the canvas class is used for drawing a rectangle?

The rect() method creates a rectangle. Tip: Use the stroke() or the fill() method to actually draw the rectangle on the canvas.

What class do we use to draw shapes and bitmaps?

Basically, Canvas is a class in Android that performs 2D drawing onto the screen of different objects.


2 Answers

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

like image 73
pandur Avatar answered Sep 30 '22 17:09

pandur


Assuming that "part within rectangle don't have content color" means that you want different fills within the rectangle; you need to draw a rectangle within your rectangle then with stroke width 0 and the desired fill colour(s).

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 );      }  } 

The activity to start it:

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 turn out this way:

enter image description here

like image 36
DonGru Avatar answered Sep 30 '22 15:09

DonGru