ChatGPT解决这个技术问题 Extra ChatGPT

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:

https://i.stack.imgur.com/2aUXE.png


Y
Yuck

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


J
Juan Cortés

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:

https://i.stack.imgur.com/J1z1o.png


Actually this is what I need screencast.com/t/oFYF5kGtw5B that red rectangle after I draw all just to add that rectangle is that possible?
This is the solution paint.setStyle(Style.STROKE); Thanks for help.
I think DonGru and Juan gave a great explanation here.. but the exact one line answer to what author is asking for has been provided below by @Yuck -- paint.setStyle(Paint.Style.STROKE)
This answer doesn't fit with the answer. It doesn't show the corrent answer. Pandur's answer is the correct one.
T
Tiago Almeida
//white background
canvas.drawRGB(255, 255, 255);
//border's properties
paint.setColor(Color.BLACK);
paint.setStrokeWidth(0);        
paint.setStyle(Paint.Style.STROKE);         
canvas.drawRect(100, 100, 200, 200, paint);

I
IntelliJ Amiya

Create a new class MyView, Which extends View. Override the onDraw(Canvas canvas) method to draw 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));
  }

For more details you can visit here

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


P
Pratik
paint.setStrokeWidth(3);

paint.setColor(BLACK);

and either one of your drawRect should work.


u
user3013823

The code is fine just setStyle of paint as STROKE

paint.setStyle(Paint.Style.STROKE);

S
Steven

Don't know if this is too late, but the way I solved this was to draw four thin rectangles that together made up a one big border. Drawing the border with one rectangle seems to be undoable since they're all opaque, so you should draw each edge of the border separately.


Or two rectangles one inside other :)
Or a circle, a circle of the background color, a three segmented drawLines() and a rectangle, for the fourth line.