安卓画布绘制矩形

如何绘制空的矩形等 borderWidth = 3和 borderColor = 黑色和部分在矩形没有内容或颜色。在 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)

谢谢。

我试试这个例子

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

它绘制矩形和填充它的黑色,但我想只是“框架”周围 就像这张图片:

enter image description here

342225 次浏览
paint.setStrokeWidth(3);


paint.setColor(BLACK);

任何一个 drawRect都可以。

假设“ 矩形内的部分没有内容颜色”意味着在矩形内需要不同的填充,那么您需要在矩形内绘制一个矩形,然后使用笔画宽度0和所需的填充颜色。

例如:

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


}


}

启动活动:

开始绘制 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);


}
}

最后会变成这样:

enter image description here

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

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

不知道这是否为时已晚,但我解决这个问题的方法是画四个细长的矩形,它们组成了一个大的边框。用一个矩形绘制边框似乎是可撤销的,因为它们都是不透明的,所以应该分别绘制边框的每个边缘。

创建一个新的类 MyView, Which extends View。覆盖 onDraw(Canvas canvas)方法在 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)


}


}

然后使用我们的自定义视图 MyView 将 Java 活动移动到 setContentView()

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

详情可浏览此网页

Http://developer.android.com/reference/android/graphics/canvas.html

代码是很好的只是 setStyle 的绘画中风

paint.setStyle(Paint.Style.STROKE);